Home > OS >  Show selected text from another page in textFormField
Show selected text from another page in textFormField

Time:09-08

final TextEditingController _musteri = TextEditingController();

TextFormField(
                  controller: _musteri,
                  onTap: () => Navigator.push(
                    context,
                    MaterialPageRoute<void>(
                      builder: (BuildContext context) => const MusteriList(),
                    ),
                  ),
                  textAlign: TextAlign.center,
                  decoration: InputDecoration(
                    labelText: 'Müşteri',
                    labelStyle: TextStyle(
                        fontFamily: 'Montserrat',
                        color: Color.fromARGB(255, 139, 158, 131)),
                    prefixIcon: Icon(
                      Icons.person_outline,
                      color: Color.fromARGB(255, 180, 147, 133),
                    ),
                  ),
                ),

This is the TextFormField I created.

    List<Map<String, dynamic>> _foundUsers = [];
  @override
  initState() {
    _foundUsers = _allUsers;
    super.initState();
  }

ListView.builder(
            itemCount: _foundUsers.length,
            itemBuilder: (context, index) => GestureDetector(
              onTap: () => Navigator.push(
                context,
                MaterialPageRoute<void>(
                  builder: (BuildContext context) => const SatisIcmali(),
                ),
              ),
              child: Card(
                key: ValueKey(_foundUsers[index]["id"]),
                color: Colors.blueAccent,
                elevation: 4,
                margin: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                child: ListTile(
                  leading: Text(
                    _foundUsers[index]["id"].toString(),
                    style: TextStyle(fontSize: 24, color: Colors.white),
                  ),
                  title: Text(
                    _foundUsers[index]["name"],
                    style: TextStyle(fontSize: 14, color: Colors.white),
                  ),
                ),
              ),
            ),
          ),

This is a list that is shown to the user when the TextFormField is clicked.

I want to show the texts I defined for ["name"] in the list I created, in the textFormField in the first code. Clicking on the card will go to the previous page and the ["name"] data in the clicked card will be displayed there.

CodePudding user response:

Pass foundUsers[index]["name"] to the navigation and you can get that on the TextFormField page and set it on that page.

Navigator.push(
                context,
                MaterialPageRoute<void>(
                  builder: (BuildContext context) => const SatisIcmali(foundUsers[index]["name"]),
                ),
              )

Check this for reference https://docs.flutter.dev/cookbook/navigation/passing-data

CodePudding user response:

You can use a Context Provider as RiverPod or GetX that you can access from different pages.

Otherwhise you have to fill _foundUsers with the newValue of TextFormField onChanged(newValue)=> setState(()=> _yourProp = newValue).

Hope for the best!

  • Related