Skip to content

WidgetsApp: Changing supportedLocales fails to update the locale #117210

@Bungeefan

Description

@Bungeefan

When providing supportedLocales to a WidgetsApp (in this case, a MaterialApp), it doesn't update the internal _locale field in didUpdateWidget.

The workaround is to provide a specific locale via the locale field. (as to be seen on line 23)

Steps to Reproduce

  1. Execute flutter run on the code sample
  2. Press button on page
  3. Observe that the text above the button doesn't change.
  4. Uncomment workaround in line 23 and try again.

Expected results: Text respectively the Locale (_locale in WidgetsApp) changes.

Actual results: _locale in WidgetsApp is still the same, nothing changes.

Code sample
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<Locale> supportedLocales = [const Locale("de")];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      // Workaround: Overrides the _locale from WidgetsApp
      // locale: supportedLocales[0],
      supportedLocales: supportedLocales,
      localizationsDelegates: GlobalMaterialLocalizations.delegates,
      routes: {
        '/': (context) => MyHomePage(onPress: () {
              setState(() {
                supportedLocales = [const Locale("en")];
              });
            }),
      },
      initialRoute: "/",
    );
  }
}

class MyHomePage extends StatelessWidget {
  final VoidCallback onPress;

  const MyHomePage({super.key, required this.onPress});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Flutter Demo Home Page")),
      body: Center(
        child: Column(
          children: [
            Text("Current locale: ${Localizations.localeOf(context)}"),
            ElevatedButton(
              onPressed: () => onPress.call(),
              child: const Text("Press me to change locale"),
            ),
          ],
        ),
      ),
    );
  }
}
flutter doctor -v
[✓] Flutter (Channel stable, 3.3.9, on Manjaro Linux 6.0.11-1-MANJARO, locale de_AT.UTF-8)
    • Flutter version 3.3.9 on channel stable at /home/shamader/tools/flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b8f7f1f986 (vor 3 Wochen), 2022-11-23 06:43:51 +0900
    • Engine revision 8f2221fbef
    • Dart version 2.18.5
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /mnt/Daten/tools/Android/Sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /home/shamader/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /usr/bin/chromium

[✗] Linux toolchain - develop for Linux desktop
    • clang version 14.0.6
    ✗ CMake is required for Linux development.
      It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from https://cmake.org/download/
    ✗ ninja is required for Linux development.
      It is likely available from your distribution (e.g.: apt install ninja-build), or can be downloaded from https://github.com/ninja-build/ninja/releases
    • pkg-config version 1.8.0

[✓] Android Studio (version 2021.3)
    • Android Studio at /home/shamader/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335
    • Flutter plugin version 71.1.3
    • Dart plugin version 213.7433
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[!] Android Studio (version 2022.1)
    • Android Studio at /home/shamader/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-1/221.6008.13.2211.9301383
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[!] Android Studio (version 2022.2)
    • Android Studio at /home/shamader/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-2/222.4345.14.2221.9321504
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] Connected device (4 available)
    • SM T500 (mobile)    • R9AR800H8RK      • android-arm64  • Android 12 (API 31)
    • M2103K19PG (mobile) • xwcm955djrrslnpj • android-arm64  • Android 12 (API 31)
    • Linux (desktop)     • linux            • linux-x64      • Manjaro Linux 6.0.11-1-MANJARO
    • Chrome (web)        • chrome           • web-javascript • Chromium 108.0.5359.94 Arch Linux

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 3 categories.

Missing statement location

WidgetsApp Code which is missing an update statement taking supportedLocales into account.

@override
void initState() {
super.initState();
_updateRouting();
_locale = _resolveLocales(WidgetsBinding.instance.platformDispatcher.locales, widget.supportedLocales);
WidgetsBinding.instance.addObserver(this);
}
@override
void didUpdateWidget(WidgetsApp oldWidget) {
super.didUpdateWidget(oldWidget);
_updateRouting(oldWidget: oldWidget);
}

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: internationalizationSupporting other languages or locales. (aka i18n)f: material designflutter/packages/flutter/material repository.found in release: 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.good first issueRelatively approachable for first-time contributorshas reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions