按下时购物车计数器不更新值,没有显示错误

Posted

技术标签:

【中文标题】按下时购物车计数器不更新值,没有显示错误【英文标题】:Shopping Cart Counter not updating value when pressed, no Error is shown for the matter 【发布时间】:2021-09-19 23:27:01 【问题描述】:

我的应用程序中有一个购物车计数器的小代码,在运行应用程序时,它不会在按下添加或删除按钮(+ 和 - 图标)时更新,尽管我为它们分配了功能,没有错误显示为什么会发生这种情况......

这是计数器的代码:

    import 'package:flutter/material.dart';

class CartCounter extends StatefulWidget 
  @override
  _CartCounterState createState() => _CartCounterState();


class _CartCounterState extends State<CartCounter> 
  int numOfItems = 0;

  @override
  Widget build(BuildContext context) 
    return Row(
      children: <Widget>[
        buildOutlineButton(
          icon: Icons.remove,
          press: () 
            if (numOfItems > 0) 
              setState(() 
                numOfItems--;
              );
            
          ,
        ),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: kDefaultPaddin / 2),
          child: Text(
            // if our item is less  then 10 then  it shows 01 02 like that
            numOfItems.toString().padLeft(2, "0"),
            style: Theme.of(context).textTheme.headline6,
          ),
        ),
        buildOutlineButton(
            icon: Icons.add,
            press: () 
              if (numOfItems < 10) 
                setState(() 
                  numOfItems++;
                );
              
            ),
      ],
    );
  

  SizedBox buildOutlineButton(
      required IconData icon, required Function press) 
    return SizedBox(
      width: 40,
      height: 32,
      child: OutlinedButton(
        style: OutlinedButton.styleFrom(
          padding: EdgeInsets.zero,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(13.0)),
        ),
        onPressed: press(),
        child: Icon(icon),
      ),
    );
  

这是我调用 Cart Counter 类的代码,它还有一个运行良好的评分栏:

    class CounterWithRateBar extends StatefulWidget 
  @override
  _CounterWithRateBarState createState() => _CounterWithRateBarState();


class _CounterWithRateBarState extends State<CounterWithRateBar> 
  // const CounterWithRateBar(
  //   Key? key,
  // ) : super(key: key);
  late double _rating;
  int _ratingBarMode = 1;
  double _initialRating = 2.0;
  IconData? _selectedIcon;

  @override
  void initState() 
    super.initState();
    _rating = _initialRating;
  
  @override
  Widget build(BuildContext context) 
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        CartCounter(),
        Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children:
            <Widget>[
              SizedBox(
                height: 10.0,
              ),
              _ratingBar(_ratingBarMode),
              SizedBox(height: 5.0),
              Text(
                'Rating: $_rating',
                style: TextStyle(fontWeight: FontWeight.bold),
              ),
            ]
        ),
      ],
    );
  

  Widget _ratingBar(int mode) 
    return RatingBar.builder(
      initialRating: _initialRating,
      minRating: 1,
      direction: Axis.horizontal,
      allowHalfRating: true,
      unratedColor: Colors.amber.withAlpha(50),
      itemCount: 5,
      itemSize: 25.0,
      itemPadding: EdgeInsets.symmetric(horizontal: 2.0),
      itemBuilder: (context, _) => Icon(
        _selectedIcon ?? Icons.star,
        color: Colors.amber,
      ),
      onRatingUpdate: (rating) 
        setState(() 
          _rating = rating;
        );
      ,
      updateOnDrag: true,
    );
  

【问题讨论】:

【参考方案1】:

你在 buildOutlineButtonDefinition 中有一个小错误

你立即调用 press 函数而不是使用 is 作为回调;

来自

onPressed: press(),

onPressed: () => press(),

完整的定义是

SizedBox buildOutlineButton(
      required IconData icon, required Function press) 
    return SizedBox(
      width: 40,
      height: 32,
      child: OutlinedButton(
        style: OutlinedButton.styleFrom(
          padding: EdgeInsets.zero,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(13.0)),
        ),
        onPressed: () => press(),
        child: Icon(icon),
      ),
    );
  

【讨论】:

以上是关于按下时购物车计数器不更新值,没有显示错误的主要内容,如果未能解决你的问题,请参考以下文章

Shopify购买按钮最小数量

删除后按后退按钮更新购物车计数

如何从datalist更新数据库表?

text 迷你购物车更新数量计数迷你购物车购物车ajax购物车柜台ajax

Flutter StreamBuilder 错误

如何在颤动中将商品计数从购物车屏幕更新到餐厅详细信息屏幕并返回到餐厅详细信息屏幕?