在 flutter 中将所有文本字段的格式设置为相同

发布于 2025-01-12 12:29:13 字数 1216 浏览 4 评论 0原文

我正在努力学习颤振。我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-01-19 12:29:13

当然,基本上,这是 Flutter 背后的主要思想 - 您正在构建可重用的组件(小部件),然后使用它们来构建您的应用程序。

  1. 确定应更改 TextField 的哪些属性。例如,让我们考虑(根据您的示例)可能是 labelTextmaxLength

  2. 创建一个包装 TextField 的自定义 Widget,并将提取的属性定义为 Widget 属性,并将它们用作 TextField 的变量:

class CustomTextField extends StatelessWidget {
  final String labelText;
  final int maxLength;

  const CustomTextField({
    required this.labelText,
    required this.maxLength,
  });

  @override
  Widget build(BuildContext context) {
    return TextField(
      maxLength: maxLength,
      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: labelText,
        filled: true,
        fillColor: Colors.grey[350],
      ),
    );
  }
}
  1. 在以下位置使用您的 CustomTextField代码:
Flexible(
  child: Padding(
    padding: EdgeInsets.all(20.0),
    child: CustomTextField(
      maxLength: 5,
      labelText: 'Empty Weight',
    ),
  ),
);

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.

  1. Identify what properties of your TextField should change. E.g. let's consider (from your example) that could be labelText and maxLength.

  2. Create a custom Widget that wraps the TextField and defines the extracted properties as Widget properties and use them as variables for the TextField:

class CustomTextField extends StatelessWidget {
  final String labelText;
  final int maxLength;

  const CustomTextField({
    required this.labelText,
    required this.maxLength,
  });

  @override
  Widget build(BuildContext context) {
    return TextField(
      maxLength: maxLength,
      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: labelText,
        filled: true,
        fillColor: Colors.grey[350],
      ),
    );
  }
}
  1. User your CustomTextField in the code:
Flexible(
  child: Padding(
    padding: EdgeInsets.all(20.0),
    child: CustomTextField(
      maxLength: 5,
      labelText: 'Empty Weight',
    ),
  ),
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文