【问题标题】:How to 'setState' only to the appBar icons instead of 'notifyListeners'如何仅对 appBar 图标“设置状态”而不是“通知侦听器”
【发布时间】:2021-08-19 18:52:45
【问题描述】:

我有一个带有一个图标的 appBar,这个图标有一个数字,在我更改应用程序中的某些内容后必须更新。我使用的是 notifyListeners(),但是这个命令正在清理我需要的列表,所以我必须在没有 notifyListeners() 的情况下更新 appbar 中的那个数字。

我尝试调用 SetState 但它不起作用.. 有没有办法只更新应用栏?

在我包含更多项目的提供者中:

  void setBadge() {
    _number = number;
    notifyListeners(); // this line I dropped out
  }

应用栏图标小部件:

class AppBarWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<Cart>(
      child: IconButton(
        icon: Icon(Icons.shopping_bag_outlined),
        onPressed: () async {
          Navigator.of(context).pushNamed(ROUTE_CART);
        },
      ),
      builder: (_, cart, child) {
        return BagBadge(
          child: child,
          value: cart.isEmpty ? '' : cart.number.toString(),
        );
      },
    );
  }
}

BagBadge:

class BagBadge extends StatelessWidget {
  final Widget child;
  final String value;

  BagBadge({
    @required this.child,
    @required this.value,
  });

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        child,
        if (value != '')
          Positioned(
            right: value.length < 4 ? 20 : 10,
            top: 30,
            child: Container(
              padding: EdgeInsets.all(value.length < 4 ? 2 : 3),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Theme.of(context).accentColor
              ),
              constraints: BoxConstraints(
                minHeight: 16,
                minWidth: 16,
              ),
              child: Text(
                value,
                textAlign: TextAlign.center,
                style: TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
              ),
            ),
          )
      ],
    );
  }
}

【问题讨论】:

  • 请包含导致您的问题的代码。
  • 查看更新。
  • 我不明白你想要达到什么目的。
  • 我想在没有notifyListeners()的情况下更新 BagBadge 的值
  • 为什么 setState() 对你不起作用?它说什么?

标签: flutter dart setstate


【解决方案1】:

编辑:这仅在您使用有状态小部件时才有效。使用无状态小部件不会显示更改。

你可以试试这样的:

import 'package:flutter/material.dart';

class AppBarWidget extends StatefulWidget {
  @override
  _AppBarWidgetState createState() => _AppBarWidgetState();
}

class _AppBarWidgetState extends State<AppBarWidget> {
  int _appBarValue = 0;

  @override
  Widget build(BuildContext context) {
    return Consumer<Cart>(
      child: IconButton(
        icon: Icon(Icons.shopping_bag_outlined),
        onPressed: () async {
          Navigator.of(context).pushNamed(ROUTE_CART);
        },
      ),
      builder: (_, cart, child) {
        return BagBadge(
          child: child,
          value: _appBarValue == 0 ? '' : '$appBarValue',
        );
      },
    );
  }
}

  setAppBarValue(int value) {
     setState(() { _appBarValue = value; });
  }
}

只要你想改变值,只需调用setAppBarValue()函数。

【讨论】:

  • 请编辑问题并向我们展示代码的外观。 appBar是怎么搭建的,你是怎么尝试改变值的……
  • 也许有办法使用notifyListeners(),但仅适用于图标消费者?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多