list view in flutter code example

Example 1: list widget flutter

ListView(
  padding: const EdgeInsets.all(8),
  children: [
    Container(
      height: 50,
      color: Colors.amber[600],
      child: const Center(child: Text('Entry A')),
    ),
    Container(
      height: 50,
      color: Colors.amber[500],
      child: const Center(child: Text('Entry B')),
    ),
    Container(
      height: 50,
      color: Colors.amber[100],
      child: const Center(child: Text('Entry C')),
    ),
  ],
)

Example 2: search in flutter listview

import 'package:flutter/material.dart';void main() => runApp(new MyApp());class MyApp extends StatelessWidget {  // This widget is the root of your application.  @override  Widget build(BuildContext context) {    return new MaterialApp(      title: 'Flutter Demo',      theme: new ThemeData(        primarySwatch: Colors.blue,      ),      home: new MyHomePage(title: 'ListView with Search),    );  }}class MyHomePage extends StatefulWidget {  MyHomePage({Key key, this.title}) : super(key: key);  final String title;  @override  _MyHomePageState createState() => new _MyHomePageState();}class _MyHomePageState extends State {  TextEditingController editingController = TextEditingController();  final duplicateItems = List.generate(10000, (i) => "Item $i");  var items = List();  @override  void initState() {    items.addAll(duplicateItems);    super.initState();  }  void filterSearchResults(String query) {    List dummySearchList = List();    dummySearchList.addAll(duplicateItems);    if(query.isNotEmpty) {      List dummyListData = List();      dummySearchList.forEach((item) {        if(item.contains(query)) {          dummyListData.add(item);        }      });      setState(() {        items.clear();        items.addAll(dummyListData);      });      return;    } else {      setState(() {        items.clear();        items.addAll(duplicateItems);      });    }  }  @override  Widget build(BuildContext context) {    return new Scaffold(      appBar: new AppBar(        title: new Text(widget.title),      ),      body: Container(        child: Column(          children: [            Padding(              padding: const EdgeInsets.all(8.0),              child: TextField(                onChanged: (value) {                  filterSearchResults(value);                },                controller: editingController,                decoration: InputDecoration(                    labelText: "Search",                    hintText: "Search",                    prefixIcon: Icon(Icons.search),                    border: OutlineInputBorder(                        borderRadius: BorderRadius.all(Radius.circular(25.0)))),              ),            ),            Expanded(              child: ListView.builder(                shrinkWrap: true,                itemCount: items.length,                itemBuilder: (context, index) {                  return ListTile(                    title: Text('${items[index]}'),                  );                },              ),            ),          ],        ),      ),    );  }}

Example 3: listview flutter

Widget _myListView(BuildContext context) {
      return ListView(
        children: [
          ListTile(
            title: Text('Sun'),
          ),
          ListTile(
            title: Text('Moon'),
          ),
          ListTile(
            title: Text('Star'),
          ),
        ],
      );
    }

Example 4: listview flutter

Widget _myListView(BuildContext context) {

      // backing data
      final europeanCountries = ['Albania', 'Andorra', 'Armenia', 'Austria', 
        'Azerbaijan', 'Belarus', 'Belgium', 'Bosnia and Herzegovina', 'Bulgaria',
        'Croatia', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland',
        'France', 'Georgia', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland',
        'Italy', 'Kazakhstan', 'Kosovo', 'Latvia', 'Liechtenstein', 'Lithuania',
        'Luxembourg', 'Macedonia', 'Malta', 'Moldova', 'Monaco', 'Montenegro',
        'Netherlands', 'Norway', 'Poland', 'Portugal', 'Romania', 'Russia',
        'San Marino', 'Serbia', 'Slovakia', 'Slovenia', 'Spain', 'Sweden', 
        'Switzerland', 'Turkey', 'Ukraine', 'United Kingdom', 'Vatican City'];

      return ListView.builder(
        itemCount: europeanCountries.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(europeanCountries[index]),
          );
        },
      );

    }

Tags:

Misc Example