当前位置:   article > 正文

Flutter新增组件实现有数字页码【商品轮播图】_flutter desktop 页码空间

flutter desktop 页码空间

在pubspec引入flutter_swiper_null_safety: ^1.0.2

swiper主页面:

  1. class MedicineDetail extends StatefulWidget {
  2. const MedicineDetail({Key? key}) : super(key: key);
  3. @override
  4. State<MedicineDetail> createState() => _MedicineDetailState();
  5. }
  6. class _MedicineDetailState extends State<MedicineDetail> {
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. appBar: null,
  11. body: Container(
  12. child: Column(
  13. children: [getMedicineDetailPic()],
  14. ),
  15. ),
  16. );
  17. }
  18. Widget getMedicineDetailPic() {
  19. List swiperDataList = [
  20. "https://img.ivsky.com/img/tupian/li/201810/30/motianlun-005.jpg",
  21. "https://img.ivsky.com/img/tupian/li/201811/06/xiaosongshu-002.jpg",
  22. "https://img.ivsky.com/img/tupian/li/201811/06/ludeng-006.jpg"
  23. ];
  24. return Container(
  25. margin: Zcss.marginTop40,
  26. child: Column(
  27. children: <Widget>[
  28. ConstrainedBox(
  29. constraints: BoxConstraints.loose(
  30. new Size(MediaQuery.of(context).size.width, 280.0)),
  31. child: Swiper(
  32. outer: false,
  33. autoplay: true,
  34. // 分页指示
  35. pagination: buildSwiperPagination(),
  36. autoplayDelay: 3000,
  37. //duration:5000,
  38. itemBuilder: (c, i) {
  39. return CachedNetworkImage(
  40. imageUrl: "${swiperDataList[i]}",
  41. // placeholder: (context, url) => new CircularProgressIndicator(backgroundColor: Colors.red[100],),
  42. errorWidget: (context, url, error) => new Icon(Icons.error),
  43. fit: BoxFit.cover,
  44. );
  45. },
  46. itemCount: swiperDataList == null ? 0 : swiperDataList.length,
  47. )),
  48. ],
  49. ),
  50. );
  51. }
  52. //自定义分页指示器
  53. buildSwiperPagination() {
  54. // 分页指示器
  55. return SwiperPagination(
  56. //指示器显示的位置
  57. alignment: Alignment.bottomRight, // 位置 Alignment.bottomCenter 底部中间
  58. // 距离调整
  59. margin: const EdgeInsets.fromLTRB(0, 0, 30, 21),
  60. // 指示器构建
  61. builder: FractionPaginationWithBackgroundBuilder(
  62. // 选中时字体大小
  63. activeFontSize: 14,
  64. // 字体大小
  65. fontSize: 14,
  66. // 字体颜色
  67. color: Colors.white,
  68. //选中时的颜色
  69. activeColor: Colors.white),
  70. );
  71. }
  72. }

组件页面代码:

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
  3. class FractionPaginationWithBackgroundBuilder extends SwiperPlugin {
  4. ///color ,if set null , will be Theme.of(context).scaffoldBackgroundColor
  5. final Color color;
  6. ///color when active,if set null , will be Theme.of(context).primaryColor
  7. final Color activeColor;
  8. font size
  9. final double fontSize;
  10. ///font size when active
  11. final double activeFontSize;
  12. const FractionPaginationWithBackgroundBuilder(
  13. {required this.color,
  14. this.fontSize: 20.0,
  15. required this.activeColor,
  16. this.activeFontSize: 35.0});
  17. @override
  18. Widget build(BuildContext context, SwiperPluginConfig config) {
  19. ThemeData themeData = Theme.of(context);
  20. Color activeColor = this.activeColor ?? themeData.primaryColor;
  21. Color color = this.color ?? themeData.scaffoldBackgroundColor;
  22. if (Axis.vertical == config.scrollDirection) {
  23. return Container(
  24. padding: EdgeInsets.all(8),
  25. decoration: BoxDecoration(
  26. borderRadius: BorderRadius.all( Radius.circular(10.0)),
  27. image: DecorationImage(
  28. image: ExactAssetImage("images/show_num.png"),
  29. ),
  30. ),
  31. child: Column(
  32. mainAxisSize: MainAxisSize.min,
  33. children: <Widget>[
  34. Text(
  35. "${config.activeIndex + 1}",
  36. style: TextStyle(color: activeColor, fontSize: activeFontSize),
  37. ),
  38. Text(
  39. "/",
  40. style: TextStyle(color: color, fontSize: fontSize),
  41. ),
  42. Text(
  43. "${config.itemCount}",
  44. style: TextStyle(color: color, fontSize: fontSize),
  45. )
  46. ],
  47. ),
  48. );
  49. } else {
  50. return Container(
  51. padding: EdgeInsets.only(top: 3,left: 8,bottom: 3,right: 8),
  52. decoration: BoxDecoration(
  53. color: Colors.grey.withAlpha((200*0.7).toInt()),
  54. borderRadius: BorderRadius.all( Radius.circular(20.0)),
  55. // image: DecorationImage(
  56. // image: ExactAssetImage("assets/images/icon/map_min.png"),
  57. // ),
  58. ),
  59. child: Row(
  60. mainAxisSize: MainAxisSize.min,
  61. children: <Widget>[
  62. Text(
  63. "${config.activeIndex + 1}",
  64. style: TextStyle(color: activeColor, fontSize: activeFontSize),
  65. ),
  66. Text(
  67. " / ${config.itemCount}",
  68. style: TextStyle(color: color, fontSize: fontSize),
  69. )
  70. ],
  71. ),
  72. );
  73. }
  74. }
  75. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号