From d01440663be949fbd46f719b901608b41c6e5b0c Mon Sep 17 00:00:00 2001 From: Naman197 <115882831+Naman197@users.noreply.github.com> Date: Tue, 25 Apr 2023 19:40:58 +0530 Subject: [PATCH] Create PasswordGenerator.dart This is a Flutter app that generates a random password based on a selected length, using a character set that includes uppercase and lowercase letters, numbers, and special characters. It includes a dropdown menu to select the password length and a button to generate a new password. --- PasswordGenerator.dart | 99 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 PasswordGenerator.dart diff --git a/PasswordGenerator.dart b/PasswordGenerator.dart new file mode 100644 index 0000000..41e1a4c --- /dev/null +++ b/PasswordGenerator.dart @@ -0,0 +1,99 @@ +import 'package:flutter/material.dart'; +import 'dart:math'; + +void main() => runApp(MyApp()); + +class MyApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Password Generator', + theme: ThemeData( + primarySwatch: Colors.blue, + ), + home: PasswordGeneratorScreen(), + ); + } +} + +class PasswordGeneratorScreen extends StatefulWidget { + @override + _PasswordGeneratorScreenState createState() => _PasswordGeneratorScreenState(); +} + +class _PasswordGeneratorScreenState extends State { + String _generatedPassword = ''; + int _passwordLength = 10; + + void _generatePassword() { + // Generate a random password of length _passwordLength + final random = Random(); + const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#\$%^&*()_+-='; + var password = ''; + for (var i = 0; i < _passwordLength; i++) { + password += charset[random.nextInt(charset.length)]; + } + + setState(() { + _generatedPassword = password; + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('Password Generator'), + ), + body: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text( + 'Generate a random password:', + style: TextStyle(fontSize: 20), + ), + SizedBox(height: 16), + Center( + child: Text( + _generatedPassword, + style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), + ), + ), + SizedBox(height: 16), + Row( + children: [ + Text( + 'Password length:', + style: TextStyle(fontSize: 18), + ), + SizedBox(width: 16), + DropdownButton( + value: _passwordLength, + onChanged: (value) { + setState(() { + _passwordLength = value!; + }); + }, + items: [ + DropdownMenuItem(value: 8, child: Text('8')), + DropdownMenuItem(value: 10, child: Text('10')), + DropdownMenuItem(value: 12, child: Text('12')), + DropdownMenuItem(value: 16, child: Text('16')), + DropdownMenuItem(value: 20, child: Text('20')), + ], + ), + ], + ), + SizedBox(height: 16), + ElevatedButton( + onPressed: _generatePassword, + child: Text('Generate'), + ), + ], + ), + ), + ); + } +}