在Flutter上更新UI的一部分 [英] Update a part of the UI on Flutter

查看:76
本文介绍了在Flutter上更新UI的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要更新整个窗口小部件很容易,我们调用 SetState()并进行更改,但是如果我不需要更新所有内容怎么办?

To update the whole widget is easy, we call SetState() and we do our changes but what happens if I don't need to update everything?

我有这个用户界面:

当我加载该页面时,我会从我的应用程序提供商那里获取元素数组:

When I load that page I get my array of elements from my app provider:

Widget build(BuildContext context) {
   var elements = shuffle(Provider.of<GameSettings>(context).set.elements);

(从以下答案中获取: Dart中的List.shuffle()?)

(took it from this answer: List.shuffle() in Dart?)

我需要 context 来获取保存的元素列表,这就是为什么我将其放在此处.

I need the context to get my saved list of elements, that's why I put it there.

我也有需要刷新的进度条:

I also have the progress bar which I need to refresh:

void _startTimer() {
    var sub = countDownTimer.listen(null);
    sub.onData((duration) {
      setState(() {
        _current = _start + duration.elapsed.inSeconds;
      });
    });

    sub.onDone(() {
      sub.cancel();
    });
  }
}

但是当我刷新它时,我也在刷新我的元素.发生这种情况是因为整个小部件都被调用了,所以我的 shuffle 函数.

But when I refresh it, I'm also refreshing my element. This is happening because the whole widget is being called and so my shuffle function.

我尝试将进度按钮移动到其他小部件,但仍然相同.我如何才能刷新此进度?

I tried moving the progress button to a different widget, but still the same. How could I just refresh this progress?

推荐答案

您可以使用StatefulBuilder()

You can use StatefulBuilder()

您的代码将为:

void _startTimer() {
    StatefulBuilder(
        builder: (BuildContext context, setState){
           var sub = countDownTimer.listen(null);
           sub.onData((duration) {
              setState(() {
                   _current = _start + duration.elapsed.inSeconds;
                });
            });

            sub.onDone(() {
             sub.cancel();
            });
         }
     )

 }

这篇关于在Flutter上更新UI的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