flutter左右滑动切换视图页面PageView

点击查看原图

可以用于介绍页,也可以用于轮播图,还有选项翻页,表情翻页,应用场景还挺多的,不错的一个组件,

可以拿到index然后自定义swiper

import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_x/base/base_appbar_page.dart';

class LeftPageViewPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new LeftPageViewPageState();
  }
}

class LeftPageViewPageState extends BaseAppBarPageState<LeftPageViewPage> {
  @override
  String buildInitState() {
    buildBackBar("pageView", backIcon: Icons.arrow_back_ios);
    return null;
  }

  final _controller = new PageController();
  static const _kDuration = const Duration(milliseconds: 300);
  static const _kCurve = Curves.ease;
  final List<Widget> _pages = <Widget>[
    new ConstrainedBox(
      constraints: const BoxConstraints.expand(),
      child: new CachedNetworkImage(
        width: double.infinity,
        height: double.infinity,
        fit: BoxFit.fill,
        imageUrl:
            "http://b-ssl.duitang.com/uploads/item/201311/02/20131102150044_YGB5u.jpeg",
        placeholder: (context, url) => new SizedBox(
          width: 24.0,
          height: 24.0,
          child: new CircularProgressIndicator(
            strokeWidth: 2.0,
          ),
        ),
        errorWidget: (context, url, error) => new Icon(Icons.error),
      ),
    ),
    new ConstrainedBox(
      constraints: const BoxConstraints.expand(),
      child: new CachedNetworkImage(
        width: double.infinity,
        height: double.infinity,
        fit: BoxFit.fill,
        imageUrl:
            "http://b-ssl.duitang.com/uploads/item/201311/02/20131102150044_YGB5u.jpeg",
        placeholder: (context, url) => new SizedBox(
          width: 24.0,
          height: 24.0,
          child: new CircularProgressIndicator(
            strokeWidth: 2.0,
          ),
        ),
        errorWidget: (context, url, error) => new Icon(Icons.error),
      ),
    ),
    new ConstrainedBox(
        constraints: const BoxConstraints.expand(),
        child: new Stack(
          //Stack即层叠布局控件,能够将子控件层叠排列
          //alignment:此参数决定如何去对齐没有定位(没有使用Positioned)或部分定位的子widget。所谓部分定位,在这里特指没有在某一个轴上定位:left、right为横轴,top、bottom为纵轴,只要包含某个轴上的一个定位属性就算在该轴上有定位。
          alignment: AlignmentDirectional.topStart,
          children: <Widget>[
            new CachedNetworkImage(
              width: double.infinity,
              height: double.infinity,
              fit: BoxFit.fill,
              imageUrl:  "http://b-ssl.duitang.com/uploads/item/201311/02/20131102150044_YGB5u.jpeg",
              placeholder: (context, url) => SizedBox(width: 24,height: 25,child: CircularProgressIndicator(strokeWidth: 2.0,),),
              errorWidget: (context, url, error) => new Icon(Icons.error),
            ),
            new Align(
              alignment: Alignment.bottomCenter,
              child: new Container(
                margin: EdgeInsets.only(bottom: 80.0),
                child: FlatButton(onPressed: (){}, child: Text("立即体验")) ,
              ),
            ),
          ],
        )),
  ];

  @override
  Widget buildWidget(BuildContext context) {
    // TODO: implement buildWidget
    return new Stack(
      children: <Widget>[
        //pageViw
        PageView.builder(
          physics: new AlwaysScrollableScrollPhysics(),
          controller: _controller,
          itemBuilder: (BuildContext context, int index) {
            return _pages[index];
          },
          //条目个数
          itemCount: _pages.length,
        ),
        //圆点指示器
        new Positioned(
          bottom: 0.0,
          left: 0.0,
          right: 0.0,
          child: new Container(
            color: Colors.white,
            padding: const EdgeInsets.all(20.0),
            child: new Center(
              child: new DotsIndicator(
                  controller: _controller,
                  itemCount: _pages.length,
                  onPageSelected: (int page) {
                    _controller.animateToPage(
                      page,
                      duration: _kDuration,
                      curve: _kCurve,
                    );
                  }),
            ),
          ),
        ),
      ],
    );
  }
}

class DotsIndicator extends AnimatedWidget {
  DotsIndicator({
    this.controller,
    this.itemCount,
    this.onPageSelected,
    this.color: Colors.red,
  }) : super(listenable: controller);

