Home > database >  How to make TextFormField enabled by a button?
How to make TextFormField enabled by a button?

Time:07-30

I want to make TextFormField enabled by press a button which is not in the same level,how to pass the value from the button?

Here's UI

return Scaffold(
 child:Column(
  children:[
    Container(
     child: Row(
      children[
      Textbutton(),
       .......//otherButtons
       ]
      )
    ),
     DefaultTabController(
      child: Column(
       children[
        TabBar(),
        TabBarView(
         child:Column(
          children[
            TextFormField(),
            .......//otherTextFormFields   
          ]
         )
        )
       ]
      )
     )
  ]
 )
);

CodePudding user response:

You can set text through TextEditingController. and to disable user input, use readOnly:true. You can test the widget and get the concept how it works.

class ColorTest extends StatefulWidget {
  ColorTest({Key? key}) : super(key: key);

  @override
  State<ColorTest> createState() => _ColorTestState();
}

class _ColorTestState extends State<ColorTest> {
  TextEditingController controller = TextEditingController();

  bool readOnly = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: Column(
        children: [
          TextButton(
            onPressed: () {
              controller.text = "Got the text from button click";
              setState(() {});
            },
            child: Text("set text"),
          ),
          TextFormField(
            controller: controller,
            readOnly: readOnly,
          ),
          TextButton(
            onPressed: () {
              readOnly = !readOnly;

              setState(() {});
            },
            child: Text("ReadOnlyMode: $readOnly"),
          ),
        ],
      )

          // materialButto(),
          ),
    );
  }
}
  • Related