From 7adfee6f3e1ae71adfbca50e56cd50662ae190a3 Mon Sep 17 00:00:00 2001 From: Eduardo Azevedo Date: Tue, 5 Dec 2023 23:21:28 -0300 Subject: [PATCH] Adding default option to lang --- lib/src/core/data/enums/supported_languages.dart | 7 +++++-- .../core/presentation/controllers/app_controller.dart | 9 +++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/src/core/data/enums/supported_languages.dart b/lib/src/core/data/enums/supported_languages.dart index c3c2f1f..dba6301 100644 --- a/lib/src/core/data/enums/supported_languages.dart +++ b/lib/src/core/data/enums/supported_languages.dart @@ -1,8 +1,8 @@ import 'package:flutter/cupertino.dart'; -import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; enum SupportedLanguages { + system, english, portuguese, } @@ -10,6 +10,7 @@ enum SupportedLanguages { extension SupportedLanguagesExtension on SupportedLanguages { Widget get icon { return switch (this) { + SupportedLanguages.system => const Icon(CupertinoIcons.settings), SupportedLanguages.english => Image.asset( 'assets/icons/languages/en.png', width: 24, @@ -23,13 +24,15 @@ extension SupportedLanguagesExtension on SupportedLanguages { String title(BuildContext context) { return switch (this) { + SupportedLanguages.system => AppLocalizations.of(context)!.system, SupportedLanguages.english => AppLocalizations.of(context)!.english, SupportedLanguages.portuguese => AppLocalizations.of(context)!.portuguese, }; } - Locale get locale { + Locale? get locale { return switch (this) { + SupportedLanguages.system => null, SupportedLanguages.english => const Locale('en'), SupportedLanguages.portuguese => const Locale('pt'), }; diff --git a/lib/src/core/presentation/controllers/app_controller.dart b/lib/src/core/presentation/controllers/app_controller.dart index e868c28..0b9920d 100644 --- a/lib/src/core/presentation/controllers/app_controller.dart +++ b/lib/src/core/presentation/controllers/app_controller.dart @@ -19,8 +19,13 @@ abstract class AppControllerBase with Store { SupportedLanguages? _selectedLanguage; SupportedLanguages? get selectedLanguage => _selectedLanguage; void changeLanguage(SupportedLanguages? language) { - _selectedLanguage = language; - _storage.saveLanguage(language); + if (language == SupportedLanguages.system) { + _selectedLanguage = null; + _storage.saveLanguage(null); + } else { + _selectedLanguage = language; + _storage.saveLanguage(language); + } } @observable