vontdeux
vontdeux

Reputation: 107

Flutter: how to prompt user for input using showDialog after tapping on ListTile, then using the input

Assuming i have the codes below,

 Widget _myListView(BuildContext context) {
  return ListView(
    children: <Widget>[
      ListTile(
        title: Text('Sun'),
        onTap:() async{
           await showDialog<String>(
                    context:context,
                    --- not sure how to proceed from here --
                 }
        }
           Navigator.pop(context); //this code makes sure it go back to another page after done with the input processing.
      ),
      ListTile(
        title: Text('Moon'),
      ),
      ListTile(
        title: Text('Star'),
      ),
    ],
  );
}

how to use the input onTap ListTile, run some method to use the input, and pop back to previous page.

Upvotes: 1

Views: 913

Answers (1)

Cesar Devesa
Cesar Devesa

Reputation: 1297

Call function _showMyDialog(); and see data in the console:

  • I used Row() to work in landscape.

    final myController = TextEditingController();
    late String userData;
    
    Future<void> _showMyDialog() async {
    
      return showDialog<void>(
        context: context,
        barrierDismissible: false, // user must tap button!
        builder: (BuildContext context) {
          return AlertDialog(
            title: Row(
              children: [
                Expanded(
                  child: TextField(
                    controller: myController,
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Enter any data',
                      hintText: 'Enter any data',
                    ),
                  ),
                ),
                Expanded(
                  child: TextButton(
                    child: const Text('OK'),
                    onPressed: () {
                      userData = myController.text;
                      myController.text = '';
                      FocusManager.instance.primaryFocus?.unfocus();
                      print(userData);
                      Navigator.of(context).pop();
                      },
                    ),
                )
              ]
            )
    
          );
        },
      );
    }
    
    @override
    void dispose() {
      myController.dispose();
      super.dispose();
    }
    

Upvotes: 1

Related Questions