Flutter基础控件之PageView

简介

PageView 是一个滑动视图列表,它也是一个无状态的Widget。

class PageView extends StatefulWidget {
  /// Creates a scrollable list that works page by page from an explicit [List]
  /// of widgets.
  ///
  /// This constructor is appropriate for page views with a small number of
  /// children because constructing the [List] requires doing work for every
  /// child that could possibly be displayed in the page view, instead of just
  /// those children that are actually visible.
  PageView({
    Key key,
    this.scrollDirection = Axis.horizontal, // 滚动方向
    this.reverse = false, // 反转视图index
    PageController controller, // 视图控制器
    this.physics,
    this.pageSnapping = true, // 弹性
    this.onPageChanged, 
    List<Widget> children = const <Widget>[],
    this.dragStartBehavior = DragStartBehavior.start,
  }) : controller = controller ?? _defaultPageController,
       childrenDelegate = SliverChildListDelegate(children),
       super(key: key);

PageView简单用法

class PageViewDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PageView(
//      reverse: true,
//      scrollDirection: Axis.vertical,
//      pageSnapping: false,
      controller: PageController(
        initialPage: 2,
        keepPage: false,
        viewportFraction: 0.8,
      ),
      onPageChanged: (currentPageIndex) =>
          debugPrint('Page: $currentPageIndex'),
      children: <Widget>[
        Container(
          color: Colors.red,
          alignment: Alignment(0.0, 0.0),
          child: Text(
            '第一个页面',
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ),
        Container(
          color: Colors.grey,
          alignment: Alignment(0.0, 0.0),
          child: Text(
            '第二个页面',
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ),
        Container(
          color: Colors.green,
          child: Text(
            '第三个页面',
            style: TextStyle(
              color: Colors.black,
              fontSize: 20.0,
            ),
          ),
        ),
      ],
    );
  }
}

可配置的PageView.builder

class PageViewBuilderDemo extends StatelessWidget {
  Widget _pageItemBuilder(BuildContext context, int index) {
    return Stack(
      children: <Widget>[
        SizedBox.expand(
          child: Image.network(
            posts[index].imageUrl,
            fit: BoxFit.cover,
          ),
        ),
        Positioned(
          bottom: 20.0,
          left: 20.0,
          child: Column(
            children: <Widget>[
              Text(
                posts[index].title,
                style: TextStyle(
                  color: Colors.red,
                  fontSize: 30.0,
                ),
              ),
              Text(
                posts[index].author,
                style: TextStyle(
                  color: Colors.yellow,
                  fontSize: 25.0,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return PageView.builder(
      itemCount: posts.length,
      itemBuilder: _pageItemBuilder,
    );
  }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 -- 这是一篇陆陆续续写了三天的文章 在实际的开发中我们经常会遇到用列表展示数据,当内容超过一屏的时候可以进...
    迷途小顽童阅读 4,995评论 0 3
  • HTML+CSS是前端开发的基础。HTML是超文本标记语言,使用文本和HTML来描述网页,定义页面的内容。CSS是...
    GreenTeakS阅读 4,551评论 1 2
  • [原创]2018-04-27 21:45 夏日 夏日夕陽 01 公元二O一二年三月二十二,上午九时...
    夏日_a4ac阅读 622评论 7 9
  • 计划与自我认知 写计划这件事不知道从什么时候开始的,大概是上初中的时候,大概是我第一次考试不是那么理想,被老娘逼着...
    反身性试探阅读 122评论 0 1