Open In App

Flutter – Generate Strong Random Password

In this article, we are going to make an application in Flutter that would Generate random passwords that cannot be easily Cracked by hackers or attackers. Here we define a method named as _generatePassword method that is responsible for generating random passwords. It uses a mix of lowercase letters, uppercase letters, numbers, and special characters. A sample video is given below to get an idea about what we are going to do in this article.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to 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.






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.




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());
  }
}

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.

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

When we clicked 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 an TextView.




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++) {
      password += characters[
          // Generate a random password
          random.nextInt(characters.length)]; 
    }
    setState(() {
      // Update the displayed password
      _password = password; 
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Password Generator'), 
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Your Password:'
              style: TextStyle(fontSize: 18),
            ),
            Text(
              _password, // Displayed password
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 20), // Vertical spacing
            ElevatedButton(
              onPressed:
                  _generatePassword, // Call _generatePassword when button is pressed
              child: Text('Generate Password'), // Button text
            ),
          ],
        ),
      ),
    );
  }
}

Here is the full Code of main.dart file:




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++) {
      password += characters[
          // Generate a random password
          random.nextInt(characters.length)]; 
    }
    setState(() {
      // Update the displayed password
      _password = password; 
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Password Generator'), 
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Your Password:'
              style: TextStyle(fontSize: 18),
            ),
            Text(
              _password, // Displayed password
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 20), // Vertical spacing
            ElevatedButton(
              onPressed:
                  _generatePassword, // Call _generatePassword when button is pressed
              child: Text('Generate Password'), // Button text
            ),
          ],
        ),
      ),
    );
  }
}

Output:


Article Tags :