如果用户想要更新任何特定数字,则在指定字段中键入OTP后,如果单击任何特定框PinCodeTextField,则框不可编辑。
这是我的代码片段。
PinCodeTextField(
autovalidateMode: _autoValidateMode,
focusNode: _otpNode,
appContext: context,
controller: _otpController,
keyboardType: TextInputType.number,
animationType: AnimationType.scale,
animationDuration: Duration(milliseconds: 300),
length: 6,
enableActiveFill: true,
pinTheme: PinTheme(
shape: PinCodeFieldShape.box,
borderRadius: BorderRadius.circular(2),
fieldHeight: 40,
fieldWidth: 40,
activeFillColor: Color(0xFFFeaedf0),
activeColor: Color(0xFFFeaedf0),
inactiveFillColor: Color(0xFFFeaedf0),
inactiveColor: Color(0xFFFeaedf0),
selectedColor: Color(0xFFFeaedf0),
selectedFillColor: Color(0xFFFeaedf0),
),
onChanged: (pin) {
_pin = pin;
},
validator: (v) {
if (v.toString().isEmpty || v!.length != 6) {
return "Please enter valid pincode".tr;
}
return null;
},
),
发布于 2021-11-19 15:00:30
有2种自定义的方法来做它,无论你喜欢你可以使用的代码。
方法1-最适合我认为
import 'package:flutter/material.dart';
class OtpForm extends StatefulWidget {
const OtpForm({Key? key}) : super(key: key);
@override
_OtpFormState createState() => _OtpFormState();
}
class _OtpFormState extends State<OtpForm> {
late FocusNode pin2FocusNode;
late FocusNode pin3FocusNode;
late FocusNode pin4FocusNode;
@override
void initState() {
super.initState();
pin2FocusNode = FocusNode();
pin3FocusNode = FocusNode();
pin4FocusNode = FocusNode();
}
@override
void dispose() {
super.dispose();
pin2FocusNode.dispose();
pin3FocusNode.dispose();
pin4FocusNode.dispose();
}
void nextField(String value, FocusNode focusNode) {
if (value.length == 1) {
focusNode.requestFocus();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Form(
child: Column(
children: [
const SizedBox(height: 50),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
SizedBox(
width: MediaQuery.of(context).size.width * 0.1,
child: TextFormField(
obscureText: true,
style: const TextStyle(fontSize: 24),
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
onChanged: (value) {
nextField(value, pin2FocusNode);
},
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.1,
child: TextFormField(
focusNode: pin2FocusNode,
obscureText: true,
style: const TextStyle(fontSize: 24),
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
onChanged: (value) => nextField(value, pin3FocusNode),
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.1,
child: TextFormField(
focusNode: pin3FocusNode,
style: const TextStyle(fontSize: 24),
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
onChanged: (value) => nextField(value, pin4FocusNode),
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.1,
child: TextFormField(
focusNode: pin4FocusNode,
style: const TextStyle(fontSize: 24),
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
onChanged: (value) {
if (value.length == 1) {
pin4FocusNode.unfocus();
// Then you need to check is the code is correct or not
}
},
),
),
],
),
],
),
),
),
);
}
}
方法2-与方法1相比不太适合,但做的工作
import 'package:flutter/material.dart';
class EnterOTP extends StatelessWidget {
const EnterOTP({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: const [
OTPDigitTextFieldBox(first: true, last: false),
OTPDigitTextFieldBox(first: false, last: false),
OTPDigitTextFieldBox(first: false, last: false),
OTPDigitTextFieldBox(first: false, last: true),
],
)
]),
),
);
}
}
class OTPDigitTextFieldBox extends StatelessWidget {
final bool first;
final bool last;
const OTPDigitTextFieldBox(
{Key? key, required this.first, required this.last})
: super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
height: 85,
child: AspectRatio(
aspectRatio: 1.0,
child: TextField(
autofocus: true,
onChanged: (value) {
if (value.length == 1 && last == false) {
FocusScope.of(context).nextFocus();
}
if (value.isEmpty && first == false) {
FocusScope.of(context).previousFocus();
}
},
showCursor: false,
readOnly: false,
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
maxLength: 1,
decoration: InputDecoration(
// contentPadding: EdgeInsets.all(0),
counter: const Offstage(),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
width: 2,
color: Colors.black,
),
borderRadius: BorderRadius.circular(10)),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(width: 1, color: Colors.black),
borderRadius: BorderRadius.circular(10)),
hintText: "",
),
),
),
);
}
}
你可以选择最适合你的。
https://stackoverflow.com/questions/70035118
复制相似问题