Open In App

Flutter - Generate Strong Random Password

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to make an application in Flutter that generates random passwords that cannot be easily cracked by hackers or attackers. Here we define a method named _generatePassword that is responsible for generating random passwords. It uses a mix of lowercase letters, uppercase letters, numbers, and special characters.

Step-by-Step Implementation

Step 1: Create a New Flutter Application

Create a new Flutter application using the command Prompt. To create a new app, write the following command and run it.

flutter create app_name

To know more about it refer this article: Creating a Simple Application in Flutter


Step 2: Import the Package

First of all, import material.dart file.

import 'package:flutter/material.dart';
import 'dart:math';

Step 3: Execute the main Method

Here, the execution of our app starts.

Dart
void main() {
  runApp(MyApp());
}


Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.

Dart
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: PasswordGenerator());
  }
}


Step 5: Create PasswordGenerator Class

In this class we are going to define a method that is responsible for generating random passsword with a mixture of Uppercase,Lowercase,numbers and Special Characters.

Dart
// Method for Generating Random Passwords
void _generatePassword() {
    
    final random = Random();
    final characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#\$%^&*()_+';
    String password = '';
    
    for (int i = 0; i < 12; i++) {
        
        // Generate a random password
        password += characters[random.nextInt(characters.length)]; 
    }
    setState(() {
        
        // Update the displayed password
        _password = password; 
    });
}


When we click a Button, the above method is called an generate a Strong password with a combination of Uppercase, LowerCase, numbers, and Special Characters. Then the password is displayed using a TextView.

Dart
class PasswordGenerator extends StatefulWidget {
  @override
  _PasswordGeneratorState createState() => _PasswordGeneratorState();
}

class _PasswordGeneratorState extends State<PasswordGenerator> {
  
  String _password = '';
  
  // Method for Generating Random Passwords
  void _generatePassword() {
      
    final random = Random();
    final characters ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#\$%^&*()_+';
    String password = '';
    
    for (int i = 0; i < 12; i++) {
        
        // Generate a random password
        password += characters[random.nextInt(characters.length)]; 
    }
    
    setState(() {
      
        // Update the displayed password
        _password = password; 
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Password Generator'), 
        backgroundColor: Colors.green,
        foregroundColor: Colors.white,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Your Password:', 
              style: TextStyle(fontSize: 18),
            ),
            Text(
                
              // Displayed password
              _password, 
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 20), 
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5), 
                ),
                backgroundColor: Colors.green, 
                foregroundColor: Colors.white,
              ),
              
              // Call _generatePassword when button is pressed
              onPressed:_generatePassword, 
              child: Text('Generate Password'), 
            ),
          ],
        ),
      ),
    );
  }
}


Complete Source Code

main.dart:

Dart
import 'package:flutter/material.dart';
import 'dart:math';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(
          
          // Set the app's primary theme color
          primarySwatch: Colors.green, 
        ),
        debugShowCheckedModeBanner: false,
        home: PasswordGenerator());
  }
}

class PasswordGenerator extends StatefulWidget {
  @override
  _PasswordGeneratorState createState() => _PasswordGeneratorState();
}

class _PasswordGeneratorState extends State<PasswordGenerator> {
  String _password = '';
  
  // Method for Generating Random Passwords
  void _generatePassword() {
    final random = Random();
    final characters ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#\$%^&*()_+';
    String password = '';
    for (int i = 0; i < 12; i++) {
          
          // Generate a random password
          password += characters[random.nextInt(characters.length)]; 
    }
    setState(() {
      
      // Update the displayed password
      _password = password; 
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Password Generator'), 
        backgroundColor: Colors.green,
        foregroundColor: Colors.white,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Your Password:', 
              style: TextStyle(fontSize: 18),
            ),
            Text(
                
              // Displayed password
              _password, 
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 20), 
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5), 
                ),
                backgroundColor: Colors.green, 
                foregroundColor: Colors.white,
              ),
              
              // Call _generatePassword when button is pressed
              onPressed:_generatePassword, 
              child: Text('Generate Password'), 
            ),
          ],
        ),
      ),
    );
  }
}


Output:



Next Article

Similar Reads