Mohammed Nabil
Mohammed Nabil

Reputation: 802

Pass data and receive on textfield widget in Flutter

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

Upvotes: 0

Views: 585

Answers (1)

Diwyansh
Diwyansh

Reputation: 3514

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

Upvotes: 1

Related Questions