How to force Flutter to rebuild / redraw all widgets?

后端 未结 9 1531
攒了一身酷
攒了一身酷 2020-12-03 06:32

Is there a way to force Flutter to redraw all widgets (e.g. after locale change)?

相关标签:
9条回答
  • 2020-12-03 06:54

    Simply Use:

    Navigator.popAndPushNamed(context,'/screenname');
    

    Whenever you need to refresh :)

    0 讨论(0)
  • 2020-12-03 06:57

    Refreshing the whole widget tree might be expensive and when you do it in front of the users eyes that wouldn't seem sweet.

    so for this purpose flutter has ValueListenableBuilder<T> class. It allows you to rebuild only some of the widgets necessary for your purpose and skip the expensive widgets.

    you can see the documents here ValueListenableBuilder flutter docs
    or just the sample code below:

      return Scaffold(
      appBar: AppBar(
        title: Text(widget.title)
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('You have pushed the button this many times:'),
            ValueListenableBuilder(
              builder: (BuildContext context, int value, Widget child) {
                // This builder will only get called when the _counter
                // is updated.
                return Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Text('$value'),
                    child,
                  ],
                );
              },
              valueListenable: _counter,
              // The child parameter is most helpful if the child is
              // expensive to build and does not depend on the value from
              // the notifier.
              child: goodJob,
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.plus_one),
        onPressed: () => _counter.value += 1,
      ),
    );
    

    And also never forget the power of setState(() {});

    0 讨论(0)
  • 2020-12-03 07:01

    Old question, but here is the solution:

    In your build method, call the rebuildAllChildren function and pass it the context:

    @override
    Widget build(BuildContext context) { 
      rebuildAllChildren(context);
      return ...
    }
    
    void rebuildAllChildren(BuildContext context) {
      void rebuild(Element el) {
        el.markNeedsBuild();
        el.visitChildren(rebuild);
      }
      (context as Element).visitChildren(rebuild);
    }
    

    This will visit all children and mark them as needing to rebuild. If you put this code in the topmost widget in your widgets tree, it will rebuild everything.

    Also note you must order that specific widget to rebuild. Also you could have some boolean so that the rebuild of that widget only rebuilds all of its children when you really need it (it's an expensive operation, of course).


    IMPORTANT: This is a hack, and you should only do this if you know what you are doing, and have strong reason to do so. One example where this is necessary is in my internationalization package: i18_extension. As Collin Jackson explained in his answer, you are really not supposed to do this in general.

    0 讨论(0)
  • 2020-12-03 07:05

    What might work for your use case is using the Navigator to reload the page. I do this when switching between "real" and "demo" mode in my app. Here's an example :

    Navigator.of(context).push(
        new MaterialPageRoute(
            builder: (BuildContext context){
              return new SplashPage();
            }
        )
    );
    

    You can replace "new SplashPage()" in the above code with whatever main widget (or screen) you would like to reload. This code can be called from anywhere you have access to a BuildContext (which is most places in the UI).

    0 讨论(0)
  • 2020-12-03 07:08

    If you are creating a multi lang app I'd suggest you be using Localization Library to handle that. Even using conventions used by other applications.

    flutter_i18n - It also has a method to regenerate over runtime - documented at the bottom

    await FlutterI18n.refresh(buildContext, languageCode, {countryCode});
    

    What it does is generating widgets with state that is managed by a combined singelton resource that handles both dynamic data and visibility.

    0 讨论(0)
  • 2020-12-03 07:09

    Your Widget should have a setState() method, everytime this method is called, the widget is redrawn.

    Documentation : Widget setState()

    0 讨论(0)
提交回复
热议问题