探索Flutter实战:GitCode上的《Flutter Book Samples》项目详解
warning:
这篇文章距离上次修改已过192天,其中的内容可能已经有所变动。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BookListPage(),
);
}
}
class BookListPage extends StatefulWidget {
@override
_BookListPageState createState() => _BookListPageState();
}
class _BookListPageState extends State<BookListPage> {
// 假设的图书列表数据
final List<Map<String, Object>> books = [
{
'title': 'Book 1',
'author': 'Author 1',
},
{
'title': 'Book 2',
'author': 'Author 2',
},
// ...更多图书数据
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Books'),
),
body: ListView.builder(
itemCount: books.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(books[index]['title']),
subtitle: Text(books[index]['author']),
);
},
),
);
}
}
这段代码展示了如何在Flutter中创建一个简单的图书列表界面。它使用了ListView.builder
来高效构建列表项,并展示了如何使用StatefulWidget
来管理状态,这对于学习如何在Flutter中处理用户界面的数据流是很有帮助的。
评论已关闭