使用flutter开发一个简单的轮播图带指示器的组件

使用PageView开发一个带指示器的轮播图组件,当轮播图切换的时候,指示器也会跟着切换,切换到当前轮播图所在的索引时,指示器的背景色会变成蓝色,否则是灰色。使用了一个curIndex变量来记录当前激活的轮播图索引。并使用Stack组件来实现定位布局。

组件代码:

Dart 复制代码
import 'package:flutter/material.dart';

// 轮播图
class Lunbo extends StatefulWidget {
  const Lunbo({super.key});

  @override
  State<Lunbo> createState() => _LunboState();
}

class _LunboState extends State<Lunbo> {
  // 定义轮播图列表
  List<Widget> imgList = [];

  // 记录一下当前图片的索引,激活指示器背景色
  var curIndex = 0;

  void handle = (int cur) {
    print("handle函数");
  };

  @override
  void initState() {
    // TODO: implement initState
    var imgUrls = [
      "https://img-blog.csdnimg.cn/img_convert/b723ea01d277dac4926a936f9b40862c.jpeg",
      "https://img-blog.csdnimg.cn/img_convert/d794e0b76460ea037fc707b01bb1d703.jpeg",
      "https://img-blog.csdnimg.cn/img_convert/f90cd009d24e58857a135cfcd44fe993.jpeg"
    ];
    // 图片列表初始化
    for (int i = 0; i < imgUrls.length; i++) {
      imgList.add(Image.network(
        imgUrls[i],
        fit: BoxFit.fill,
      ));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('轮播图')),
        body: Container(
          // width: double.infinity,
          height: 200,
          child: Stack(
            children: [
              PageView(
                scrollDirection: Axis.horizontal,
                // reverse: true,
                children: imgList,
                onPageChanged: (index) {
                  print("轮播图切换了: ${index}");
                  setState(() {
                    curIndex = index;
                  });
                },
              ),
              Positioned(
                  left: 0,
                  right: 0,
                  bottom: 2,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: List.generate(3, (index) {
                      return Container(
                        width: 20,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            color:
                                curIndex == index ? Colors.blue : Colors.grey),
                        child: Text(
                          "$index",
                          style: TextStyle(
                              color: Colors.white, fontWeight: FontWeight.bold),
                        ),
                      );
                    }).toList(),
                  ))
            ],
          ),
        ));
  }
}
相关推荐
shuangyueliao12 小时前
103 个小游戏的商店素材,我一张图都没手动做:截图 / 封面 / 打包 / 提审全自动
前端
用户27639657651012 小时前
放弃 JWT 后,我给 FastAPI 游戏服务端手撸了一套 token session
javascript
ZhengEnCi12 小时前
前端项目为什么需要一份统一的 theme.css:用 CSS 变量管住全局样式与主题切换
前端·css·前端工程化
wordbaby12 小时前
一次微信小程序 `web-view` 跳转原生页回跳后 H5 白屏的排查与修复(Hash 路由污染)
前端
用户7218804383512 小时前
密码骰子:结合AIUI在眼镜上生成一串16位密码
前端
江华森12 小时前
全链路可观测落地记:kube-prometheus-stack 在 K3s 上的 8 个坑与一整套修复脚本
前端
用户842981424181012 小时前
从 JavaScript 到可运行的字节码 - ByteJS 使用指南
javascript
deli00712 小时前
27 寸 4K 开 125% 为什么发虚?PPI 和缩放一次算清
前端
广州华水科技12 小时前
当大坝面临风险时,如何有效实施单北斗GNSS变形监测?
前端
学以智用13 小时前
wangEditor V5 精简完整参考手册
javascript