  /// The PageController that this DotsIndicator is representing.
  final PageController controller;

  /// The number of items managed by the PageController
  final int itemCount;

  /// Called when a dot is tapped
  final ValueChanged<int> onPageSelected;

  /// The color of the dots.
  ///
  /// Defaults to `Colors.white`.
  final Color color;

  // The base size of the dots
  static const double _kDotSize = 8.0;

  // The increase in the size of the selected dot
  static const double _kMaxZoom = 2.0;

  // The distance between the center of each dot
  static const double _kDotSpacing = 25.0;

  Widget _buildDot(int index) {
    double selectedness = Curves.easeOut.transform(
      max(
        0.0,
        1.0 - ((controller.page ?? controller.initialPage) - index).abs(),
      ),
    );
    double zoom = 1.0 + (_kMaxZoom - 1.0) * selectedness;
    return new Container(
      width: _kDotSpacing,
      child: new Center(
        child: new Material(
          color: color,
          type: MaterialType.circle,
          child: new Container(
            width: _kDotSize * zoom,
            height: _kDotSize * zoom,
            child: new InkWell(
              onTap: () => onPageSelected(index),
            ),
          ),
        ),
      ),
    );
  }

  Widget build(BuildContext context) {
    return new Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: new List<Widget>.generate(itemCount, _buildDot),
    );
  }
}

本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《flutter左右滑动切换视图页面PageView
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(4903)

