flutter - 如何在 FLUTTER 中使用信用卡日期格式 mm/yy
问题描述
child: new TextFormField(
maxLength:5,
controller:
TextEditingController(text: donation.date),
onChanged: (value) {
donation.date = value;
},
decoration: InputDecoration(
suffixText: '',
suffixStyle: TextStyle(
color: Color(0xffde0486),
),
labelText: 'Expiry Date',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.number,
textInputAction: TextInputAction.done,
style: TextStyle(color: Colors.white),
autofocus: true,
),
我有这段代码用于我在Flutter中实现的信用卡。我想将 mm/yy 格式放在文本字段上。当用户输入月份时,文本字段应自动显示“/”。我该怎么做呢?
解决方案
您可以使用该onChange
方法实现您的结果。您可以检查输入的长度,如果它是 2,则在文本中插入一个“/”,即dd
- 您需要将控制器存储在单独的变量中,因为我们想稍后使用它来插入“/”:
var _controller = new TextEditingController(text: donation.date);
- 将此
_controller
和onChange
回调添加到您的TextFormField
:
TextFormField(
controller: _controller, //<-- Add controller here
onChanged: (value) {
if(value.length == 2) _controller.text += "/"; //<-- Automatically show a '/' after dd
donation.date = value;
},
...
),
推荐阅读
- java - Spring @value 属性:无法将“java.util.Collections$UnmodifiableMap”类型的值转换为所需的“java.util.Map”类型
- android - 在 Android 11 上的状态栏下扩展视图
- ios - M1“命令 ValidateEmbeddedBinary 失败,退出代码非零”Xcode 中的存档错误
- javascript - 如何使用vue动态添加新消息,当新消息产生时,窗口可以滚动到最新消息位置?
- powershell - 使用 PSCustomObject 在单独的列中列出多个文件夹然后选择一些?
- reactjs - React 表行删除不会删除 TextInput
- python-3.x - 特威利奥。Python。分析通话状态
- python - AttributeError: Caught AttributeError in DataLoader worker process 0. - 微调预训练的 Transformer 模型
- azure - 使用其他订阅时无法在 Azure 上部署基础设施
- java - 如何以有效的方式基于键与对象的 Arraylist 进行比较