Get Selected Item From Listview In Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 10-10-2020

ListView widget has a property named as onTap() which would call each time when app user clicks on ListTile item. We can easily call any function or specific task on onTap event. On the onTap() method we would pass the selected item to stand alone function where we would print the clicked item on mobile screen using Alert dialog message box.

Get Selected Item From Listview In Flutter Android App

Get Selected Item From ListView
Complete Code For Get Selected Item From ListView In Flutter 
main.dart

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        home: Scaffold(
            appBar: AppBar(title: Text('Get ListView Selected Item')),
            body: Center(
                child: ListViewWidget()
            )
        )
    );
  }
}

class ListViewWidget extends StatefulWidget {
  ListViewState createState() => ListViewState();
}
class ListViewState extends State {
  List<String> items = [
    'Apple',
    'Banana',
    'Mango',
    'Orange',
  ];

  getListViewItems(String item){
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: new Text(item),
          actions: <Widget>[
            FlatButton(
              child: new Text("OK"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
          child: ListView(
            children: items
                .map((data) => ListTile(
                title: Text(data),
                onTap: ()=>{
                  getListViewItems(data)
                }
            ))
                .toList(),
          ),
        )
    );
  }
}

Related Post