main.dart
import 'package:flutter/material.dart'; import 'list.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Material App', home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override State<StatefulWidget> createState() { return MyHomeState(); } } class MyHomeState extends State<MyHomePage> { var count = 0; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('AppBar title')), body: Center( child: Column( children: [ Text('hello $count'), MaterialButton( color: Colors.blue, textColor: Colors.white, child: Text('点我'), onPressed: () { Navigator.push(context, MaterialPageRoute(builder: (context) { return ListPage(); })); }) ], ), ), ); } }
list.dart
import 'package:flutter/material.dart'; class ListPage extends StatelessWidget { @override Widget build(BuildContext context) { var dataList = []; for (var i = 0; i < 5; i++) { dataList .add({'id': 'id$i', 'title': 'title$i', 'subtitle': 'subtitle$i'}); } List<Widget> widgetList = <Widget>[]; for (var item in dataList) { widgetList.add(ListTile( title: Text(item['title']), subtitle: Text(item['subtitle']), leading: Icon( Icons.abc, color: Colors.orange, ), trailing: Icon(Icons.keyboard_arrow_right), )); } return Scaffold( appBar: AppBar( title: Text('AppBar'), ), body: Center( child: ListView( children: widgetList, ), ), ); } }