ilteoood / flutter_i18n

I18n made easy, for Flutter!
MIT License
217 stars 57 forks source link

App reloads on every save #206

Closed EricKhoury closed 4 months ago

EricKhoury commented 1 year ago

This is my setup in main Build:

          Locale currentLocale = ref.watch(localeProvider).locale;

          ....

          return GestureDetector(
              onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
              child: ResponsiveSizer(
                builder: (context, orientation, screenType) {
                  return MaterialApp(
                      locale: currentLocale,
                      localizationsDelegates: [
                        FlutterI18nDelegate(
                          translationLoader: FileTranslationLoader(),
                          missingTranslationHandler: (key, locale) {
                            log("--- Missing Key: $key, languageCode: ${locale!.languageCode}");
                          },
                        ),
                        GlobalMaterialLocalizations.delegate,
                        GlobalCupertinoLocalizations.delegate,
                        GlobalWidgetsLocalizations.delegate,
                      ],
                      supportedLocales: [
                        const Locale('en', ''), // English
                        const Locale('sv', ''), // Swedish
                      ],

                      title: 'My app',
                      theme: ThemeData(
                          splashColor: Colors.transparent,
                          visualDensity: VisualDensity.adaptivePlatformDensity,
                          primarySwatch: MyTheme.CompanyColors.primary,
                          fontFamily: 'Poppins'),
                      navigatorKey: _navigationService.navigatorKey,
                      onGenerateRoute: generateRoute,
                      home: widget.child);
                },
              ));
        }

Since I added the FlutterI18nDelegate inside localizationsDelegates, the whole app reloads on every file save. I get rid of the behaviour if I remove the FlutterI18nDelegate. Is there any obvious errors on my part here?

ilteoood commented 4 months ago

The delegate should be created outside the widget tree, to avoid this behaviour. An example can be found here: https://github.com/ilteoood/flutter_i18n/blob/master/example/lib/network_example.dart#L20