• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Flutter商城的类型的计数器

武飞扬头像
篮球与鸡
帮助1

flutter商城类型增加减少产品数量的计数器,

 效果图

学新通

/// 默认数是1
final int count ;

/// 是否显示删除提示框 默认是true
final bool isShowDialog ;

/// 允许最大数量默认是5
final int maxCount;

/// 点击加号的回调方法
final _AddCallBack  addCallBack;

/// 点击减号的回调方法
final  _MzeCallBack  mzeCallBack;

/// 点击提示框删除的回调方法
final _EnterDelete  enterDelete;

直接上代码

  1.  
    import 'package:flutter/material.dart';
  2.  
    import 'package:get/get.dart';
  3.  
    import 'package:bruno/bruno.dart';
  4.  
     
  5.  
    typedef _AddCallBack = void Function(int selectIndex,);
  6.  
    typedef _MzeCallBack = void Function(int selectIndex,);
  7.  
    typedef _EnterDelete = void Function(int selectIndex,);
  8.  
     
  9.  
     
  10.  
    class CounterPage extends StatefulWidget {
  11.  
     
  12.  
    /// 默认数
  13.  
    final int count ;
  14.  
     
  15.  
    /// 是否显示删除提示框
  16.  
    final bool isShowDialog ;
  17.  
     
  18.  
    /// 允许最大数量
  19.  
    final int maxCount;
  20.  
     
  21.  
    /// 点击加号的回调方法
  22.  
    final _AddCallBack addCallBack;
  23.  
    /// 点击减号的回调方法
  24.  
    final _MzeCallBack mzeCallBack;
  25.  
    /// 点击提示框删除的回调方法
  26.  
    final _EnterDelete enterDelete;
  27.  
     
  28.  
    const CounterPage({Key? key,this.count = 1,required this.addCallBack,required this.mzeCallBack,required this.enterDelete,
  29.  
    this.isShowDialog = true,this.maxCount = 5}) : super(key: key);
  30.  
     
  31.  
    @override
  32.  
    _CounterPageState createState() => _CounterPageState();
  33.  
    }
  34.  
     
  35.  
    class _CounterPageState extends State<CounterPage> {
  36.  
     
  37.  
    late int count ;
  38.  
     
  39.  
    @override
  40.  
    void initState() {
  41.  
    // TODO: implement initState
  42.  
    super.initState();
  43.  
    count = widget.count;
  44.  
    }
  45.  
     
  46.  
    @override
  47.  
    Widget build(BuildContext context) {
  48.  
     
  49.  
     
  50.  
    return Container(
  51.  
    child: Row(
  52.  
    children: [
  53.  
    Container(
  54.  
    padding: const EdgeInsets.only(bottom: 9),
  55.  
    child: IconButton(onPressed: (){
  56.  
     
  57.  
    if(count == 1){
  58.  
    if(widget.isShowDialog==true){
  59.  
    BrnDialogManager.showConfirmDialog(context,
  60.  
    cancel: '取消', confirm: '确定',message: '确定要删除',title: '温馨提示',onCancel: (){
  61.  
    Navigator.of(context).pop();
  62.  
    },onConfirm: (){
  63.  
    widget.enterDelete(count);
  64.  
    Navigator.of(context).pop();
  65.  
    }
  66.  
    );
  67.  
    }
  68.  
    count == 1;
  69.  
    return;
  70.  
    }else{
  71.  
    count--;
  72.  
    }
  73.  
    widget.mzeCallBack(count,);
  74.  
     
  75.  
    setState(() {
  76.  
     
  77.  
    });
  78.  
    }, icon: const Icon(Icons.minimize_rounded),iconSize: 15,color: Colors.grey,),
  79.  
    ),
  80.  
    Container(
  81.  
    height: 25,
  82.  
    alignment: Alignment.center,
  83.  
    width: 35,
  84.  
    color: Colors.grey.shade200,
  85.  
    child: Text('$count',style: const TextStyle(fontWeight: FontWeight.w800),),
  86.  
    ),
  87.  
    IconButton(
  88.  
    onPressed: (){
  89.  
    if(count ==widget.maxCount){
  90.  
    return;
  91.  
    }
  92.  
    print(count);
  93.  
    if(count<10){
  94.  
    count ;
  95.  
    }
  96.  
    widget.addCallBack(count);
  97.  
    setState(() {
  98.  
     
  99.  
    });
  100.  
    }, icon: const Icon(Icons.add_rounded),iconSize: 15,color: Colors.grey,),
  101.  
    ],
  102.  
    ),
  103.  
    );
  104.  
    }
  105.  
    }

使用:

  1.  
    CounterPage(addcallback: (c){
  2.  
     
  3.  
    }, mzecallback: (c){
  4.  
     
  5.  
    }, enterDelete: (c){
  6.  
     
  7.  
    }),

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgjeich
系列文章
更多 icon
同类精品
更多 icon
继续加载