Flutter密码生成插件safe_password_generator的使用
Flutter密码生成插件safe_password_generator的使用
safe_password_generator
是一个用于Dart和Flutter的安全且可定制的密码生成器。它可以生成包含大写、小写、数字和特殊字符的强密码,并计算密码强度和获取强度标签。
特性
- 生成具有自定义长度的安全密码
- 包含/排除大写、小写、数字和特殊字符
- 计算密码强度(0到100)
- 获取密码强度标签(例如:“Very Strong”、“Weak”)
- 支持Dart和Flutter环境
开始使用
在你的 pubspec.yaml
文件中添加以下依赖:
dependencies:
safe_password_generator: ^1.0.0
使用方法
基本用法
在需要的地方导入包:
import 'package:safe_password_generator/safe_password_generator.dart';
void main() {
// 生成密码
final password = SafePasswordGenerator.generatePassword(
length: 16,
includeUppercase: true,
includeLowercase: true,
includeNumbers: true,
includeSpecialCharacters: true,
);
print('Generated Password: $password');
// 计算密码强度
final strength = SafePasswordGenerator.calculatePasswordStrength(password);
print('Password Strength: ${strength.toStringAsFixed(2)}%');
// 获取强度标签
final label = SafePasswordGenerator.getStrengthLabel(strength);
print('Strength Label: $label');
}
示例应用
以下是一个完整的Flutter示例应用,展示了如何使用 safe_password_generator
插件生成密码并显示密码强度。
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:safe_password_generator/src/safe_password_generator.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const PasswordGeneratorScreen(),
);
}
}
class PasswordGeneratorScreen extends StatefulWidget {
const PasswordGeneratorScreen({super.key});
@override
State<PasswordGeneratorScreen> createState() => _PasswordGeneratorScreenState();
}
class _PasswordGeneratorScreenState extends State<PasswordGeneratorScreen> {
final passwordCharacterController = TextEditingController();
bool includeUppercase = false;
bool includeLowercase = false;
bool includeNumber = false;
bool includeSymbol = false;
double progress = 0;
String generatedPassword = '';
@override
dispose() {
super.dispose();
passwordCharacterController.dispose();
}
bool isAnyCheckboxSelected() {
return includeUppercase || includeLowercase || includeNumber || includeSymbol;
}
Color passwordStrengthColor(double strength) {
if (strength < 40) {
return Colors.red;
} else if (strength >= 40 && strength < 60) {
return Colors.orange;
} else if (strength >= 60 && strength < 80) {
return Colors.lightGreen;
} else if (strength >= 80) {
return Colors.green;
} else {
return Colors.transparent;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Safe Password Generator'),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: [
TextField(
style: Theme.of(context).textTheme.bodySmall?.copyWith(fontSize: 16, color: Colors.black),
controller: passwordCharacterController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: const BorderSide(color: Colors.black)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: const BorderSide(color: Colors.black)),
hintText: 'Enter password length',
),
),
const SizedBox(height: 10),
PasswordTypeWidget(
title: 'Include uppercase',
currentState: includeUppercase,
onChanged: (value) {
setState(() {
includeUppercase = value!;
});
},
),
PasswordTypeWidget(
title: 'Include lowercase',
currentState: includeLowercase,
onChanged: (value) {
setState(() {
includeLowercase = value!;
});
},
),
PasswordTypeWidget(
title: 'Include numbers',
currentState: includeNumber,
onChanged: (value) {
setState(() {
includeNumber = value!;
});
},
),
PasswordTypeWidget(
title: 'Include symbol',
currentState: includeSymbol,
onChanged: (value) {
setState(() {
includeSymbol = value!;
});
},
),
const SizedBox(height: 10),
SizedBox(
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: !isAnyCheckboxSelected() ? null : Colors.blue,
),
onPressed: !isAnyCheckboxSelected()
? null
: () {
if (passwordCharacterController.text.isEmpty) {
SnackBarUtils.snackBar(context, 'Please enter password length');
return;
}
final length = int.tryParse(passwordCharacterController.text) ?? 0;
if (length <= 0) {
SnackBarUtils.snackBar(context, 'Please enter a valid password length');
return;
}
final generatePassword = SafePasswordGenerator.generatePassword(
length: length,
includeUppercase: includeUppercase,
includeLowercase: includeLowercase,
includeNumbers: includeNumber,
includeSpecialCharacters: includeSymbol,
);
setState(() {
generatedPassword = generatePassword;
progress = SafePasswordGenerator.calculatePasswordStrength(generatePassword);
});
},
child: Text(
'Generate',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: !isAnyCheckboxSelected() ? Colors.grey : null,
),
),
),
),
const SizedBox(height: 8),
CopyAbleText(text: generatedPassword),
const SizedBox(height: 12),
Text('Password Strength', style: Theme.of(context).textTheme.bodySmall),
LinearTracker(
progress: progress,
color: passwordStrengthColor(progress),
height: 10,
),
const SizedBox(height: 5),
Text(
SafePasswordGenerator.getStrengthLabel(progress),
style: Theme.of(context).textTheme.bodySmall?.copyWith(fontSize: 15),
),
],
),
),
),
);
}
}
class PasswordTypeWidget extends StatelessWidget {
final bool currentState;
final void Function(bool?)? onChanged;
final String title;
const PasswordTypeWidget({
super.key,
required this.currentState,
this.onChanged,
required this.title,
});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
if (onChanged != null) {
onChanged!(!currentState);
}
},
child: Row(
children: [
Checkbox(
value: currentState,
onChanged: onChanged,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
Text(
title,
style: Theme.of(context).textTheme.bodySmall?.copyWith(fontSize: 18, fontWeight: FontWeight.w400),
),
],
),
);
}
}
class SnackBarUtils {
static void snackBar(BuildContext context, String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
message,
style: Theme.of(context).textTheme.bodySmall?.copyWith(fontSize: 12),
),
),
);
}
}
class CopyAbleText extends StatelessWidget {
final String text;
const CopyAbleText({super.key, required this.text});
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Container(
padding: const EdgeInsets.all(6),
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
borderRadius: BorderRadius.circular(10.0),
),
child: Text(text, style: const TextStyle(fontSize: 18)),
),
),
const SizedBox(width: 9),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: text.isEmpty ? Colors.grey : Colors.blue,
),
onPressed: text.isEmpty
? null
: () async {
await Clipboard.setData(ClipboardData(text: text));
SnackBarUtils.snackBar(context, '$text copied to clipboard');
},
child: Text(
'Copy',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
fontSize: 14,
color: text.isEmpty ? Colors.grey : null,
),
),
),
],
);
}
}
class LinearTracker extends StatefulWidget {
final double progress;
final Color? color;
final bool shouldShowLabel;
final double height;
const LinearTracker({
super.key,
required this.progress,
this.color,
this.shouldShowLabel = false,
required this.height,
});
@override
State<LinearTracker> createState() => _LinearTrackerState();
}
class _LinearTrackerState extends State<LinearTracker> with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 500),
);
_animation = Tween<double>(
begin: 0,
end: widget.progress,
).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeOutCubic,
),
);
_controller.forward();
}
@override
void didUpdateWidget(covariant LinearTracker oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.progress != widget.progress) {
_animation = Tween<double>(
begin: oldWidget.progress,
end: widget.progress,
).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeOutCubic),
);
_controller
..reset()
..forward();
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (widget.shouldShowLabel)
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Padding(
padding: const EdgeInsets.only(bottom: 4),
child: Text('${_animation.value.round()}%', style: Theme.of(context).textTheme.bodySmall),
);
},
),
Stack(
alignment: Alignment.center,
children: [
Container(
height: widget.height,
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(widget.height / 2),
),
),
AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Align(
alignment: Alignment.centerLeft,
child: FractionallySizedBox(
widthFactor: (_animation.value / 100).clamp(0.0, 1.0),
child: Container(
height: widget.height,
decoration: BoxDecoration(
color: widget.color,
borderRadius: BorderRadius.circular(widget.height / 2),
),
),
),
);
},
),
if (widget.shouldShowLabel)
Text(
'${widget.progress.round()}%',
style: const TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.w500,
),
),
],
)
],
);
}
}
截图
API参考
详细API文档请访问 API reference。
希望这个示例对你有所帮助!如果有任何问题或需要进一步的帮助,请随时提问。
1 回复
更多关于Flutter密码生成插件safe_password_generator的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用safe_password_generator
插件来生成密码的示例代码。这个插件可以帮助你生成符合特定复杂性和长度要求的密码。
1. 添加依赖
首先,你需要在pubspec.yaml
文件中添加safe_password_generator
依赖:
dependencies:
flutter:
sdk: flutter
safe_password_generator: ^x.y.z # 请将x.y.z替换为最新版本号
然后运行flutter pub get
来获取依赖。
2. 导入插件
在你的Dart文件中导入该插件:
import 'package:safe_password_generator/safe_password_generator.dart';
3. 使用插件生成密码
下面是一个简单的例子,展示如何使用safe_password_generator
插件生成一个随机密码:
import 'package:flutter/material.dart';
import 'package:safe_password_generator/safe_password_generator.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String generatedPassword = '';
void _generatePassword() {
// 创建SafePasswordGenerator实例
final generator = SafePasswordGenerator();
// 生成一个包含大小写字母、数字和特殊字符的12位密码
generatedPassword = generator.generatePassword(
length: 12,
includeLowercase: true,
includeUppercase: true,
includeNumbers: true,
includeSpecialCharacters: true,
);
// 更新UI
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Safe Password Generator Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Generated Password:',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 10),
Text(
generatedPassword,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _generatePassword,
child: Text('Generate Password'),
),
],
),
),
);
}
}
解释
- 依赖添加:首先在
pubspec.yaml
文件中添加safe_password_generator
依赖。 - 导入插件:在需要生成密码的Dart文件中导入该插件。
- 生成密码:
- 创建
SafePasswordGenerator
实例。 - 使用
generatePassword
方法生成密码,可以指定密码的长度和包含的字符类型(小写字母、大写字母、数字和特殊字符)。 - 使用
setState
方法更新UI以显示生成的密码。
- 创建
这个示例展示了如何创建一个简单的Flutter应用,点击按钮即可生成一个符合特定要求的随机密码。你可以根据需要调整密码的长度和包含的字符类型。