简体   繁体   English

Flutter 只读表单小部件

[英]Flutter readonly Form widget

How can I set a Form widget readonly in flutter.如何在 flutter 中将表单小部件设置为只读。 Setting the Form widget readonly means that all the widgets inside the Form widget will automatically become readonly.将 Form 小部件设置为只读意味着 Form 小部件内的所有小部件将自动变为只读。 Is there something built-in?有内置的东西吗?

You can add it in StatelessWidget and make it readonly, than you can call it any where, Like this:您可以将它添加到 StatelessWidget 并使其成为只读,而不是您可以在任何地方调用它,如下所示:

class Input extends StatelessWidget {
  const Input({Key? key, required this.controller, required this.hint}) : super(key: key);
final TextEditingController controller;
final String hint;
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      readOnly: true,
      controller:controller ,
      decoration: InputDecoration(
        hintText: hint,
      ),
    );
  }
}

You can either use the native flutter elements of type readOnly for the TextFormField widget or set the onPressed of a button to null.您可以为 TextFormField 小部件使用 readOnly 类型的原生 flutter 元素,或者将按钮的 onPressed 设置为 null。

You can also use the IgnorePointer widget to ignore the user's touch on a widget.您还可以使用 IgnorePointer 小部件来忽略用户对小部件的触摸。

return Form(
      child: ListView(
        children: [
          TextFormField(  
            readOnly: readOnly,
            decoration: InputDecoration(
              labelText: "Quest 1",
            ),
          ),
          IgnorePointer(
            ignoring: readOnly,
            child: TextFormField(  
              decoration: InputDecoration(
                labelText: "Quest 2",
              ),
            ),
          ),
          ElevatedButton(  
            onPressed: null,
            child: Text("Confirm"),
          ),
          IgnorePointer(
            ignoring: readOnly,
            child: ElevatedButton(  
              onPressed: (){
                debugPrint("confirm");
              },
              child: Text("Confirm"),
            ),
          )
        ]
      ),
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM