Flutter一次性密码输入插件fancy_otp_text_fields的使用
Flutter一次性密码输入插件fancy_otp_text_fields的使用
一个高度可定制的一次性密码输入框。
屏幕截图
成功模式 | 普通模式 | 可定制化 | 错误模式 |
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
加载模式 | 可定制化加载模式 | 普通使用 |
---|---|---|
![]() |
![]() |
![]() |
代码片段
简单使用
import 'package:flutter/material.dart';
import 'package:fancy_otp_text_field/fancy_otp_text_field.dart';
class SimpleUse extends StatefulWidget {
const SimpleUse({Key? key}) : super(key: key);
@override
_SimpleUseState createState() => _SimpleUseState();
}
class _SimpleUseState extends State<SimpleUse> {
OtpTextFieldsController otpTextFieldsController = OtpTextFieldsController();
void changeMode(OtpState otpState) {
// 改变模式以改变设计
// 模式 => {success, loading, error, normal}
otpTextFieldsController.currentState.value = otpState;
}
void changeOtpValues(String otpValue) {
// 如果你有自动填充短信功能,你应该使用这个
otpTextFieldsController.otpValue.value = otpValue;
}
@override
Widget build(BuildContext context) {
return OtpTextFields(
otpLength: 5,
otpTextFieldsController: otpTextFieldsController,
defaultTextStyle: const TextStyle(fontSize: 18, color: Colors.black),
);
}
}
使用所有可定制参数
import 'package:flutter/material.dart';
import 'package:fancy_otp_text_field/fancy_otp_text_field.dart';
class CustomUse extends StatefulWidget {
const CustomUse({Key? key}) : super(key: key);
@override
_CustomUseState createState() => _CustomUseState();
}
class _CustomUseState extends State<CustomUse> {
OtpTextFieldsController otpTextFieldsController = OtpTextFieldsController();
void changeMode(OtpState otpState) {
// 改变模式以改变设计
// 模式 => {success, loading, error, normal}
otpTextFieldsController.currentState.value = otpState;
}
void changeOtpValues(String otpValue) {
// 如果你有自动填充短信功能,你应该使用这个
otpTextFieldsController.otpValue.value = otpValue;
}
@override
Widget build(BuildContext context) {
return OtpTextFields(
otpLength: 5,
otpTextFieldsController: otpTextFieldsController,
defaultTextStyle: const TextStyle(fontSize: 18, color: Colors.black),
textStyleLoading: const TextStyle(fontSize: 18, color: Colors.blue),
// 文本样式当模式为加载时
textStyleSuccess: const TextStyle(fontSize: 18, color: Colors.green),
// 文本样式当模式为成功时
textStyleError: const TextStyle(fontSize: 18, color: Colors.red),
// 文本样式当模式为错误时
textStyleActive: const TextStyle(fontSize: 18, color: Colors.black),
// 文本样式当模式为正常且焦点在文本框内时
textStyleFilled: const TextStyle(fontSize: 18, color: Colors.grey),
// 文本样式当模式为正常且焦点不在文本框内时
decorationSuccessBox: BoxDecoration(
color: Colors.green,
shape: BoxShape.rectangle,
border: Border.all(color: Colors.black, width: 3),
borderRadius: BorderRadius.all(Radius.circular(5)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(3, 3),
)
],
),
// 成功模式下的容器装饰
decorationErrorBox: BoxDecoration(
color: Colors.red,
shape: BoxShape.rectangle,
border: Border.all(color: Colors.black, width: 3),
borderRadius: BorderRadius.all(Radius.circular(5)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(3, 3),
)
],
),
// 错误模式下的容器装饰
decorationLoadingBox: BoxDecoration(
color: Colors.blue,
shape: BoxShape.rectangle,
border: Border.all(color: Colors.black, width: 3),
borderRadius: BorderRadius.all(Radius.circular(5)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(3, 3),
)
],
),
// 加载模式下的容器装饰
decorationActiveBox: BoxDecoration(
color: Colors.grey,
shape: BoxShape.rectangle,
border: Border.all(color: Colors.black, width: 3),
borderRadius: BorderRadius.all(Radius.circular(5)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(3, 3),
)
],
),
// 正常模式且焦点不在文本框内的容器装饰
decorationFilledBox: BoxDecoration(
color: Colors.black,
shape: BoxShape.rectangle,
border: Border.all(color: Colors.black, width: 3),
borderRadius: BorderRadius.all(Radius.circular(5)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(3, 3),
)
],
),
// 正常模式且文本已填写的容器装饰
decorationEmptyBox: BoxDecoration(
color: Colors.white,
shape: BoxShape.rectangle,
border: Border.all(color: Colors.black, width: 3),
borderRadius: BorderRadius.all(Radius.circular(5)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(3, 3),
)
],
), // 正常模式且文本为空的容器装饰
);
}
}
当你用代码填写OTP时,将显示动画:
otpTextFieldsController.otpValue.value = otpValue;
更多关于Flutter一次性密码输入插件fancy_otp_text_fields的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复