Flutter 轮播图封装

Flutter 轮播图封装

PageView组件

支持滚动、懒加载、按需渲染,可以用来实现分页加载、轮播图,可以通过scrollDirection 设置滚动的方向。

效果图

flutter PageView 实现轮播图

实现

dart 复制代码
 @override
  void initState() {
    super.initState();
    // 组件初始化时启动定时器(可选)
    _setTimer();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  List<int> _list = List.generate(10, (index) => index + 1);
  int _currentIndex = 0;
  PageController _pageController = PageController();
  Timer? _timer;

  /// 设置定时器
  void _setTimer() {
    _timer?.cancel();
    _timer = Timer.periodic(Duration(seconds: 3), (timer) {
    // 3s 自动轮播
      _currentIndex += 1;
      if (_currentIndex == _list.length) {
        _currentIndex = 0;
      }
      _jumpPage(_currentIndex);
      setState(() {});
    });
  }

  void _cancelTimer() {
    _timer?.cancel();
  }

  void _jumpPage(int index) {
    // 跳转到指定页面,动画平滑过渡
    _pageController.animateToPage(index,
        duration: const Duration(milliseconds: 300), curve: Curves.linear).then((value) {
        	// 重启定时器
          _setTimer();
        },);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blue,
      child: Stack(
        children: [
          PageView(
            onPageChanged: (index) {}, // 也可以在此处设置_curIndex = index, 这样左右滑动也可以跳转,但是指示器动画不好看
            controller: _pageController, // 控制器
            children: List.generate(_list.length, (index) { // 轮播
              return Container(
                alignment: Alignment.center,
                child: Text(
                  "第${index + 1}轮播图",
                  style: const TextStyle(color: Colors.white),
                ),
              );
            }),
          ),
          Positioned(
              bottom: 10,
              left: 0,
              right: 0,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: List.generate(_list.length, (index) {
                  return GestureDetector(
                    onTap: () {
                     // 点击指示器,跳转到对应的页面
                      _cancelTimer(); // 取消定时器, 重置计时时间
                      _jumpPage(index);
                      setState(() {
                        _currentIndex = index;
                      });
                    },
                    child: Container(
                      margin: const EdgeInsets.only(left: 10),
                      height: 20,
                      width: 20,
                      decoration: BoxDecoration(
                          borderRadius:
                              const BorderRadius.all(Radius.circular(20)),
                          color: _currentIndex == index
                              ? Colors.red
                              : Colors.white), // 指示器颜色判读
                    ),
                  );
                }),
              ))
        ],
      ),
    );
};
相关推荐
火柴就是我16 分钟前
学习一些常用的混合模式之BlendMode. dst
android·flutter
前端不太难37 分钟前
Sliver 为什么能天然缩小 rebuild 影响面
flutter·性能优化·状态模式
带带弟弟学爬虫__2 小时前
Flutter 逆向想学却无从下手?
flutter
行者962 小时前
Flutter跨平台开发:颜色选择器适配OpenHarmony
flutter·harmonyos·鸿蒙
不爱吃糖的程序媛3 小时前
深度解析OpenHarmony跨平台框架生态:RN、Flutter、Cordova、KMP四大方向全梳理
flutter
kirk_wang3 小时前
Flutter艺术探索-Flutter样式系统:TextStyle与主题配置
flutter·移动开发·flutter教程·移动开发教程
火柴就是我3 小时前
Flutter 混合模式下:saveLayer 混合注意点
android·flutter
AiFlutter4 小时前
四、动画图表(03):饼图
flutter·低代码·低代码平台·aiflutter·aiflutter低代码
西西学代码4 小时前
Flutter---通过案例来详细了解状态管理
flutter
LawrenceLan4 小时前
Flutter 零基础入门(八):Dart 类(Class)与对象(Object)
前端·flutter