美文网首页Flutter实战专题
Flutter基础篇之三-创建一个列表ListView

Flutter基础篇之三-创建一个列表ListView

作者: Michale_Zuo | 来源:发表于2020-09-19 14:52 被阅读0次

1.pubspec.yaml文件里添加 english_words: ^3.1.5依赖

image.png
2.main.dart文件里添加ListView
IDE敲stful,出现创建模板,给class取名字
image.png
在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

相关文章

网友评论

    本文标题:Flutter基础篇之三-创建一个列表ListView

    本文链接:https://www.haomeiwen.com/subject/ofvcyktx.html