【发布时间】:2023-04-05 18:06:02
【问题描述】:
在我的 OTP 文本字段中,单个元素或文本字段的底部边框的焦点颜色默认为蓝色。怎么改成橙色??另外,我怎样才能将该边框的未聚焦颜色从黑色更改为白色。即默认不输入任何数字,所有块或底边框的颜色为白色???我将突出显示图像中的属性。
如果您没有正确理解我想说的话,请查看这张图片 - MobileImage
这是代码 -
import 'package:flutter/material.dart';
import 'package:otp_text_field/style.dart';
class OTPTextField extends StatefulWidget {
/// Number of the OTP Fields
final int length;
/// Total Width of the OTP Text Field
final double width;
/// Width of the single OTP Field
final double fieldWidth;
/// Manage the type of keyboard that shows up
TextInputType keyboardType;
/// The style to use for the text being edited.
final TextStyle style;
/// Text Field Alignment
/// default: MainAxisAlignment.spaceBetween [MainAxisAlignment]
final MainAxisAlignment textFieldAlignment;
/// Obscure Text if data is sensitive
final bool obscureText;
/// Text Field Style for field shape.
/// default FieldStyle.underline [FieldStyle]
final FieldStyle fieldStyle;
/// Callback function, called when a change is detected to the pin.
final ValueChanged<String> onChanged;
/// Callback function, called when pin is completed.
final ValueChanged<String> onCompleted;
OTPTextField(
{Key key,
this.length = 4,
this.width = 20,
this.fieldWidth = 50,
this.keyboardType = TextInputType.number,
this.style = const TextStyle(),
this.textFieldAlignment = MainAxisAlignment.spaceBetween,
this.obscureText = false,
this.fieldStyle = FieldStyle.underline,
this.onChanged,
this.onCompleted})
: assert(length > 1);
@override
_OTPTextFieldState createState() => _OTPTextFieldState();
}
class _OTPTextFieldState extends State<OTPTextField> {
List<FocusNode> _focusNodes;
List<TextEditingController> _textControllers;
List<Widget> _textFields;
List<String> _pin;
@override
void initState() {
super.initState();
_focusNodes = List<FocusNode>(widget.length);
_textControllers = List<TextEditingController>(widget.length);
_pin = List.generate(widget.length, (int i) {
return '';
});
_textFields = List.generate(widget.length, (int i) {
return buildTextField(context, i);
});
}
@override
void dispose() {
_textControllers
.forEach((TextEditingController controller) => controller.dispose());
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
width: widget.width,
child: Row(
mainAxisAlignment: widget.textFieldAlignment,
crossAxisAlignment: CrossAxisAlignment.center,
children: _textFields,
),
);
}
/// This function Build and returns individual TextField item.
///
/// * Requires a build context
/// * Requires Int position of the field
Widget buildTextField(BuildContext context, int i) {
if (_focusNodes[i] == null) _focusNodes[i] = new FocusNode();
if (_textControllers[i] == null)
_textControllers[i] = new TextEditingController();
return Container(
width: widget.fieldWidth,
child: TextField(
cursorColor: Colors.white,
controller: _textControllers[i],
keyboardType: widget.keyboardType,
textAlign: TextAlign.center,
maxLength: 1,
style: widget.style,
focusNode: _focusNodes[i],
obscureText: true,
decoration: InputDecoration(
fillColor: Colors.white,
counterText: "",
border: widget.fieldStyle == FieldStyle.box
? OutlineInputBorder(borderSide: BorderSide(width: 4.0, color: Colors.green))
: null),
onChanged: (String str) {
// Check if the current value at this position is empty
// If it is move focus to previous text field.
if (str.isEmpty) {
if (i == 0) return;
_focusNodes[i].unfocus();
_focusNodes[i - 1].requestFocus();
}
// Update the current pin
setState(() {
_pin[i] = str;
});
// Remove focus
if (str.isNotEmpty) _focusNodes[i].unfocus();
// Set focus to the next field if available
if (i + 1 != widget.length && str.isNotEmpty)
FocusScope.of(context).requestFocus(_focusNodes[i + 1]);
String currentPin = "";
_pin.forEach((String value) {
currentPin += value;
});
// if there are no null values that means otp is completed
// Call the `onCompleted` callback function provided
if (!_pin.contains(null) &&
!_pin.contains('') &&
currentPin.length == widget.length) {
widget.onCompleted(currentPin);
}
// Call the `onChanged` callback function
widget.onChanged(currentPin);
},
),
);
}
}
【问题讨论】: