Home > Back-end >  Flutter readonly Form widget
Flutter readonly Form widget

Time:09-17

How can I set a Form widget readonly in flutter. Setting the Form widget readonly means that all the widgets inside the Form widget will automatically become readonly. Is there something built-in ?

CodePudding user response:

You can add it in StatelessWidget and make it readonly, than you can call it any where, Like this:

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,
      ),
    );
  }
}

CodePudding user response:

You can either use the native flutter elements of type readOnly for the TextFormField widget or set the onPressed of a button to null.

You can also use the IgnorePointer widget to ignore the user's touch on a widget.

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"),
            ),
          )
        ]
      ),
    );
  • Related