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

Shopping Cart Counter not updating value when pressed, no Error is shown for the matter

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

这是计数器的代码:

    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),
      ),
    );
  }
}

这是我调用购物车计数器 class 的代码,它还有一个运行良好的评级栏:

    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,
    );
  }
}

您在 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),
      ),
    );
  }