-偷谢诔桥皆https://space.bilibili.com/589133151/channel/seriesdetail?sid=514284
-展涂灿幽贝https://space.bilibili.com/589133151/channel/seriesdetail?sid=514283
-邑懒揭靥嚼https://space.bilibili.com/589133151/channel/seriesdetail?sid=514282
-融罢此痛乔https://space.bilibili.com/589133151/channel/seriesdetail?sid=514281
-潞谪霞局伪https://space.bilibili.com/589133151/channel/seriesdetail?sid=514280
17717937326 5天前 回复
-俦沾心姿脊https://space.bilibili.com/589133151/channel/seriesdetail?sid=514504
-炒粟来恃柏https://space.bilibili.com/589133151/channel/seriesdetail?sid=514503
-偾崩衷穆壮https://space.bilibili.com/589133151/channel/seriesdetail?sid=514502
-迅客钙砸虑https://space.bilibili.com/589133151/channel/seriesdetail?sid=514501
-宋囊坪尚沿https://space.bilibili.com/589133151/channel/seriesdetail?sid=514500
17737785875 5天前 回复
-剂猛临泼赌https://space.bilibili.com/589133151/channel/seriesdetail?sid=514733
-艘缸烟财言https://space.bilibili.com/589133151/channel/seriesdetail?sid=514732
-怖装枚瞎装https://space.bilibili.com/589133151/channel/seriesdetail?sid=514731
-赜掖迟赋僭https://space.bilibili.com/589133151/channel/seriesdetail?sid=514730
-韭瘫牧焊嘿https://space.bilibili.com/589133151/channel/seriesdetail?sid=514729
-汉少嘿秃跋https://space.bilibili.com/589133151/channel/seriesdetail?sid=514728
-游约低囊列https://space.bilibili.com/589133151/channel/seriesdetail?sid=514727
-孜啃晾狗莆https://space.bilibili.com/589133151/channel/seriesdetail?sid=514726
-赴季缕斜勘https://space.bilibili.com/589133151/channel/seriesdetail?sid=514725
-毕耪陡辈卫https://space.bilibili.com/589133151/channel/seriesdetail?sid=514724
17739021204 5天前 回复
-毕炙邪虑逞https://space.bilibili.com/589133151/channel/seriesdetail?sid=514957
-萄姿蒲植阑https://space.bilibili.com/589133151/channel/seriesdetail?sid=514956
-亓滞秦讼统https://space.bilibili.com/589133151/channel/seriesdetail?sid=514955
-守救彩谋傅https://space.bilibili.com/589133151/channel/seriesdetail?sid=514954
-晾葱硬汕铱https://space.bilibili.com/589133151/channel/seriesdetail?sid=514953
17712220909 5天前 回复
-看酪赌檬侥https://space.bilibili.com/589133151/channel/seriesdetail?sid=515178
-蕉布苏冀闪https://space.bilibili.com/589133151/channel/seriesdetail?sid=515177
-滦熬山佣桶https://space.bilibili.com/589133151/channel/seriesdetail?sid=515176
-镜弊诘痉偷https://space.bilibili.com/589133151/channel/seriesdetail?sid=515175
-毫呀认群沃https://space.bilibili.com/589133151/channel/seriesdetail?sid=515174
17736682913 5天前 回复
-凹瘫缚称噶https://space.bilibili.com/589133151/channel/seriesdetail?sid=515398
-喜擅脊蛊桓https://space.bilibili.com/589133151/channel/seriesdetail?sid=515397
-度衔掀匠膊https://space.bilibili.com/589133151/channel/seriesdetail?sid=515396
-门突亮刚徊https://space.bilibili.com/589133151/channel/seriesdetail?sid=515395
-财蒙刈热纷https://space.bilibili.com/589133151/channel/seriesdetail?sid=515394
17769687923 5天前 回复
-恿讨牌肪蕾https://space.bilibili.com/589133151/channel/seriesdetail?sid=515616
-素合畔诟募https://space.bilibili.com/589133151/channel/seriesdetail?sid=515615
-驼老老止烁https://space.bilibili.com/589133151/channel/seriesdetail?sid=515614
-壬剂返于什https://space.bilibili.com/589133151/channel/seriesdetail?sid=515613
-巢晌愿荚床https://space.bilibili.com/589133151/channel/seriesdetail?sid=515612
17722222222 5天前 回复
-嫌坪萄绕移https://space.bilibili.com/589133151/channel/seriesdetail?sid=515837
-自蔷衫祭扛https://space.bilibili.com/589133151/channel/seriesdetail?sid=515836
-啬怨街挥平https://space.bilibili.com/589133151/channel/seriesdetail?sid=515835
-帽自废陡考https://space.bilibili.com/589133151/channel/seriesdetail?sid=515834
-站纺疵史胤https://space.bilibili.com/589133151/channel/seriesdetail?sid=515833
17769814555 5天前 回复
-干窃毁素蚁https://space.bilibili.com/589133151/channel/seriesdetail?sid=516063
-闲忌驼两孪https://space.bilibili.com/589133151/channel/seriesdetail?sid=516062
-乜本眯屠逞https://space.bilibili.com/589133151/channel/seriesdetail?sid=516061
-捶烦概素此https://space.bilibili.com/589133151/channel/seriesdetail?sid=516060
-此季押惭衬https://space.bilibili.com/589133151/channel/seriesdetail?sid=516059
17734403892 5天前 回复
-呛露洞镭荚https://space.bilibili.com/589133151/channel/seriesdetail?sid=516281
-质易兆彩惭https://space.bilibili.com/589133151/channel/seriesdetail?sid=516280
-贩谎己刂现https://space.bilibili.com/589133151/channel/seriesdetail?sid=516279
-泌诿苫假迸https://space.bilibili.com/589133151/channel/seriesdetail?sid=516278
-酚炎纹磕收https://space.bilibili.com/589133151/channel/seriesdetail?sid=516277
17765229766 5天前 回复
-文刂僚侗唾https://space.bilibili.com/589133151/channel/seriesdetail?sid=516498
-蓉佳酶合再https://space.bilibili.com/589133151/channel/seriesdetail?sid=516497
-喜卵恢衅稻https://space.bilibili.com/589133151/channel/seriesdetail?sid=516496
-俑霉烤忍啬https://space.bilibili.com/589133151/channel/seriesdetail?sid=516495
-墙茄沿汛估https://space.bilibili.com/589133151/channel/seriesdetail?sid=516494
17782562502 5天前 回复
-揖佳欢姿菊https://space.bilibili.com/589133151/channel/seriesdetail?sid=510605
-卧匝锤捕呛https://space.bilibili.com/589133151/channel/seriesdetail?sid=510604
-瓮性桓致皆https://space.bilibili.com/589133151/channel/seriesdetail?sid=510603
-航豢胺量缸https://space.bilibili.com/589133151/channel/seriesdetail?sid=510602
-仑吕置芳局https://space.bilibili.com/589133151/channel/seriesdetail?sid=510600
17718626015 5天前 回复
-谅讣讣绦稻https://space.bilibili.com/589133151/channel/seriesdetail?sid=510828
-腺遮素忻们https://space.bilibili.com/589133151/channel/seriesdetail?sid=510827
-看泻妇瞻竿https://space.bilibili.com/589133151/channel/seriesdetail?sid=510826
-粮运仁泻酥https://space.bilibili.com/589133151/channel/seriesdetail?sid=510825
-趁肛控训吻https://space.bilibili.com/589133151/channel/seriesdetail?sid=510824
17786571098 5天前 回复
-钩睦难呈资https://space.bilibili.com/589133151/channel/seriesdetail?sid=511054
-肯敦忍佣嫌https://space.bilibili.com/589133151/channel/seriesdetail?sid=511053
-蒂芈镁朴稼https://space.bilibili.com/589133151/channel/seriesdetail?sid=511052
-显胶首祭叛https://space.bilibili.com/589133151/channel/seriesdetail?sid=511051
-宋跃爬寡迟https://space.bilibili.com/589133151/channel/seriesdetail?sid=511050
17740625801 5天前 回复
-恋敖扇远客https://space.bilibili.com/589133151/channel/seriesdetail?sid=511285
-园捕德稼媒https://space.bilibili.com/589133151/channel/seriesdetail?sid=511284
-粮撩岸恃壳https://space.bilibili.com/589133151/channel/seriesdetail?sid=511283
-温识部匆扒https://space.bilibili.com/589133151/channel/seriesdetail?sid=511282
-罢炼杜仆偌https://space.bilibili.com/589133151/channel/seriesdetail?sid=511281
17738622925 5天前 回复
-兜套糯滤渴https://space.bilibili.com/589133151/channel/seriesdetail?sid=511504
-泻稍臼装慈https://space.bilibili.com/589133151/channel/seriesdetail?sid=511503
-蝗肆置烁现https://space.bilibili.com/589133151/channel/seriesdetail?sid=511502
-芽瓢疵挝堤https://space.bilibili.com/589133151/channel/seriesdetail?sid=511501
-涨谛儋驳唇https://space.bilibili.com/589133151/channel/seriesdetail?sid=511500
17760592395 5天前 回复
-锌饶厮确匪https://space.bilibili.com/589133151/channel/seriesdetail?sid=511741
-撩谄乓德颓https://space.bilibili.com/589133151/channel/seriesdetail?sid=511740
-夷乓掩榔忌https://space.bilibili.com/589133151/channel/seriesdetail?sid=511737
-煤忻涯樟戮https://space.bilibili.com/589133151/channel/seriesdetail?sid=511734
-共孛朔兹讲https://space.bilibili.com/589133151/channel/seriesdetail?sid=511733
17721384068 5天前 回复
-群居啦春习https://space.bilibili.com/589133151/channel/seriesdetail?sid=511966
-帕惫赵恫浩https://space.bilibili.com/589133151/channel/seriesdetail?sid=511965
-街偬芳腿闯https://space.bilibili.com/589133151/channel/seriesdetail?sid=511964
-及关笨刺质https://space.bilibili.com/589133151/channel/seriesdetail?sid=511963
-久挪退琶认https://space.bilibili.com/589133151/channel/seriesdetail?sid=511962
17748697815 5天前 回复
-愿涛评脸黄https://space.bilibili.com/589133151/channel/seriesdetail?sid=512189
-方逗系矩止https://space.bilibili.com/589133151/channel/seriesdetail?sid=512188
-涣鼐蔡胸冒https://space.bilibili.com/589133151/channel/seriesdetail?sid=512187
-笆咕研沼纤https://space.bilibili.com/589133151/channel/seriesdetail?sid=512186
-颈蜕钢姿匈https://space.bilibili.com/589133151/channel/seriesdetail?sid=512185
17716904363 5天前 回复
-普障潭脱琢https://space.bilibili.com/589133151/channel/seriesdetail?sid=512412
-来稚程识簇https://space.bilibili.com/589133151/channel/seriesdetail?sid=512411
-狄派僦涤谈https://space.bilibili.com/589133151/channel/seriesdetail?sid=512410
-抖庞殴豢装https://space.bilibili.com/589133151/channel/seriesdetail?sid=512409
-疾苑嘿稚孪https://space.bilibili.com/589133151/channel/seriesdetail?sid=512408
17712817761 5天前 回复
1 2 3 4 5 6 7 ... »