简体   繁体   English

flutter - 避免整个小部件重建

[英]flutter - Avoiding whole widget rebuild

Trying a 'like button' within a Future builder with many other widgets as below,在 Future 构建器中尝试“喜欢按钮”以及以下许多其他小部件, 在此处输入图像描述

onPressed: () {
    if (aleadyLiked.length > 0) {
        unlike(profileId);
     } else {
        like(profileId);
     }
   setState(() {});
 },

And this is how my future builder starts,这就是我未来的建造者开始的方式,

@override
Widget build(BuildContext context) {
return FutureBuilder(
  future: getProfile(profileId), 
  builder: (context, snapshot) {
  =======Other widgets here======
  }

Issue is onPressed of the like icon-button I am doing the setState() which is causing the whole Future builder to reload, Is there a way just to update the Like Button and the Like count, I was thinking to use some client side counter logic which callbacks to actual DB updates.Please help.问题是类似图标按钮的 onPressed 我正在执行 setState() 导致整个 Future 构建器重新加载,有没有办法更新 Like Button 和 Like 计数,我正在考虑使用一些客户端计数器回调实际数据库更新的逻辑。请帮助。

Loading Profile part on initState() can be achieved, but how to handle updating and reflecting 'Likes', can that Like-button region alone be reloaded?可以在 initState() 上加载配置文件部分,但是如何处理更新和反映“喜欢”,可以单独重新加载喜欢按钮区域吗?

You should not get this User Profile like this, but what you can do rather, you can get the user profile inside the initState, and before data is not loaded you can show either loader of something.您不应该像这样获取此用户配置文件,但您可以做的是,您可以在 initState 中获取用户配置文件,并且在未加载数据之前,您可以显示任何加载程序。

Something like this...像这样的东西...

  User _user;
  
  Future<User> getUserProfile(profileId) async{
    ///Todo : Implementation of get User Profile
  }
  
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getUserProfile("userId").then((user){
      setState(() {
        _user =user;
      });
    })
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: this._user == null ? CircularProgressIndicator() : UserWidget(),
    );
  }

So this is how I finally achieved it,所以这就是我最终实现它的方式,

void initState() {
 getProfile(profileId).then((user){
  setState(() {
    _profile =user;
    _counter =_profile.profilelikes.length;
    _isAlreadyLiked=_profile.allreadyliked.length > 0;
  });
 });
 super.initState();
}

And OnPressed() is而 OnPressed() 是

                                                            onPressed: () {
                                                              if (_isAlreadyLiked) {
                                                                unlike(profileId);
                                                                setState(() {
                                                                  _counter--;
                                                                  _isAlreadyLiked=false;
                                                                });
                                                              } else {
                                                                like(profileId);
                                                                setState(() {
                                                                  _counter++;
                                                                  _isAlreadyLiked=true;
                                                                });
                                                              }
                                                            },

Downside: The likes by other users will reflect on Wiget reload only, which is fine for me, for now.缺点:其他用户的喜欢只会反映在 Wiget 重新加载上,这对我来说很好,目前。

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

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