先贴一下运行效果

在开发过程中往往会遇到需求是这样的,我们要实现的效果如图二所示。

项目中使用的轮播图插件是:

通过查阅Swipe_pagination的源代码可知,目前只提供了FractionPaginationBuilder(百分数类型的,例如:1/5)、RectSwipePaginationBuilder(矩形)、DotSwiperPaginationBuilder(圆点)。像我们目前的需求就是做带圆角的,选中的是带圆角的圆柱,未选中的是圆点。

最终通过复制修改代码(swipe_pagination.dart)实现的功能

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';class SquareSwiperPaginationBuilder extends SwiperPlugin {///color when current index,if set null , will be Theme.of(context).primaryColorfinal Color activeColor;///,if set null , will be Theme.of(context).scaffoldBackgroundColorfinal Color color;///Size of the dot when activatefinal double activeSize;///Size of the dotfinal double size;/// Space between dotsfinal double space;///加了一个自定义高度的参数,如果要使用Size.height 的话需要修改size的类型(double->Size),这样的话也需要重新写PageIndicator,过犹不及了。///Height of the dotsfinal double height;final Key key;const SquareSwiperPaginationBuilder({this.activeColor,this.color,this.key,this.height: 4.0,this.size: 4.0,this.activeSize: 10.0,this.space: 3.0});@overrideWidget build(BuildContext context, SwiperPluginConfig config) {if (config.itemCount > 20) {print("The itemCount is too big, we suggest use FractionPaginationBuilder instead of DotSwiperPaginationBuilder in this sitituation");}Color activeColor = this.activeColor;Color color = this.color;if (activeColor == null || color == null) {ThemeData themeData = Theme.of(context);activeColor = this.activeColor ?? themeData.primaryColor;color = this.color ?? themeData.scaffoldBackgroundColor;}if (config.indicatorLayout != PageIndicatorLayout.NONE &&config.layout == SwiperLayout.DEFAULT) {return new PageIndicator(count: config.itemCount,controller: config.pageController,layout: config.indicatorLayout,size: size,activeColor: activeColor,color: color,space: space,);}List<Widget> list = [];int itemCount = config.itemCount;int activeIndex = config.activeIndex;for (int i = 0; i < itemCount; ++i) {bool active = i == activeIndex;list.add(Container(key: Key("pagination_$i"),margin: EdgeInsets.all(space),child: active? SizedBox(width: activeSize,height: height,child: DecoratedBox(decoration: BoxDecoration(borderRadius:BorderRadius.all(Radius.circular(height / 2)),color: activeColor),),): SizedBox(width: size,height: height,child: DecoratedBox(decoration: BoxDecoration(borderRadius:BorderRadius.all(Radius.circular(height / 2)),color: color),),),));}if (config.scrollDirection == Axis.vertical) {return new Column(key: key,mainAxisSize: MainAxisSize.min,children: list,);} else {return new Row(key: key,mainAxisSize: MainAxisSize.min,children: list,);}}
}typedef Widget SwiperPaginationBuilder(BuildContext context, SwiperPluginConfig config);class SwiperCustomPagination extends SwiperPlugin {final SwiperPaginationBuilder builder;SwiperCustomPagination({@required this.builder});@overrideWidget build(BuildContext context, SwiperPluginConfig config) {return builder(context, config);}
}class SquareSwiperPagination extends SwiperPlugin {static const SwiperPlugin square = const SquareSwiperPaginationBuilder();/// Alignment.bottomCenter by default when scrollDirection== Axis.horizontal/// Alignment.centerRight by default when scrollDirection== Axis.verticalfinal Alignment alignment;/// Distance between pagination and the containerfinal EdgeInsetsGeometry margin;/// Build the widetfinal SwiperPlugin builder;final Key key;const SquareSwiperPagination({this.alignment,this.key,this.margin: const EdgeInsets.all(10.0),this.builder: SquareSwiperPagination.square});Widget build(BuildContext context, SwiperPluginConfig config) {Alignment alignment = Alignment.bottomCenter;Widget child = Container(margin: margin,child: this.builder.build(context, config),);///拿掉这个判断之后,使得指示器无论在轮播图之上还是下面都可以居中// if (!config.outer) {child = new Align(key: key,alignment: alignment,child: child,);// }return child;}
}

大部分代码都是源代码,主要是改了最后list.add(Container(...))部分(大约在第70行代码附近),通过active的判断,返回不同的Widget。

在实际使用中就可以这样写:

这样就可以实现如文章顶部第二个轮播图的效果。

  • color:是指默认的未选中的指示器的颜色
  • activeColor:选中时的指示器的颜色
  • size:未选中时的指示器的宽度
  • activeSize:选中时的指示器的宽度

同时,自定义的指示器的代码里面还加入了一个height参数,用来设置指示器的高度。可以通过设置size和activeSize一致,或包含height,再控制圆角实现, 实现矩形的定长指示器,这里不再赘述。

Flutter练习:实现自定义的分页指示器相关推荐

  1. flutter tabBar 选项卡自定义指示器

    使用flutter TabBar 做自定义选项卡. 1.根据index  使用三目运算,缺点点击以后没有滑动效果. tabs:TabMoudels.map((i) => Container(pa ...

  2. 【Flutter】自定义 Flutter 组件 ( 创建自定义 StatelessWidget、StatefulWidget 组件 | 调用自定义组件 )

    文章目录 一.Flutter 组件简介 二.Flutter 自定义 StatelessWidget 组件流程 1.导入父类包 2.选择继承的父类 3.设置成员变量及构造函数 4.重写 build 方法 ...

  3. Flutter RichText支持自定义文本溢出效果

    extended text 相关文章 Flutter RichText支持图片显示和自定义图片效果 Flutter RichText支持自定义文本溢出效果 Flutter RichText支持自定义文 ...

  4. Flutter RichText支持自定义文字背景

    extended text 相关文章 Flutter RichText支持图片显示和自定义图片效果 Flutter RichText支持自定义文本溢出效果 Flutter RichText支持自定义文 ...

  5. 给DataList分页有两个办法:1、自定义实现分页方法 2、用第三方控件(例如AspNetPager)

    给DataList分页有两个办法:1.自定义实现分页方法 2.用第三方控件(例如AspNetPager) 先介绍下如何自定义实现分页方法. 我的DataList分页方法的核心原理是利用PagedDat ...

  6. Flutter代码锦囊---自定义曲线裁剪

    在实际开发中,很多APP里面都会有一些布局需要用到曲线,而不是直线,下面举两个例子,一个是京东APP的页面,一个是淘宝APP的页面,它们都用到了曲线. 在Flutter中可以怎么实现这个效果呢,有一个 ...

  7. Mybatis自定义轻量级分页组件(易集成,易拓展)

    Mybatis自定义轻量级分页组件(易集成,易拓展) 其实github有一个叫做PageHelper的开源分页组件,我也用过,封装的还可以.只是感觉他的量级偏重,其实很多参数,都是我们开发中不需要的参 ...

  8. ViewPager Indicator 自定义标题和指示器样式

    ViewPager Indicator 自定义标题和指示器样式 Indicator 自定义样式 可支持自定义内容 使用方式 1.xml中直接引用 2.绑定ViewPager 3.自定义样式实现和修改 ...

  9. 安卓Tablayout自定义文字、指示器长度和颜色

    安卓Tablayout自定义文字.指示器长度和颜色 废话不多说,先上效果图.没有效果图的文章都是扯淡: 安卓Tablayout自定义文字.指示器长度和颜色 新的改变 以上就是所有的代码 附上demo源 ...

最新文章

  1. 如何将txt文档插入sql2000数据库
  2. 计算机组成原理课后习题答案一
  3. Spring Validation 校验
  4. 点击链接,执行.py脚本,cgi脚本,浏览器中没有显示解析后的web页面,而是.py文件本身的代码内容...
  5. 又一起.NET程序挂死, 用 Windbg 抽丝剥茧式的真实案例分析
  6. matlab画半球面,Matlab 绘制3D半球
  7. javascript基础学习
  8. 全面介绍单元测试 -转贴
  9. UI设计师——你是什么设计师?
  10. python爬取商品信息
  11. 最棒的游戏制作软件VAM Virt A mate汉化 优秀豪华
  12. Nachos操作系统-文件系统添加多级目录
  13. 常用运放做跟随器使用总结
  14. 【Genotype(基因串) 玩具取名】题解
  15. 你知道PDF拆分合并怎么弄吗?两个方法帮你轻松搞定!
  16. DNk开发步骤与环境配置
  17. Window系统多硬盘设置新引导盘
  18. 华为防火墙基础自学系列 | 证书申请方式
  19. 8代u能装服务器系统吗,8代U安装win7,几代U支持win7
  20. 做设计师要用到的工具软件

热门文章

  1. 星球乐园 | 害怕水逆是因为你已经很久没有读完一本书
  2. 认识数字影片版本(CAM、TS、TC、DVD、HD、BD、TVRIP等)
  3. 【三维路径规划】基于matlab复杂三维地形的无人机路径规划【含Matlab源码 2171期】
  4. 成都五月花计算机专业学校正规吗,成都市|成都市五月花计算机专业学校怎么样...
  5. php%3e%3etxt_《3e迅雷》_3e迅雷最新版下载
  6. 谷歌浏览器的图片下载
  7. 下载蓝盒插件_Fredo6插件下载 FredoTools(Fredo工具箱) 3.8c for Sketchup 中文免费版 下载-脚本之家...
  8. TensorRt推理部署优化方案及流程概述
  9. 台式计算机不显示,台式电脑开机显示器不显示怎么办
  10. hive取当前周的周一