Flutter随机输入面板插件random_input_pad的使用
Flutter随机输入面板插件random_input_pad的使用
RandomInputPad
是一个可自定义的随机排列数字键盘小部件,专为安全的PIN输入设计。此Flutter小部件非常适合任何涉及密码或安全性相关的屏幕,其不可预测的输入布局可以增加额外的安全性。
特性
- 随机按键布局:每次初始化时按键都会被随机打乱,防止可预测的PIN输入。
- 可定制的设计:可以配置颜色、形状、按钮边框、文本标签和其他UI元素。
- 完成回调:当用户输入完成后或按下“确定”时,触发带有用户输入的
onFinish
回调函数。 - 支持自定义输入长度:设置所需的数字输入位数。
安装
在您的Flutter项目中使用 RandomInputPad
,只需将其添加到 pubspec.yaml
文件的依赖项中:
dependencies:
flutter:
sdk: flutter
random_input_pad:
然后在Dart文件中导入它:
import 'package:random_input_pad/random_input_pad.dart';
基本用法
将 RandomInputPad
小部件添加到您的widget树中,并根据需要自定义参数:
import 'package:flutter/material.dart';
import 'package:random_input_pad/random_input_pad.dart';
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Random Input Pad Example")),
body: Center(
child: RandomInputPad(
onFinish: (input) {
// 处理完成的PIN输入
print("输入的PIN: $input");
},
totalInput: 4, // 设置所需的数字位数
backgroundColor: Colors.white,
actionButtonCollor: Colors.indigo,
filledColor: Colors.green,
title: "请输入您的PIN",
titleColor: Colors.black,
numberColor: Colors.black,
buttonBorderColor: Colors.grey,
inputButtonRounded: 8.0,
),
),
),
);
}
}
void main() => runApp(MyApp());
参数
参数名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
onFinish |
Function | 必填 | 当用户点击“确定”或达到输入长度时触发的回调函数,接收输入的PIN。 |
totalInput |
int | 6 | 所需的输入PIN长度。 |
backgroundColor |
Color | Colors.white | 输入面板容器的背景颜色。 |
actionButtonCollor |
Color | Colors.indigo | 确定和退格按钮的颜色。 |
filledColor |
Color | Colors.indigo | 已输入数字的填充颜色。 |
title |
String | “Masukkan PIN kamu” | 显示在输入面板上方的标题文字。 |
titleColor |
Color | Colors.black | 标题文字的颜色。 |
numberColor |
Color | Colors.black | 按键上的数字文字颜色。 |
buttonBorderColor |
Color | Colors.grey | 每个按键边框的颜色。 |
inputButtonRounded |
double | 4.0 | 按键的圆角半径。 |
示例
以下是如何创建默认设置的 RandomInputPad
:
RandomInputPad(
onFinish: (input) {
print("输入的PIN: $input");
},
)
以下是创建自定义 RandomInputPad
的示例:
RandomInputPad(
onFinish: (input) {
print("PIN已完整输入: $input");
},
totalInput: 4,
backgroundColor: Colors.blue.shade50,
actionButtonCollor: Colors.redAccent,
filledColor: Colors.green,
title: "安全PIN输入",
titleColor: Colors.blueGrey,
numberColor: Colors.black,
buttonBorderColor: Colors.blueGrey,
inputButtonRounded: 10.0,
)
示例代码
以下是一个完整的示例代码,展示如何使用 RandomInputPad
:
import 'package:flutter/material.dart';
import 'package:random_input_pad/random_input_pad.dart';
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: Body(),
);
}
}
class Body extends StatelessWidget {
const Body({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: ElevatedButton(
onPressed: () async {
String? pin = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => const InputPin()),
);
if (pin != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Center(child: Text("PIN : $pin")),
backgroundColor: Colors.blue.shade500,
showCloseIcon: true,
),
);
}
},
child: const Text("输入PIN"),
),
),
),
);
}
}
class InputPin extends StatelessWidget {
const InputPin({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: null,
),
body: SafeArea(
child: RandomInputPad(
onFinish: (input) {
Navigator.pop(context, input);
},
totalInput: 6,
backgroundColor: Colors.white,
filledColor: Colors.indigo,
actionButtonCollor: Colors.indigo,
titleColor: Colors.black,
title: "请输入您的PIN",
inputButtonRounded: 4.0,
numberColor: Colors.black,
buttonBorderColor: Colors.grey,
),
),
);
}
}
更多关于Flutter随机输入面板插件random_input_pad的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter随机输入面板插件random_input_pad的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
random_input_pad
是一个用于 Flutter 的随机输入面板插件,它可以生成一个随机的输入面板,通常用于验证码输入、随机密码生成等场景。以下是如何在 Flutter 项目中使用 random_input_pad
插件的步骤。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 random_input_pad
插件的依赖。
dependencies:
flutter:
sdk: flutter
random_input_pad: ^0.1.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入包
在你的 Dart 文件中导入 random_input_pad
包。
import 'package:random_input_pad/random_input_pad.dart';
3. 使用 RandomInputPad
你可以在你的 Flutter 应用中使用 RandomInputPad
小部件。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:random_input_pad/random_input_pad.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Random Input Pad Example'),
),
body: Center(
child: RandomInputPad(
onSubmitted: (String value) {
print('Submitted value: $value');
},
),
),
),
);
}
}
4. 自定义 RandomInputPad
RandomInputPad
提供了一些可选的参数,允许你自定义输入面板的外观和行为。以下是一些常用的参数:
length
: 输入的长度(默认值为 4)。buttonSize
: 按钮的大小(默认值为 60.0)。buttonColor
: 按钮的背景颜色(默认值为Colors.blue
)。textColor
: 按钮上文本的颜色(默认值为Colors.white
)。onSubmitted
: 当用户完成输入时的回调函数。
RandomInputPad(
length: 6,
buttonSize: 70.0,
buttonColor: Colors.green,
textColor: Colors.black,
onSubmitted: (String value) {
print('Submitted value: $value');
},
)
5. 处理用户输入
在 onSubmitted
回调中,你可以处理用户输入的值。例如,你可以验证输入的验证码是否正确,或者将输入的值保存到数据库中。
RandomInputPad(
onSubmitted: (String value) {
if (value == '1234') {
print('Correct code!');
} else {
print('Incorrect code!');
}
},
)
6. 运行应用
现在你可以运行你的 Flutter 应用,并查看 RandomInputPad
的效果。
flutter run