1.pubspec.yaml文件里添加 english_words: ^3.1.5
依赖
2.main.dart文件里添加ListView
IDE敲
stful
,出现创建模板,给class取名字在build方法的return 方法里创建ListView
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: RandomWords(),
);
}
}
class RandomWords extends StatefulWidget {
@override
_RandomWordsState createState() => _RandomWordsState();
}
class _RandomWordsState extends State<RandomWords> {
final _suggestions = <WordPair>[];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ListView'),
),
body: _buildSuggestions(),
);
}
Widget _buildSuggestions() {
return ListView.builder( //创建ListView
padding: EdgeInsets.all(16),
itemBuilder: (contentx, i) {
if (i.isOdd) return Divider(); // 间隔线
final index = i ~/ 2;
if (index >= _suggestions.length) {// 获取新的20个数据
_suggestions.addAll(generateWordPairs().take(20));
}
return _buildRow(_suggestions[index]);
});
}
// iOS里的每个cell
Widget _buildRow(WordPair pair) {
return ListTile(
title: Text(
pair.asPascalCase,
),
trailing: Icon(
Icons.favorite_border,
),
onTap: () {
print('tap ListView Row');
},
);
}
}
详情请见demo