前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter组件基础——ListView

Flutter组件基础——ListView

原创
作者头像
莫空9081
修改2021-07-23 14:26:33
5730
修改2021-07-23 14:26:33
举报
文章被收录于专栏:iOS 备忘录iOS 备忘录

Flutter组件基础——ListView

ListView是滚动列表,类似于iOS中ScrollView,可横向、纵向滚动,内容不限。

<!--more-->

ListView的使用

ListView的使用很简单,但是需要多多练习;

ListView的使用,通过设置children来实现,children中的Item为Widget对象。

纵向滚动

代码如下:

代码语言:txt
复制
class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return MaterialApp(
            title: 'ListView Learn',
            home: Scaffold(
                appBar: new AppBar(
                    title: new Text('ListView Widget')
                ),
                body: new ListView(
                    children: <Widget>[
                      Container(
                        height: 50,
                        color: Colors.orangeAccent,
                        child: const Center(
                          child: Text('Entry A'),
                        ),
                      ),
                      Container(
                        height: 50,
                        color: Colors.lightGreenAccent,
                        child: const Center(
                          child: Text('Entry B'),
                        ),
                      ),
                      new ListTile(
                        leading: new Icon(Icons.access_time),
                        title: new Text('access_time'),
                      ),
                      new Image.network(
                          'https://inews.gtimg.com/newsapp_ls/0/13792660143/0.jpeg')
                    ],
                )
            )
        );
    }
}

效果如下:

simulator screen shot - iphone 12 pro max - 2021-07-23 at 09.04.14.png
simulator screen shot - iphone 12 pro max - 2021-07-23 at 09.04.14.png

横向滚动

ListViewscrollDirection控制滑动方向

代码如下

代码语言:txt
复制
class MyList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(scrollDirection: Axis.horizontal, children: [
      new Container(
        width: 180.0,
        color: Colors.lightBlue,
      ),
      new Container(
        width: 180.0,
        color: Colors.lightGreen,
      ),
      new Container(
        width: 180.0,
        color: Colors.orange,
      ),
      new Container(
        width: 180.0,
        color: Colors.orangeAccent,
      )
    ]);
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Text Widget',
        home: Scaffold(
          body: Center(
            child: Container(
              height: 200.0,
              child: MyList(),
            ),
          ),
        ));
  }
}

效果如下:

simulator screen shot - iphone 12 pro max - 2021-07-23 at 11.20.02.png
simulator screen shot - iphone 12 pro max - 2021-07-23 at 11.20.02.png

注意写法的不同,在这里自定义了一个MyList的Widget,然后在MyApp中使用MyList,就避免了在父视图嵌套太多的问题。

动态列表 ListView.builder()

使用动态列表需要先来看一下List类型,

List类型

List是集合类型,声明有几种方式,使用方式可以参考Swift中的Array

  • var myList = List(): 非固定长度的数组
  • var myList = List(2): 长度为2的数组
  • var myList = List<String>(): 创建一个String类型的数组
  • var myList = [1, 2, 3]: 创建一个1、2、3的数组

也可以使用generate方法来生成List元素,比如

代码语言:txt
复制
new List<String>.generate(1000,, (i) => "Item $i");

动态列表

代码如下:

代码语言:txt
复制
class MyList extends StatelessWidget {
  final List<String> entries = <String>['A', 'B', 'C'];
  final List colors = [
    Colors.orangeAccent,
    Colors.lightBlueAccent,
    Colors.cyanAccent
  ];

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: const EdgeInsets.all(8),
      itemCount: entries.length,
      itemBuilder: (BuildContext context, int index) {
        return Container(
          height: 50,
          color: colors[index],
          child: Center(
            child: Text('Entry ${entries[index]}'),
          ),
        );
      },
    );
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'List Build Learn',
        home: Scaffold(
          appBar: new AppBar(
            title: new Text('List Build Learn'),
          ),
          body: Center(
            child: Container(
              child: MyList(),
            ),
          ),
        ));
  }
}

效果如下:

simulator screen shot - iphone 12 pro max - 2021-07-23 at 13.38.38.png
simulator screen shot - iphone 12 pro max - 2021-07-23 at 13.38.38.png

参考

ListView Dev Doc

Flutter免费视频第二季-常用组件

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Flutter组件基础——ListView
    • ListView的使用
      • 纵向滚动
      • 横向滚动
      • 动态列表 ListView.builder()
    • 参考
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档