美文网首页
Flutter-ListView

Flutter-ListView

作者: 梦幽辰 | 来源:发表于2019-12-29 18:57 被阅读0次

    ListView是最常用的可滚动组件之一,它可以沿一个方向线性排布所有子组件,并且它也支持基于Sliver的延迟构建模型。

    class ListViewDemoPage extends StatelessWidget {
      Widget _listItemBuilder(BuildContext context, int index){
        return Container(
          color: Colors.white,
          margin: EdgeInsets.all(8),
          child: Column(
            children: <Widget>[
              Image.network(posts[index].imageUrl),
              SizedBox(height: 16,),
              Text(
                posts[index].title,
                style: Theme.of(context).textTheme.title,
              ),
              Text(
                posts[index].author,
                style: Theme.of(context).textTheme.subhead,
              ),
              SizedBox(height: 16,)
            ],
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.grey[200],
          appBar: AppBar(
            title: Text('ListView'),
          ),
          body: ListView.builder(
            itemCount: posts.length, // post为一个数据源
            itemBuilder: _listItemBuilder,
          ),
        );
      }
    }
    

    数据源post

    class Post {
      Post({
        this.title,
        this.author,
        this.imageUrl,
        this.description,
      });
    
      final String title;
      final String author;
      final String imageUrl;
      final String description;
    
      bool selected = false;
    }
    
    final List<Post> posts = [
      Post(
        title: 'Candy Shop',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/candy-shop.jpg',
      ),
      Post(
        title: 'Childhood in a picture',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/childhood-in-a-picture.jpg',
      ),
      Post(
        title: 'Contained',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/contained.jpg',
      ),
      Post(
        title: 'Dragon',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/dragon.jpg',
      ),
      Post(
        title: 'Free Hugs',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/free_hugs.jpg',
      ),
      Post(
        title: 'Gravity Falls',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/gravity-falls.png',
      ),
      Post(
        title: 'Icecream Truck',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/icecreamtruck.png',
      ),
      Post(
        title: 'keyclack',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/keyclack.jpg',
      ),
      Post(
        title: 'Overkill',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/overkill.png',
      ),
      Post(
        title: 'Say Hello to Barry',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/say-hello-to-barry.jpg',
      ),
      Post(
        title: 'Space Skull',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/space-skull.jpg',
      ),
      Post(
        title: 'The Old Fashioned',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/the-old-fashioned.png',
      ),
      Post(
        title: 'Tornado',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/tornado.jpg',
      ),
      Post(
        title: 'Undo',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/undo.jpg',
      ),
      Post(
        title: 'White Dragon',
        author: 'Mohamed Chahin',
        description: 'Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.',
        imageUrl: 'https://resources.ninghao.org/images/white-dragon.jpg',
      )
    ];
    

    相关文章

      网友评论

          本文标题:Flutter-ListView

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