Get Text of a item of List in flutter


Get Text of a item of List in flutter



Step 1. Need a list view builder (ListView.builder)

Step 2. Row Item (itemBuilder)

Full example will be like this-

class NetworkDiagnosis extends StatefulWidget {
  @override
  _NetworkDiagnosisState createState() => _NetworkDiagnosisState();
}

class _NetworkDiagnosisState extends State<NetworkDiagnosis> {
  List<String> list= List();

  @override
  void initState() {
    super.initState();
    list.add('a');
    list.add('b');
    list.add('c');
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: AppTheme.appBarColor,
        title: const Text('Title'),
      ),
      body: ListView.builder(
        itemBuilder: (context, index) => RowItem( list[index]),
        itemCount: list.length,
      ),
    );
  }
}




class RowItem extends StatelessWidget {
  final String data;

  RowItem(this.data);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        print(data);
      },
      child: Text(data),
    );
  }
}



Previous
Next Post »