移动开发中圆点指示器给用户很好的用户体验,iOS 有自己的控件UIPageControl 方便快捷实现。既然想用Flutter代替,就想着如何实现UIPageControl 类似的功能。这篇文章就在前辈的基础上,实战一下PageView指示器。来实现圆点指示器的功能。(文章结尾会贴上参考的文章)

先看一下效果

完整代码:


import 'dart:math';
import 'package:flutter/material.dart';class PageControlTest extends StatelessWidget {@overrideWidget build(BuildContext context) {return new Scaffold(appBar: new AppBar(title: new Text('DemoList'),),body: new MyHomePage(),);}
}/// An indicator showing the currently selected page of a PageController
class DotsIndicator extends AnimatedWidget {DotsIndicator({this.controller,this.itemCount,this.onPageSelected,this.color: Colors.white,}) : super(listenable: controller);/// The PageController that this DotsIndicator is representing.final PageController controller;/// The number of items managed by the PageControllerfinal int itemCount;/// Called when a dot is tappedfinal ValueChanged<int> onPageSelected;/// The color of the dots.////// Defaults to `Colors.white`.final Color color;// The base size of the dotsstatic const double _kDotSize = 8.0;// The increase in the size of the selected dotstatic const double _kMaxZoom = 2.0;// The distance between the center of each dotstatic const double _kDotSpacing = 25.0;Widget _buildDot(int index,int pageCount) {// 当前位置double currentPosition =((controller.page ?? controller.initialPage.toDouble()) %pageCount.toDouble());// 计算变化曲线double selectedness = Curves.easeOut.transform(max(0.0,1.0 - (currentPosition - index).abs(),),);// 从0点跳到最后一个时状态需要特殊处理if (currentPosition > pageCount - 1 && index == 0) {selectedness = 1 - (pageCount.toDouble() - currentPosition);}print('selectedness $index');print('selectedness $selectedness');print('selectedness $controller.page');print('selectedness $controller.initialPage');// 计算缩放大小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,(int index) {return _buildDot(index, itemCount);}),);}
}class MyHomePage extends StatefulWidget {@overrideState createState() => new MyHomePageState();
}class MyHomePageState extends State<MyHomePage> {final _controller = new PageController();static const _kDuration = const Duration(milliseconds: 300);static const _kCurve = Curves.ease;final _kArrowColor = Colors.black.withOpacity(0.8);final List<Widget> _pages = <Widget>[new ConstrainedBox(constraints: const BoxConstraints.expand(),child: new FlutterLogo(colors: Colors.blue),),new ConstrainedBox(constraints: const BoxConstraints.expand(),child: new FlutterLogo(style: FlutterLogoStyle.stacked, colors: Colors.red),),new ConstrainedBox(constraints: const BoxConstraints.expand(),child: new FlutterLogo(style: FlutterLogoStyle.horizontal, colors: Colors.green),),];@overrideWidget build(BuildContext context) {return new Scaffold(
//      appBar: new AppBar(
//        title: new Text('PageControl'),
//      ),body: Center(child:new IconTheme(data: new IconThemeData(color: _kArrowColor),child: new Stack(children: <Widget>[new PageView.builder(physics: new AlwaysScrollableScrollPhysics(),controller: _controller,itemBuilder: (BuildContext context, int index) {return _pages[index % _pages.length];},),new Positioned(bottom: 0.0,left: 0.0,right: 0.0,child: new Container(color: Colors.grey[800].withOpacity(0.5),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,);},),),),),],),),));}
}

BannerGalleryInFlutter

这是一个多样化定制的轮播图,实现无限滑动Banner。

参考文章

Flutter pageview切换指示器
flutter PageView实现左右滑动切换视图

Flutter开发之PageView指示器(31)相关推荐

  1. Flutter开发之ListView添加HeaderView和FooterView-2(39)

    参考文章:RecyclerView添加HeaderView和FooterView 接着Flutter开发之ListView添加HeaderView和FooterView-1 继续研究. 通过Recyc ...

  2. Flutter开发之AlertDialog、AboutDialog对话框组件-2(41)

    继上一篇介绍了SimpleDialog对话框组件 Flutter开发之SimpleDialog对话框组件-1(40) 这里再介绍一种带有确定.取消按钮的对话框组件:AlertDialog.AboutD ...

  3. Flutter开发之ListView使用第三方flutter_refresh加载更多(37)

    在Flutter开发之ListView使用第三方pull_to_refresh加载更多(36) 中我们实现了下拉刷新.上拉分页加载的功能.今天介绍另一个ListView使用第三方flutter_ref ...

  4. Flutter开发之ListView使用第三方pull_to_refresh加载更多(36)

    在Flutter开发之ListView下拉刷新&上拉加载更多(35) 中我们实现了下拉刷新.上拉分页加载的功能.但是使用起来非常不方便,且不满一屏时难以处理. 今天介绍ListView使用第三 ...

  5. Flutter开发之ListView下拉刷新上拉加载更多(35)

    在Flutter开发之ListView组件(21) 文章中,我们了解了ListView组件的基本使用.但是数据比较少,没有涉及分页加载.而实际开发中,下拉刷新和分页加载几乎是所有APP的标配.在iOS ...

  6. Flutter开发之iOS后台定位开发详解

    Flutter开发之iOS后台定位开发详解 需求目的 开发一个功能持续获取用户的位置发送给后端,PC端会根据后端传来的移动端发送的位置信息,来绘制使用者的运动轨迹. 实现需求 是否实现 后台定位 ✅ ...

  7. Flutter开发之Input-TextField-文本输入框(45)

    之前介绍了一些基础控件,回头来看遗漏了一些基础的控件的用法.TextField 是最常用的文本输入widget.今天就来学习使用一下. 参看: Flutter组件-Input-TextField-文本 ...

  8. Flutter开发之BottomSheetDialog选择组件-5(44)

    BottomSheetDialog.ModalBottomSheetDialog同样也是需要借助showDialog唤起,就跟它名字一样,这两种dialog是从屏幕下方向上弹出的,不同的是Bottom ...

  9. Flutter开发之SnackBar提示组件-4(43)

    SnackBar无论是用法还是功能使用几乎都跟原生Android一样 ,唯一有一点需要留意的是在Scaffold.of(context).showSnackBar()中传递的context必须不能是S ...

最新文章

  1. 【探索PowerShell 】【三】PowerShell下使用Aliases
  2. 韩顺平php视频笔记51-52 数组的概念 创建 遍历
  3. [转载] 机器学习 scikit-learn1 预测贷款用户是否会逾期
  4. gnuwin32从全量备份中单表还原_入门MySQL——备份与恢复
  5. 8 EDA技术实用教程【组合电路的Verilog的设计】
  6. 诛仙2怎样修改服务器时间同步,《诛仙2》2月22日更新公告
  7. 污水流量计常见故障形成原因检测方法
  8. 6U VPX数据存储板学习资料保存:基于6U VPX 的mSATA高性能数据存储板
  9. 锂电池电量百分比计算_电池soc是什么意思
  10. 神经网络的参数(Weight)
  11. android vr sdk 架构,PowerVR图形SDK v4.0及工具终于问世
  12. echart可视化图表多条折线显示数据混乱问题
  13. quartus更新symbol后没反应_Quartus II使用常见问题
  14. java定义数组变量初始化为0_java中怎么数组初始化?
  15. vue支付宝html,vue 解决在微信内置浏览器中调用支付宝支付的情况
  16. ensp查看历史配置命令_华为路由查看配置命令是什么?
  17. 性能分析与问题排查:工具:三件套:HeapHero
  18. CSS gradient渐变之webkit核心浏览器下的使用
  19. 【转】WebMatrix 从零建站如此简单!来自微软的免费网站服务器快速建站套件
  20. Vue3前端模仿实现Windows窗口

热门文章

  1. docker笔记1----Get Docker
  2. 手摸手,带你用 vue 动画实现原生 app 切换效果,丝滑般的体验
  3. centos6.5搭建lnmp环境
  4. ubuntu14.04 qt4 C++开发环境搭建
  5. forfiles命令批量删除N天前文件
  6. FCKeditor的使用说明
  7. WDSL文件中的XML元素
  8. Openresty使用
  9. C#反射(Reflection)详解
  10. NYOJ-括号配对问题 技巧性的非栈道法