在 flutter 中将所有文本字段的格式设置为相同
我正在努力学习颤振。我有以下 TextField 设置,因为我想要它,我可以复制并粘贴应用程序中每个 TextField 的所有设置/属性,但是有没有更好的方法使所有 TextField 具有相同的设置/属性?
Flexible(
child: Padding(
padding: EdgeInsets.all(20.0),
child: TextField(
maxLength: 5,
maxLengthEnforcement: MaxLengthEnforcement.enforced,
keyboardType: TextInputType.numberWithOptions(decimal: true, signed: false, ),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r"[0-9.]"))],
//keyboardType: TextInputType.number,
decoration: InputDecoration(
labelStyle: TextStyle(color: Colors.black,
fontStyle: FontStyle.normal, fontSize: 20, fontWeight: FontWeight.bold ),
floatingLabelAlignment: FloatingLabelAlignment.center,
//border: OutlineInputBorder(),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(45),
),
floatingLabelBehavior: FloatingLabelBehavior.always,
labelText: 'Empty Weight',
filled: true,
fillColor: Colors.grey[350],
),
),
),
),
I'm trying to learn Flutter. I have the below TextField setup as I would like it, I could copy and paste all the settings/properties for each TextField in the app, however is there a better way to make all of the TextFields have the same setting/properties?
Flexible(
child: Padding(
padding: EdgeInsets.all(20.0),
child: TextField(
maxLength: 5,
maxLengthEnforcement: MaxLengthEnforcement.enforced,
keyboardType: TextInputType.numberWithOptions(decimal: true, signed: false, ),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r"[0-9.]"))],
//keyboardType: TextInputType.number,
decoration: InputDecoration(
labelStyle: TextStyle(color: Colors.black,
fontStyle: FontStyle.normal, fontSize: 20, fontWeight: FontWeight.bold ),
floatingLabelAlignment: FloatingLabelAlignment.center,
//border: OutlineInputBorder(),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(45),
),
floatingLabelBehavior: FloatingLabelBehavior.always,
labelText: 'Empty Weight',
filled: true,
fillColor: Colors.grey[350],
),
),
),
),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,基本上,这是 Flutter 背后的主要思想 - 您正在构建可重用的组件(小部件),然后使用它们来构建您的应用程序。
确定应更改
TextField
的哪些属性。例如,让我们考虑(根据您的示例)可能是labelText
和maxLength
。创建一个包装
TextField
的自定义 Widget,并将提取的属性定义为 Widget 属性,并将它们用作TextField
的变量:CustomTextField
代码:Of course and basically, this is the main idea behind Flutter - you are building reusable components (widgets) and later use them to build your app.
Identify what properties of your
TextField
should change. E.g. let's consider (from your example) that could belabelText
andmaxLength
.Create a custom Widget that wraps the
TextField
and defines the extracted properties as Widget properties and use them as variables for theTextField
:CustomTextField
in the code: