Flutter Tutorial part 2: Building a listview in Flutter

Ying Chen
2 min readApr 19, 2019

--

In this tutorial, I will show you how to build a listview in Flutter.

Create a new flutter app from the demo.

Build your list view

Widget _buildListView() {
return ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: (context, i) {
return _buildRow("row: " + i.toString());
}
);
}

There is a function for building row _buildRow

Widget _buildRow(String message) {
return ListTile(
title: Text(
message,
// style: _biggerFont,
),
);
}

Then update the main.dart file as following.

import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Solar Flutter',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter List View'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _buildListView(),
);
}
Widget _buildListView() {
return ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: (context, i) {
return _buildRow("row: " + i.toString());
}
);
}

Widget _buildRow(String message) {
return ListTile(
title: Text(
message,
// style: _biggerFont,
),
);
}
}

That’s it!

Github Link

More tutorials

--

--