1

In my Flutter project, to improve performance I created many const widgets, these widgets will not rebuild when their parent widgets rebuild.

But after the user changes language of the app, I need to rebuild the whole app to apply the text changes.

Is there a way to force the app to completely rebuild? Thanks, any advice would be appreciated.

leodriesch
  • 5,308
  • 5
  • 30
  • 52
Michael
  • 415
  • 6
  • 16
  • There is, that is `Inheritedwidget`. Did you use `Localization` widget or made everything yourself? Because that widget provided by Flutter already handles everything – Rémi Rousselet Nov 23 '18 at 07:44
  • High is an adjective and cannot be used in the context: "to high performance". You should use "to improve performance" instead – leodriesch Nov 23 '18 at 14:31
  • Thank you @RémiRousselet, I made everything by myself. `Inheritedwidget` looks like not working for my situation. – Michael Nov 24 '18 at 13:43
  • I am sorry for my poor English, thank you @01leo – Michael Nov 24 '18 at 13:45

2 Answers2

5

'Inheritedwidget` is a solution to redraw any widget when the passed value change. Even stateless and const widgets.

For translations for example, flutter already provides an InheritedWidget, which you can bind to using Localizations.of method

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
4

yes there is a way to do it. you have to do full restart (now it's called Hot Restart) in the code, the way to do it is to put your App inside a static widget(why static? because it'll be created once just to avoid the null or anything like that). and when you want to do full restart, just perform a hot-reload in that widget, after that it'll restart your app. you can use it from everywhere

here is the way :

1- first in main.dart, put the your App inside the Restart widget :

import 'package:flutter/material.dart';
import 'home.dart';

void main() {
  runApp(new HotRestartController(
    child: new MyApp()
  ));
}

2- write your hotRestartController inside the file :

class HotRestartController extends StatefulWidget {
  final Widget child;

  HotRestartController({this.child});

  static performHotRestart(BuildContext context) {
    final _HotRestartControllerState state = context.ancestorStateOfType(const TypeMatcher<_HotRestartControllerState>());
    state.performHotRestart();
  }

  @override
  _HotRestartControllerState createState() => new _HotRestartControllerState();
}

class _HotRestartControllerState extends State<HotRestartController> {
  Key key = new UniqueKey();

  void performHotRestart() {
    this.setState(() {
      key = new UniqueKey();
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      key: key,
      child: widget.child,
    );
  }
}

3- anytime, anywhere you can import the main.dart, and call "performHotRestart" using:

HotRestartController.restartApp(context)

have fun !

  • That's not what the OP is asking for – Rémi Rousselet Nov 23 '18 at 07:44
  • he is asking for "a way to force to rebuild the whole app", this is the way – Yousuf AL-Mawali Nov 23 '18 at 07:52
  • He's asking for a way to update all the `const` widget that depends on translations, not to _hard reset_ everything. Or else this is a duplicate question and I'll close it. – Rémi Rousselet Nov 23 '18 at 08:02
  • 1
    The answer you provided already exists here https://stackoverflow.com/questions/50115311/flutter-how-to-force-an-application-restart-in-production-mode/50116077#50116077. It's literally the same code – Rémi Rousselet Nov 23 '18 at 08:04
  • ancestorStateOfType is deprecated, please replace it with : final _HotRestartControllerState state = context.findAncestorStateOfType<_HotRestartControllerState>(); – AVEbrahimi Jul 13 '20 at 06:06
  • Even though this is not what OP is asking for, I am glad I saw this answer as it opened to me another opportunity to solve this issue, via Remi's other answer here: https://stackoverflow.com/questions/50115311/flutter-how-to-force-an-application-restart-in-production-mode/50116077#50116077 – om-ha Sep 13 '22 at 08:17