import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'custom.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: GeneratePassword()
);
}
}
class GeneratePassword extends StatefulWidget {
@override
_GeneratePasswordState createState() => _GeneratePasswordState();
}
class _GeneratePasswordState extends State<GeneratePassword> {
final _controller = TextEditingController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) =>
Scaffold(
key:scaffoldKey,
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.cyan,
title: Text('Flutter Generate Random Password'),
),
body: Container(
padding: EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
children: [
Text("Generate Strong Random Password", style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold
),),
],
),
SizedBox(height: 15,),
TextFormField(
controller: _controller,
readOnly: true,
enableInteractiveSelection: false,
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.cyan,),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.cyan),
),
suffixIcon: IconButton(
onPressed: () {
final data = ClipboardData(text: _controller.text);
Clipboard.setData(data);
final snackbar = SnackBar(
content: Text("Password Copy"));
scaffoldKey.currentState
..removeCurrentSnackBar()
..showSnackBar(snackbar);
},
icon: Icon(Icons.copy))
),
),
SizedBox(height: 15,),
buildButtonWidget()
],
),
),
);
Widget buildButtonWidget() {
return ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black
),
onPressed: () {
final password = generatePassword();
_controller.text = password;
},
child: Text("Password Generate", style: TextStyle(color: Colors.white),)
);
}
}
import 'dart:math';
String generatePassword({
bool letter = true,
bool isNumber = true,
bool isSpecial = true,
}) {
final length = 20;
final letterLowerCase = "abcdefghijklmnopqrstuvwxyz";
final letterUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
final number = '0123456789';
final special = '@#%^*>\$@?/[]=+';
String chars = "";
if (letter) chars += '$letterLowerCase$letterUpperCase';
if (isNumber) chars += '$number';
if (isSpecial) chars += '$special';
return List.generate(length, (index) {
final indexRandom = Random.secure().nextInt(chars.length);
return chars[indexRandom];
}).join('');
}
No comments:
Post a Comment