getx icon indicating copy to clipboard operation
getx copied to clipboard

Black screen when locale changed

Open Remering opened this issue 3 years ago • 0 comments

Describe the bug Screen went black when locale changed. It only occurred when using GetMaterialApp.router.

Reproduction code example:

class LocaledButton extends GetView<LocaleController> {
  const LocaledButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: () {
          if (controller.locale()?.languageCode == 'en') {
            controller.locale(const Locale('zh', 'Hans'));
          } else {
            controller.locale(const Locale('en'));
          }
        },
        child: Text(LocaleKeys.hello_world.tr));
  }
}

To Reproduce Steps to reproduce the behavior:

  1. Press center button
  2. Get a black screen

Expected behavior No black screen

Screenshots GIF

Flutter Version: 3.0.4

Getx Version: 4.6.5

Describe on which device you found the bug: Windows 10 (21H1)

Minimal reproduce code Provide a minimum reproduction code for the problem

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_demo/generated/locales.g.dart';

class LocaleController extends GetxController {
  final Rx<Locale?> locale = Get.deviceLocale.obs;

  @override
  void onInit() {
    super.onInit();
    ever(locale, (Locale? newLocale) {
      if (newLocale == null) return;
      Get.updateLocale(newLocale);
    });
  }
}

void main(List<String> args) {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final LocaleController controller = Get.put(LocaleController());
    return GetMaterialApp.router(
      locale: controller.locale(),
      translationsKeys: AppTranslation.translations,
      routeInformationParser: GetInformationParser(initialRoute: '/home'),
      routerDelegate: GetDelegate(),
      getPages: routes,
    );
  }
}

final routes = [GetPage(name: '/home', page: () => const HomeView())];

class HomeView extends StatelessWidget {
  const HomeView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        body: const Center(
          child: LocaledButton(),
        ));
  }
}

class LocaledButton extends GetView<LocaleController> {
  const LocaledButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: () {
          if (controller.locale()?.languageCode == 'en') {
            controller.locale(const Locale('zh', 'Hans'));
          } else {
            controller.locale(const Locale('en'));
          }
        },
        child: Text(LocaleKeys.hello_world.tr));
  }
}

Remering avatar Jul 12 '22 09:07 Remering