简体   繁体   English

如何重建特定的小部件属性而不是整个小部件?

[英]How to rebuild a particular widget property instead of the whole widget?

I am making an application and for showing circular progress indicator i had used library of modal_progress_hud and when i set the state of inAsyncCall true or false, it rebuild all of its child widgets inside modal_progress_hud.我正在制作一个应用程序并显示循环进度指示器,我使用了 modal_progress_hud 库,当我将 inAsyncCall 的 state 设置为真或假时,它会在 modal_progress_hud 内重建其所有子小部件。 And i do not want to rebuild all the UI again and again.而且我不想一次又一次地重建所有的 UI。 It decreases the efficency of application by increasing usage of GPU.它通过增加 GPU 的使用来降低应用效率。 Is there any other way to make it better, that we just change the property and rebuild that property of widget only instead of whole screen and widgets inside it.有没有其他方法可以让它变得更好,我们只需更改属性并仅重建小部件的该属性,而不是整个屏幕和其中的小部件。

  bool circularindicator = false;
  Color circularColor;
  double circularOpacity;

              child: ModalProgressHUD(
                inAsyncCall: circularindicator,
                child: body,
                color: circularColor,
                opacity: circularOpacity,
              ),

In body, we have all of the widgets in listview.在正文中,我们在列表视图中拥有所有小部件。 And after pressing the login button we called the showProgress() method and set the state and rebuild the widget.按下登录按钮后,我们调用 showProgress() 方法并设置 state 并重建小部件。

  @override
  showProgress() {
    setState(() {
      circularindicator = true;
      circularOpacity = 0.5;
      circularColor = Colors.grey;
    });
  }

Make your class StatelessWidget and use Provider Package for state management.制作您的 class StatelessWidget并使用Provider Package进行 state 管理。 Inside the widget build function keep the Provider listener false and wrap the changeable widget with Consumer Widget .在小部件内部构建 function 保持Provider侦听器为false并使用Consumer Widget包装可更改的小部件。

 Widget build(BuildContext context) {
  final cart = Provider.of<Cart>(context, listen: false);
  return GridTileBar(
        leading: Consumer<Product>(
          builder: (ctx, product, child) => IconButton(
            onPressed: () {
              product.toggleFavoriteStatus();
            },
            icon: Icon(product.isFavorite ? Icons.favorite : Icons.favorite_border),
            color: Theme.of(context).accentColor,
          ),
        ),
        trailing: IconButton(
          icon: Icon(Icons.shopping_cart),
          onPressed: () {
            cart.addItem(product.id, product.title, product.price);
          },
          color: Theme.of(context).accentColor,
        ),
        backgroundColor: Colors.black87,
        title: Text(product.title),
      );
}

For more details on provider package: State Management Using Provider有关提供程序 package 的更多详细信息: State 使用提供程序进行管理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM