Home > Enterprise >  Pass data and receive on textfield widget in Flutter
Pass data and receive on textfield widget in Flutter

Time:12-21

How to pass a data from first screen to second screen but should receive on textfield widget in second screen. I have tried but there is no option in textfield widget, how to do this?

TextField(
                                keyboardType: TextInputType.text,
                                decoration: InputDecoration(
                                    fillColor: Colors.white,
                                    hintText: "",
                                    filled: true,
                                    labelText: "First name",
                                    enabledBorder: OutlineInputBorder(
                                      borderSide: BorderSide(
                                          color: Colors.white),
                                      borderRadius: BorderRadius.circular(6),
                                    ),
                                    focusedBorder: OutlineInputBorder(
                                      borderSide: BorderSide(
                                          color: Colors.white),
                                      borderRadius: BorderRadius.circular(6),
                                    )),
                              ),

CodePudding user response:

To set default text in your TextField you need to use TextEditingController, you can use it by initializing in initState or directly to the TextField, here are the examples

To initialize with initState

  late TextEditingController _controller;
  
  @override
  void initState() {
    _controller.text = widget.text;
    super.initState();
  }

To pass value directly

TextField(
            controller: TextEditingController(text: widget.text),
            keyboardType: TextInputType.text,
            decoration: InputDecoration(
                fillColor: Colors.white,
                hintText: "",
                filled: true,
                labelText: "First name",
                enabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.white),
                  borderRadius: BorderRadius.circular(6),
                ),
                focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.white),
                  borderRadius: BorderRadius.circular(6),
                )),
          )
  • Related