From 909560d6940aa5e5c14b31345b21f6424cbc0ca7 Mon Sep 17 00:00:00 2001 From: allan Date: Thu, 30 Apr 2020 22:40:29 -0300 Subject: [PATCH 1/6] resolved #67 --- lib/app/app_widget.dart | 2 +- .../country_page_stagger_animation.dart | 6 ++-- .../settings/global_settings_controller.dart | 7 ++-- .../global_settings_controller.g.dart | 6 ++-- lib/app/modules/settings/settings_page.dart | 3 +- .../world/world_page_stagger_animation.dart | 9 +++-- lib/app/shared/config/config.dart | 14 ++++---- lib/app/shared/utils/theme/constants.dart | 15 +++++---- lib/app/shared/utils/theme/extra_pallete.dart | 10 ++++++ .../shared/utils/theme/theme_interface.dart | 25 ++++++++++++++ lib/app/shared/utils/theme/theme_utils.dart | 21 +++++++----- .../shared/utils/theme/themes/theme_dark.dart | 27 +++++++++++++++ .../utils/theme/themes/theme_dracula.dart | 33 +++++++++++++++++++ .../utils/theme/themes/theme_light.dart | 26 +++++++++++++++ 14 files changed, 168 insertions(+), 36 deletions(-) create mode 100644 lib/app/shared/utils/theme/extra_pallete.dart create mode 100644 lib/app/shared/utils/theme/theme_interface.dart create mode 100644 lib/app/shared/utils/theme/themes/theme_dark.dart create mode 100644 lib/app/shared/utils/theme/themes/theme_dracula.dart create mode 100644 lib/app/shared/utils/theme/themes/theme_light.dart diff --git a/lib/app/app_widget.dart b/lib/app/app_widget.dart index 0e2d792..4141bdf 100644 --- a/lib/app/app_widget.dart +++ b/lib/app/app_widget.dart @@ -23,7 +23,7 @@ class _AppWidgetState extends State { return MaterialApp( navigatorKey: Modular.navigatorKey, title: 'Flutter Slidy', - theme: controller.globalSettingsController.theme, + theme: controller.globalSettingsController.theme.themeData, initialRoute: '/', onGenerateRoute: Modular.generateRoute, ); diff --git a/lib/app/modules/country/country_page_stagger_animation.dart b/lib/app/modules/country/country_page_stagger_animation.dart index 11c6d10..72ca239 100644 --- a/lib/app/modules/country/country_page_stagger_animation.dart +++ b/lib/app/modules/country/country_page_stagger_animation.dart @@ -54,7 +54,7 @@ class CoutryPageStaggerAnimation extends StatelessWidget { title: "Número de Mortes", todayNum: "${info.todayDeaths}", percentageBadge: PercentageBadgeWidget( - color: Color(0xffFF5656), + color: appController.globalSettingsController.theme.extraPallete.error, percentage: (info.deaths) / (info.cases), ), ), @@ -63,7 +63,7 @@ class CoutryPageStaggerAnimation extends StatelessWidget { number: "${info.recovered}", title: "Pacientes recuperados", percentageBadge: PercentageBadgeWidget( - color: Color(0xff40CA87), + color: appController.globalSettingsController.theme.extraPallete.success, percentage: (info.recovered) / (info.cases), ), ), @@ -72,7 +72,7 @@ class CoutryPageStaggerAnimation extends StatelessWidget { number: "${info.critical}", title: "Pacientes em estado grave", percentageBadge: PercentageBadgeWidget( - color: Color(0xffDBC716), + color: appController.globalSettingsController.theme.extraPallete.warning, percentage: (info.critical) / (info.cases), ), ), diff --git a/lib/app/modules/settings/global_settings_controller.dart b/lib/app/modules/settings/global_settings_controller.dart index c4ba581..8c43231 100644 --- a/lib/app/modules/settings/global_settings_controller.dart +++ b/lib/app/modules/settings/global_settings_controller.dart @@ -1,5 +1,6 @@ import 'package:corona_data/app/shared/models/country_model.dart'; import 'package:corona_data/app/shared/repositories/local_storage_interface.dart'; +import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; import 'package:corona_data/app/shared/utils/theme/theme_utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter_modular/flutter_modular.dart'; @@ -20,10 +21,10 @@ abstract class _GlobalSettingsControllerBase with Store { ObservableFuture country; @computed - ThemeData get theme { - return ThemeUtils.getThemeData(themeName?.value); + ITheme get theme { + return ThemeUtils.getTheme(themeName?.value); } - + @computed bool get isReady { return themeName.value != null && country.value != null; diff --git a/lib/app/modules/settings/global_settings_controller.g.dart b/lib/app/modules/settings/global_settings_controller.g.dart index ce05a4d..b7fc801 100644 --- a/lib/app/modules/settings/global_settings_controller.g.dart +++ b/lib/app/modules/settings/global_settings_controller.g.dart @@ -9,11 +9,11 @@ part of 'global_settings_controller.dart'; // ignore_for_file: non_constant_identifier_names, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic mixin _$GlobalSettingsController on _GlobalSettingsControllerBase, Store { - Computed _$themeComputed; + Computed _$themeComputed; @override - ThemeData get theme => - (_$themeComputed ??= Computed(() => super.theme)).value; + ITheme get theme => + (_$themeComputed ??= Computed(() => super.theme)).value; Computed _$isReadyComputed; @override diff --git a/lib/app/modules/settings/settings_page.dart b/lib/app/modules/settings/settings_page.dart index 34eda87..7f47d05 100644 --- a/lib/app/modules/settings/settings_page.dart +++ b/lib/app/modules/settings/settings_page.dart @@ -4,7 +4,6 @@ import 'package:corona_data/app/modules/settings/widgets/theme_dropdown.dart'; import 'package:corona_data/app/shared/models/country_model.dart'; import 'package:corona_data/app/shared/utils/constants.dart'; import 'package:corona_data/app/shared/utils/snackbar_util.dart'; -import 'package:corona_data/app/shared/utils/theme/constants.dart'; import 'package:corona_data/app/shared/utils/widgets/custom_divider.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; @@ -47,7 +46,7 @@ class _SettingsPageState (value) { snackbar.enqueueMessage( message: 'Settings has been changed!', - color: ThemeColors.success, + color: appController.globalSettingsController.theme.extraPallete.success, id: "SettingsForm"); }); diff --git a/lib/app/modules/world/world_page_stagger_animation.dart b/lib/app/modules/world/world_page_stagger_animation.dart index 24e5f76..af71732 100644 --- a/lib/app/modules/world/world_page_stagger_animation.dart +++ b/lib/app/modules/world/world_page_stagger_animation.dart @@ -1,3 +1,4 @@ +import 'package:corona_data/app/app_controller.dart'; import 'package:corona_data/app/shared/models/info_model.dart'; import 'package:corona_data/app/shared/utils/constants.dart'; import 'package:corona_data/app/shared/utils/widgets/summary_header_widget.dart'; @@ -6,10 +7,12 @@ import 'package:corona_data/app/shared/widgets/animations/virus_circular_animati import 'package:corona_data/app/shared/widgets/info_tile_widget.dart'; import 'package:corona_data/app/shared/widgets/percentage_badge_widget.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_modular/flutter_modular.dart'; class WorldPageStaggerAnimation extends StatelessWidget { final AnimationController controller; final InfoModel info; + final AppController appController = Modular.get(); WorldPageStaggerAnimation({Key key, this.controller, this.info}) : super(key: key) { @@ -50,7 +53,7 @@ class WorldPageStaggerAnimation extends StatelessWidget { todayNum: "${info.todayDeaths}", title: "Número de Mortes", percentageBadge: PercentageBadgeWidget( - color: Color(0xffFF5656), + color: appController.globalSettingsController.theme.extraPallete.error, percentage: (info.deaths) / (info.cases), ), ), @@ -59,7 +62,7 @@ class WorldPageStaggerAnimation extends StatelessWidget { number: "${info.recovered}", title: "Pacientes recuperados", percentageBadge: PercentageBadgeWidget( - color: Color(0xff40CA87), + color: appController.globalSettingsController.theme.extraPallete.success, percentage: (info.recovered) / (info.cases), ), ), @@ -68,7 +71,7 @@ class WorldPageStaggerAnimation extends StatelessWidget { number: "${info.critical}", title: "Pacientes em estado grave", percentageBadge: PercentageBadgeWidget( - color: Color(0xffDBC716), + color: appController.globalSettingsController.theme.extraPallete.warning, percentage: (info.critical) / (info.cases), ), ), diff --git a/lib/app/shared/config/config.dart b/lib/app/shared/config/config.dart index 9da662d..10a2a46 100644 --- a/lib/app/shared/config/config.dart +++ b/lib/app/shared/config/config.dart @@ -1,9 +1,11 @@ -import 'package:corona_data/app/shared/utils/theme/constants.dart'; -import 'package:flutter/material.dart'; +import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; +import 'package:corona_data/app/shared/utils/theme/themes/theme_dark.dart'; +import 'package:corona_data/app/shared/utils/theme/themes/theme_dracula.dart'; +import 'package:corona_data/app/shared/utils/theme/themes/theme_light.dart'; -final Map themes = { - "light": themeLight, - "dark": themeDark, - "dracula": themeDracula, +final Map themes = { + "light": ThemeLight(), + "dark": ThemeDark(), + "dracula": ThemeDracula(), }; diff --git a/lib/app/shared/utils/theme/constants.dart b/lib/app/shared/utils/theme/constants.dart index 78dff87..cef0345 100644 --- a/lib/app/shared/utils/theme/constants.dart +++ b/lib/app/shared/utils/theme/constants.dart @@ -36,12 +36,13 @@ final ThemeData themeDracula = ThemeData( buttonTheme: ButtonThemeData(buttonColor: Color(0xfff1fa8c)), buttonColor: Color(0xfff1fa8c), secondaryHeaderColor: Color(0xff44475a), + ); -class ThemeColors{ - const ThemeColors._(); - static const Color success = Colors.green; - static const Color error = Colors.red; - static const Color warning = Colors.orange; - static const Color info = Colors.blue; -} \ No newline at end of file +// class ThemeColors{ +// const ThemeColors._(); +// static const Color success = Colors.green; +// static const Color error = Colors.red; +// static const Color warning = Colors.orange; +// static const Color info = Colors.blue; +// } \ No newline at end of file diff --git a/lib/app/shared/utils/theme/extra_pallete.dart b/lib/app/shared/utils/theme/extra_pallete.dart new file mode 100644 index 0000000..c727f1e --- /dev/null +++ b/lib/app/shared/utils/theme/extra_pallete.dart @@ -0,0 +1,10 @@ +import 'package:flutter/material.dart'; + +class ExtraPallete { + final Color success; + final Color info; + final Color warning; + final Color error; + + ExtraPallete({this.success, this.info, this.warning, this.error}); +} \ No newline at end of file diff --git a/lib/app/shared/utils/theme/theme_interface.dart b/lib/app/shared/utils/theme/theme_interface.dart new file mode 100644 index 0000000..42346a4 --- /dev/null +++ b/lib/app/shared/utils/theme/theme_interface.dart @@ -0,0 +1,25 @@ +import 'package:corona_data/app/shared/utils/theme/extra_pallete.dart'; +import 'package:flutter/material.dart'; + +abstract class ITheme { + ThemeData themeData; + ExtraPallete extraPallete; + + ThemeData getThemeData(); + ExtraPallete getExtraPallete(); + + ITheme initialize(){ + this.themeData = getThemeData(); + this.extraPallete = getExtraPallete(); + + return this; + } +} + + + + + + + + diff --git a/lib/app/shared/utils/theme/theme_utils.dart b/lib/app/shared/utils/theme/theme_utils.dart index 863c7f1..150884b 100644 --- a/lib/app/shared/utils/theme/theme_utils.dart +++ b/lib/app/shared/utils/theme/theme_utils.dart @@ -1,18 +1,23 @@ import 'package:corona_data/app/shared/config/config.dart'; -import 'package:flutter/material.dart'; +import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; class ThemeUtils { const ThemeUtils._(); - static ThemeData getThemeData(String theme) { - if(theme == null){ - return themes['dark']; + static ITheme getTheme(String themeName) { + ITheme themeInstance; + themeName = themeName != null?themeName.toLowerCase():''; + + if(themeName == null){ + themeInstance = themes['dark']; } - theme=theme.toLowerCase(); - if (themes.containsKey(theme) || theme == null) { - return themes[theme]; + + + else if (themes.containsKey(themeName)) { + themeInstance = themes[themeName]; } else { - return themes['dark']; + themeInstance = themes['dark']; } + return themeInstance.initialize(); } static List getThemeNamesPretty(){ diff --git a/lib/app/shared/utils/theme/themes/theme_dark.dart b/lib/app/shared/utils/theme/themes/theme_dark.dart new file mode 100644 index 0000000..ff1c625 --- /dev/null +++ b/lib/app/shared/utils/theme/themes/theme_dark.dart @@ -0,0 +1,27 @@ +import 'package:corona_data/app/shared/utils/theme/extra_pallete.dart'; +import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; +import 'package:flutter/material.dart'; + +class ThemeDark extends ITheme { + + @override + ExtraPallete getExtraPallete() { + return ExtraPallete( + success: Color(0xff40CA87), + error: Color(0xffFF5656), + warning: Color(0xffDBC716), + info: Colors.blue); + } + + @override + ThemeData getThemeData() { + return ThemeData( + primaryColor: Color(0xff393e3b), + primaryColorLight: Color(0xff9C9F98), + primaryColorDark: Color(0xff231F1C), + accentColor: Color(0xffEBE9E7), + canvasColor: Color(0xff393e3b), + backgroundColor: Color(0xff231F1C), + secondaryHeaderColor: Color(0xff2C2F26)); + } +} \ No newline at end of file diff --git a/lib/app/shared/utils/theme/themes/theme_dracula.dart b/lib/app/shared/utils/theme/themes/theme_dracula.dart new file mode 100644 index 0000000..53dde4d --- /dev/null +++ b/lib/app/shared/utils/theme/themes/theme_dracula.dart @@ -0,0 +1,33 @@ +import 'package:corona_data/app/shared/utils/theme/extra_pallete.dart'; +import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; +import 'package:flutter/material.dart'; + +class ThemeDracula extends ITheme { + @override + ExtraPallete getExtraPallete() { + return ExtraPallete( + success: Color(0xff50fa7b), + error: Color(0xffff5555), + warning: Color(0xffffb86c), + info: Color(0xff8be9fd)); + } + + @override + ThemeData getThemeData() { + return ThemeData( + primaryColor: Color(0xff44475a), + primaryColorLight: Color(0xffbd93f9), + primaryColorDark: Color(0xff282a36), + accentColor: Color(0xfff8f8f2), + canvasColor: Color(0xff282a36), + backgroundColor: Color(0xff282a36), + errorColor: Color(0xffff5555), + iconTheme: IconThemeData(color: Color(0xfff8f8f2)), + primaryIconTheme: IconThemeData(color: Color(0xfff8f8f2)), + accentIconTheme: IconThemeData(color: Color(0xfff8f8f2)), + buttonTheme: ButtonThemeData(buttonColor: Color(0xfff1fa8c)), + buttonColor: Color(0xfff1fa8c), + secondaryHeaderColor: Color(0xff44475a), + ); + } +} \ No newline at end of file diff --git a/lib/app/shared/utils/theme/themes/theme_light.dart b/lib/app/shared/utils/theme/themes/theme_light.dart new file mode 100644 index 0000000..41d6a97 --- /dev/null +++ b/lib/app/shared/utils/theme/themes/theme_light.dart @@ -0,0 +1,26 @@ +import 'package:corona_data/app/shared/utils/theme/extra_pallete.dart'; +import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; +import 'package:flutter/material.dart'; + +class ThemeLight extends ITheme { + @override + ExtraPallete getExtraPallete() { + return ExtraPallete( + success: Colors.green, + error: Colors.red, + warning: Colors.orange, + info: Colors.blue); + } + + @override + ThemeData getThemeData() { + return ThemeData( + primaryColor: Color(0xffdcddda), + primaryColorLight: Color(0xff83867e), + primaryColorDark: Color(0xffEBE9E7), + accentColor: Color(0xff231F1C), + backgroundColor: Color(0xffEBE9E7), + buttonColor: Color(0xff231F1C), + secondaryHeaderColor: Color(0xffcfd0cd)); + } +} \ No newline at end of file From 902126730fa177e80e37f8dbede239995b590e65 Mon Sep 17 00:00:00 2001 From: allan Date: Fri, 1 May 2020 12:52:11 -0300 Subject: [PATCH 2/6] Adjusting Localization on App --- lib/app/app_widget.dart | 21 ++++++++++++++++++- lib/app/modules/country/country_page.dart | 2 +- .../country_page_stagger_animation.dart | 2 +- .../translations/country_page.i18n.dart | 12 +++++++++++ lib/app/modules/home/home_controller.dart | 7 +++++-- lib/app/modules/home/home_page.dart | 13 +++++++++--- .../home/translations/home_page.i18n.dart | 16 ++++++++++++++ pubspec.lock | 21 ++++++++++++++++++- pubspec.yaml | 8 +++++-- 9 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 lib/app/modules/country/translations/country_page.i18n.dart create mode 100644 lib/app/modules/home/translations/home_page.i18n.dart diff --git a/lib/app/app_widget.dart b/lib/app/app_widget.dart index 4141bdf..f398752 100644 --- a/lib/app/app_widget.dart +++ b/lib/app/app_widget.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_modular/flutter_modular.dart'; +import 'package:i18n_extension/i18n_widget.dart'; import 'package:mobx/mobx.dart'; import 'app_controller.dart'; @@ -22,10 +24,27 @@ class _AppWidgetState extends State { Widget build(BuildContext context) { return MaterialApp( navigatorKey: Modular.navigatorKey, - title: 'Flutter Slidy', + title: 'C19 Tracker', theme: controller.globalSettingsController.theme.themeData, initialRoute: '/', onGenerateRoute: Modular.generateRoute, + localizationsDelegates: [ + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: [ + const Locale('en', "US"), + const Locale('pt', "BR"), + ], + builder: (context,widget){ + + return I18n( + initialLocale: (Locale("pt","BR")), + child:widget + ); + }, + ); } } diff --git a/lib/app/modules/country/country_page.dart b/lib/app/modules/country/country_page.dart index 3a18ef8..62cce71 100644 --- a/lib/app/modules/country/country_page.dart +++ b/lib/app/modules/country/country_page.dart @@ -6,9 +6,9 @@ import 'package:corona_data/app/shared/widgets/try_again_widget.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_modular/flutter_modular.dart'; - import 'country_controller.dart'; + class CountryPage extends StatefulWidget { final String title; final AnimationController controller; diff --git a/lib/app/modules/country/country_page_stagger_animation.dart b/lib/app/modules/country/country_page_stagger_animation.dart index 72ca239..9732b35 100644 --- a/lib/app/modules/country/country_page_stagger_animation.dart +++ b/lib/app/modules/country/country_page_stagger_animation.dart @@ -9,7 +9,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_modular/flutter_modular.dart'; import '../../app_controller.dart'; - +import "translations/country_page.i18n.dart"; class CoutryPageStaggerAnimation extends StatelessWidget { final AnimationController controller; final InfoModel info; diff --git a/lib/app/modules/country/translations/country_page.i18n.dart b/lib/app/modules/country/translations/country_page.i18n.dart new file mode 100644 index 0000000..58ac63e --- /dev/null +++ b/lib/app/modules/country/translations/country_page.i18n.dart @@ -0,0 +1,12 @@ +import 'package:i18n_extension/i18n_extension.dart'; + +extension Localization on String { + + static var _t = Translations("en_us") + + { + "en_us": "Brazil", + "pt_br": "Brasil", + }; + + String get i18n => localize(this, _t); +} \ No newline at end of file diff --git a/lib/app/modules/home/home_controller.dart b/lib/app/modules/home/home_controller.dart index c57d00d..63b3d46 100644 --- a/lib/app/modules/home/home_controller.dart +++ b/lib/app/modules/home/home_controller.dart @@ -1,7 +1,9 @@ import 'package:corona_data/app/modules/country/country_module.dart'; +import 'package:corona_data/app/modules/settings/global_settings_controller.dart'; import 'package:corona_data/app/modules/states_map/states_map_module.dart'; import 'package:corona_data/app/modules/world/world_module.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_modular/flutter_modular.dart'; import 'package:mobx/mobx.dart'; part 'home_controller.g.dart'; @@ -11,11 +13,12 @@ class HomeController = _HomeControllerBase with _$HomeController; abstract class _HomeControllerBase with Store { @observable int selectedIndex; + GlobalSettingsController globalSettingsController = Modular.get(); - final _titleName = ["country", "Mundo", "Mapa"]; + final _titleName = ["country", "World", "Map"]; @computed - String get title => _titleName[selectedIndex]; + String get title => _titleName[selectedIndex]=='country'?globalSettingsController.country.value.name:_titleName[selectedIndex]; final List _pages = [ CountryModule(), diff --git a/lib/app/modules/home/home_page.dart b/lib/app/modules/home/home_page.dart index ee8befc..484749d 100644 --- a/lib/app/modules/home/home_page.dart +++ b/lib/app/modules/home/home_page.dart @@ -5,6 +5,8 @@ import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_modular/flutter_modular.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:google_fonts/google_fonts.dart'; +import 'package:i18n_extension/i18n_widget.dart'; +import 'translations/home_page.i18n.dart'; class HomePage extends StatefulWidget { HomePage({Key key}) : super(key: key); @@ -15,9 +17,16 @@ class HomePage extends StatefulWidget { class _HomePageState extends ModularState { final AppController appController = Modular.get(); + @override + void initState() { + // TODO: implement initState + super.initState(); + I18n.of(context).locale = Locale("en"); + } @override Widget build(BuildContext context) { + return Observer( builder: (BuildContext context) { @@ -26,9 +35,7 @@ class _HomePageState extends ModularState { backgroundColor: Theme.of(context).backgroundColor, appBar: AppBar( title: Text( - controller.title == 'country' - ? appController.globalSettingsController.country.value.name - : controller.title, + controller.title.i18n, style: GoogleFonts.robotoSlab( fontSize: 24, letterSpacing: -1.9, diff --git a/lib/app/modules/home/translations/home_page.i18n.dart b/lib/app/modules/home/translations/home_page.i18n.dart new file mode 100644 index 0000000..05bf5be --- /dev/null +++ b/lib/app/modules/home/translations/home_page.i18n.dart @@ -0,0 +1,16 @@ +import 'package:i18n_extension/i18n_extension.dart'; + +extension Localization on String { + + static var _t = Translations("en_us") + + { + "en_us": "Brazil", + "pt_br": "Brasil", + }+ + { + "en_us": "World", + "pt_br": "Mundo", + }; + + String get i18n => localize(this, _t); +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index f37f9ca..5cc57d4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -216,6 +216,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_localizations: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" flutter_mobx: dependency: "direct main" description: @@ -326,6 +331,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.1.3" + i18n_extension: + dependency: "direct main" + description: + name: i18n_extension + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.5" image: dependency: transitive description: @@ -339,7 +351,7 @@ packages: name: intl url: "https://pub.dartlang.org" source: hosted - version: "0.16.1" + version: "0.16.0" io: dependency: transitive description: @@ -583,6 +595,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.5.5" + sprintf: + dependency: transitive + description: + name: sprintf + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.2" stack_trace: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 0d99396..d53f729 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,11 +14,11 @@ description: A new Flutter project. version: 1.0.0+1 environment: - sdk: ">=2.2.0 <3.0.0" + sdk: ">=2.6.0 <3.0.0" dependencies: flutter_svg: ^0.17.4 - intl: ^0.16.1 + intl: ^0.16.0 number_display: ^2.2.1 modal_bottom_sheet: ^0.1.5 path_provider: ^1.6.5 @@ -33,6 +33,9 @@ dependencies: flutter_modular: ^0.5.6 flare_flutter: ^2.0.2 autocomplete_textfield: ^1.7.3 + i18n_extension: ^1.3.5 + flutter_localizations: + sdk: flutter flutter: sdk: flutter @@ -46,6 +49,7 @@ dev_dependencies: build_runner: ^1.8.0 flutter_test: sdk: flutter + # For information on the generic Dart part of this file, see the From 1a19792e1c194303295cbccc230b3829343a3d03 Mon Sep 17 00:00:00 2001 From: allan Date: Fri, 1 May 2020 14:21:17 -0300 Subject: [PATCH 3/6] Theme configuration and begin integrations with GlobalSettingsController Done --- lib/app/app_widget.dart | 2 +- lib/app/modules/home/home_page.dart | 3 +- .../settings/global_settings_controller.dart | 28 ++++++++++- lib/app/shared/config/config.dart | 10 ++++ .../repositories/local_storage_hive.dart | 19 ++++++++ .../repositories/local_storage_interface.dart | 3 ++ .../localization/locales/en_us_locale.dart | 15 ++++++ .../localization/locales/pt_br_locale.dart | 21 ++++++++ .../localization/localization_interface.dart | 11 +++++ .../localization/localization_utils.dart | 29 +++++++++++ lib/app/shared/utils/theme/constants.dart | 48 ------------------- 11 files changed, 136 insertions(+), 53 deletions(-) create mode 100644 lib/app/shared/utils/localization/locales/en_us_locale.dart create mode 100644 lib/app/shared/utils/localization/locales/pt_br_locale.dart create mode 100644 lib/app/shared/utils/localization/localization_interface.dart create mode 100644 lib/app/shared/utils/localization/localization_utils.dart diff --git a/lib/app/app_widget.dart b/lib/app/app_widget.dart index f398752..c8141f8 100644 --- a/lib/app/app_widget.dart +++ b/lib/app/app_widget.dart @@ -40,7 +40,7 @@ class _AppWidgetState extends State { builder: (context,widget){ return I18n( - initialLocale: (Locale("pt","BR")), + initialLocale: controller.globalSettingsController.locale.getLocale(), child:widget ); }, diff --git a/lib/app/modules/home/home_page.dart b/lib/app/modules/home/home_page.dart index 484749d..156d2c4 100644 --- a/lib/app/modules/home/home_page.dart +++ b/lib/app/modules/home/home_page.dart @@ -5,7 +5,6 @@ import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_modular/flutter_modular.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:google_fonts/google_fonts.dart'; -import 'package:i18n_extension/i18n_widget.dart'; import 'translations/home_page.i18n.dart'; class HomePage extends StatefulWidget { @@ -21,7 +20,7 @@ class _HomePageState extends ModularState { void initState() { // TODO: implement initState super.initState(); - I18n.of(context).locale = Locale("en"); + } @override diff --git a/lib/app/modules/settings/global_settings_controller.dart b/lib/app/modules/settings/global_settings_controller.dart index 8c43231..897e452 100644 --- a/lib/app/modules/settings/global_settings_controller.dart +++ b/lib/app/modules/settings/global_settings_controller.dart @@ -1,5 +1,7 @@ import 'package:corona_data/app/shared/models/country_model.dart'; import 'package:corona_data/app/shared/repositories/local_storage_interface.dart'; +import 'package:corona_data/app/shared/utils/localization/localization_interface.dart'; +import 'package:corona_data/app/shared/utils/localization/localization_utils.dart'; import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; import 'package:corona_data/app/shared/utils/theme/theme_utils.dart'; import 'package:flutter/material.dart'; @@ -17,6 +19,9 @@ abstract class _GlobalSettingsControllerBase with Store { @observable ObservableFuture themeName; + @observable + ObservableFuture localeKey; + @observable ObservableFuture country; @@ -24,21 +29,26 @@ abstract class _GlobalSettingsControllerBase with Store { ITheme get theme { return ThemeUtils.getTheme(themeName?.value); } + @computed + ILocalization get locale { + return LocalizationUtils.getLocale(localeKey?.value); + } @computed bool get isReady { - return themeName.value != null && country.value != null; + return themeName.value != null && country.value != null && localeKey.value != null; } @action void init() { getTheme(); getCountry(); + getLocale(); } @computed int get isChanged => - (country.value.name + themeName.value.toString()).hashCode; + (country.value.name + themeName.value.toString() + localeKey.value).hashCode; @action void getTheme() { @@ -67,6 +77,20 @@ abstract class _GlobalSettingsControllerBase with Store { this.country = localStorage.getCountry().asObservable(); } + @action + void setLocale(String localeKey) { + if (localeKey!= this.localeKey.value) { + this.localeKey = ObservableFuture.value(localeKey); + localStorage.setLocale(localeKey); + } + } + + @action + void getLocale() { + this.localeKey = localStorage.getLocale().asObservable(); + } + + } diff --git a/lib/app/shared/config/config.dart b/lib/app/shared/config/config.dart index 10a2a46..4ab3048 100644 --- a/lib/app/shared/config/config.dart +++ b/lib/app/shared/config/config.dart @@ -1,7 +1,11 @@ +import 'package:corona_data/app/shared/utils/localization/locales/en_us_locale.dart'; +import 'package:corona_data/app/shared/utils/localization/locales/pt_br_locale.dart'; +import 'package:corona_data/app/shared/utils/localization/localization_interface.dart'; import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; import 'package:corona_data/app/shared/utils/theme/themes/theme_dark.dart'; import 'package:corona_data/app/shared/utils/theme/themes/theme_dracula.dart'; import 'package:corona_data/app/shared/utils/theme/themes/theme_light.dart'; +import 'package:flutter/material.dart'; final Map themes = { "light": ThemeLight(), @@ -9,3 +13,9 @@ final Map themes = { "dracula": ThemeDracula(), }; + +final Map locales = { + PTBRLocale.key:PTBRLocale(), + ENUSLocale.key:ENUSLocale(), +}; + diff --git a/lib/app/shared/repositories/local_storage_hive.dart b/lib/app/shared/repositories/local_storage_hive.dart index 3cdb005..feaec9d 100644 --- a/lib/app/shared/repositories/local_storage_hive.dart +++ b/lib/app/shared/repositories/local_storage_hive.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:io'; import 'package:corona_data/app/shared/models/country_model.dart'; +import 'package:corona_data/app/shared/utils/localization/localization_utils.dart'; import 'package:hive/hive.dart'; import 'package:path_provider/path_provider.dart'; @@ -11,6 +12,7 @@ import 'local_storage_interface.dart'; class LocalStorageHive implements ILocalStorage{ final String _themeKey = 'THEME'; final String _countryKey = 'COUNTRY'; + final String _localeKey = 'LOCALE'; Completer _instance = Completer(); @@ -60,5 +62,22 @@ class LocalStorageHive implements ILocalStorage{ await box.put(_countryKey, value); } + + @override + Future getLocale() async{ + var box = await _instance.future; + + var value = await box.get(_localeKey); + value ??= LocalizationUtils.defaultLocaleKey; + + return value.toString(); + } + + @override + Future setLocale(String value) async { + var box = await _instance.future; + + await box.put(_localeKey, value); + } } \ No newline at end of file diff --git a/lib/app/shared/repositories/local_storage_interface.dart b/lib/app/shared/repositories/local_storage_interface.dart index 88d9a29..9a08ef7 100644 --- a/lib/app/shared/repositories/local_storage_interface.dart +++ b/lib/app/shared/repositories/local_storage_interface.dart @@ -4,6 +4,9 @@ abstract class ILocalStorage { Future getTheme(); Future setTheme(String value); + Future getLocale(); + Future setLocale(String value); + Future getCountry(); Future setCountry(CountryModel country); diff --git a/lib/app/shared/utils/localization/locales/en_us_locale.dart b/lib/app/shared/utils/localization/locales/en_us_locale.dart new file mode 100644 index 0000000..69402bd --- /dev/null +++ b/lib/app/shared/utils/localization/locales/en_us_locale.dart @@ -0,0 +1,15 @@ +import 'package:corona_data/app/shared/utils/localization/localization_interface.dart'; + +class ENUSLocale extends ILocalization{ + @override + String get countryCode => 'US'; + + @override + String get languageCode => 'en'; + + @override + String get name => 'United States'; + + static const String key = "United States"; + +} \ No newline at end of file diff --git a/lib/app/shared/utils/localization/locales/pt_br_locale.dart b/lib/app/shared/utils/localization/locales/pt_br_locale.dart new file mode 100644 index 0000000..e776e52 --- /dev/null +++ b/lib/app/shared/utils/localization/locales/pt_br_locale.dart @@ -0,0 +1,21 @@ +import 'package:corona_data/app/shared/utils/localization/localization_interface.dart'; + +class PTBRLocale extends ILocalization{ + @override + // TODO: implement countryCode + String get countryCode => "BR"; + + @override + // TODO: implement languageCode + String get languageCode => 'pt'; + + @override + // TODO: implement name + String get name => 'Brazil'; + + static const String key = "BRAZIL"; + + + + +} \ No newline at end of file diff --git a/lib/app/shared/utils/localization/localization_interface.dart b/lib/app/shared/utils/localization/localization_interface.dart new file mode 100644 index 0000000..ded749d --- /dev/null +++ b/lib/app/shared/utils/localization/localization_interface.dart @@ -0,0 +1,11 @@ +import 'package:flutter/material.dart'; + +abstract class ILocalization{ + final String name = null; + final String languageCode = null; + final String countryCode = null; + + Locale getLocale(){ + return Locale(languageCode,countryCode); + } +} \ No newline at end of file diff --git a/lib/app/shared/utils/localization/localization_utils.dart b/lib/app/shared/utils/localization/localization_utils.dart new file mode 100644 index 0000000..2ad7704 --- /dev/null +++ b/lib/app/shared/utils/localization/localization_utils.dart @@ -0,0 +1,29 @@ +import 'package:corona_data/app/shared/config/config.dart'; +import 'package:corona_data/app/shared/utils/localization/locales/en_us_locale.dart'; +import 'package:corona_data/app/shared/utils/localization/localization_interface.dart'; + +class LocalizationUtils { + const LocalizationUtils._(); + static ILocalization getLocale(String locale) { + ILocalization themeInstance; + locale = locale != null?locale.toLowerCase():''; + + if(locale == null){ + themeInstance = locales[defaultLocaleKey]; + } + + + else if (locales.containsKey(locale)) { + themeInstance = locales[locale]; + } else { + themeInstance = locales[defaultLocaleKey]; + } + return themeInstance; + } + + static List getThemeNamesPretty(){ + return locales.keys.map((name)=>name.toUpperCase()).toList(); + } + + static const defaultLocaleKey=ENUSLocale.key; +} \ No newline at end of file diff --git a/lib/app/shared/utils/theme/constants.dart b/lib/app/shared/utils/theme/constants.dart index cef0345..e69de29 100644 --- a/lib/app/shared/utils/theme/constants.dart +++ b/lib/app/shared/utils/theme/constants.dart @@ -1,48 +0,0 @@ -import 'package:flutter/material.dart'; - - -final ThemeData themeLight = ThemeData( - primaryColor: Color(0xffdcddda), - primaryColorLight: Color(0xff83867e), - primaryColorDark: Color(0xffEBE9E7), - accentColor: Color(0xff231F1C), - backgroundColor: Color(0xffEBE9E7), - buttonColor: Color(0xff231F1C), - secondaryHeaderColor: Color(0xffcfd0cd), - -); - -final ThemeData themeDark = ThemeData( - primaryColor: Color(0xff393e3b), - primaryColorLight: Color(0xff9C9F98), - primaryColorDark: Color(0xff231F1C), - accentColor: Color(0xffEBE9E7), - canvasColor: Color(0xff393e3b), - backgroundColor: Color(0xff231F1C), - secondaryHeaderColor: Color(0xff2C2F26), -); - -final ThemeData themeDracula = ThemeData( - primaryColor: Color(0xff44475a), - primaryColorLight: Color(0xffbd93f9), - primaryColorDark: Color(0xff282a36), - accentColor: Color(0xfff8f8f2), - canvasColor: Color(0xff282a36), - backgroundColor: Color(0xff282a36), - errorColor: Color(0xffff5555), - iconTheme: IconThemeData(color: Color(0xfff8f8f2)), - primaryIconTheme: IconThemeData(color: Color(0xfff8f8f2)), - accentIconTheme: IconThemeData(color: Color(0xfff8f8f2)), - buttonTheme: ButtonThemeData(buttonColor: Color(0xfff1fa8c)), - buttonColor: Color(0xfff1fa8c), - secondaryHeaderColor: Color(0xff44475a), - -); - -// class ThemeColors{ -// const ThemeColors._(); -// static const Color success = Colors.green; -// static const Color error = Colors.red; -// static const Color warning = Colors.orange; -// static const Color info = Colors.blue; -// } \ No newline at end of file From 9977dbf020f582d7e1cfce043485f1f8030e2f27 Mon Sep 17 00:00:00 2001 From: allan Date: Fri, 1 May 2020 16:12:33 -0300 Subject: [PATCH 4/6] Integration between i18n, modular and mobx --- lib/app/app_widget.dart | 56 +++++++++++++------ .../settings/global_settings_controller.dart | 7 +++ .../global_settings_controller.g.dart | 46 ++++++++++++++- lib/app/modules/settings/settings_page.dart | 44 +++++---------- .../widgets/locale/locale_dropdown.dart | 46 +++++++++++++++ .../settings/widgets/locale/locale_row.dart | 41 ++++++++++++++ .../widgets/{ => theme}/theme_dropdown.dart | 0 .../settings/widgets/theme/theme_row.dart | 43 ++++++++++++++ .../repositories/local_storage_hive.dart | 3 +- .../localization/locales/en_us_locale.dart | 2 +- .../localization/locales/pt_br_locale.dart | 2 +- .../localization/localization_utils.dart | 9 +-- 12 files changed, 246 insertions(+), 53 deletions(-) create mode 100644 lib/app/modules/settings/widgets/locale/locale_dropdown.dart create mode 100644 lib/app/modules/settings/widgets/locale/locale_row.dart rename lib/app/modules/settings/widgets/{ => theme}/theme_dropdown.dart (100%) create mode 100644 lib/app/modules/settings/widgets/theme/theme_row.dart diff --git a/lib/app/app_widget.dart b/lib/app/app_widget.dart index c8141f8..d9a331b 100644 --- a/lib/app/app_widget.dart +++ b/lib/app/app_widget.dart @@ -13,11 +13,29 @@ class AppWidget extends StatefulWidget { class _AppWidgetState extends State { final AppController controller = Modular.get(); + final GlobalKey _rootKey = GlobalKey(); + List disposers = []; @override void initState() { super.initState(); - reaction((_) => controller.globalSettingsController.theme, (_) => (setState((){}))); + + disposers.add( + reaction( + (_) => controller.globalSettingsController.theme, + (_) => (setState(() {})), + ), + ); + + disposers.add( + reaction( + (_) => controller.globalSettingsController.locale, + (_) { + I18n.of(_rootKey.currentContext).locale = + controller.globalSettingsController.locale.getLocale(); + }, + ), + ); } @override @@ -28,23 +46,29 @@ class _AppWidgetState extends State { theme: controller.globalSettingsController.theme.themeData, initialRoute: '/', onGenerateRoute: Modular.generateRoute, - localizationsDelegates: [ - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, - ], - supportedLocales: [ - const Locale('en', "US"), - const Locale('pt', "BR"), - ], - builder: (context,widget){ - + localizationsDelegates: [ + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: [ + const Locale('en', "US"), + const Locale('pt', "BR"), + ], + builder: (context, widget) { return I18n( - initialLocale: controller.globalSettingsController.locale.getLocale(), - child:widget - ); + initialLocale: + controller.globalSettingsController.locale.getLocale(), + child: Container(key: _rootKey, child: widget)); }, - ); } + + @override + void dispose() { + disposers.forEach((disposer) => disposer()); + print(disposers); + print("disposados"); + super.dispose(); + } } diff --git a/lib/app/modules/settings/global_settings_controller.dart b/lib/app/modules/settings/global_settings_controller.dart index 897e452..38ace0d 100644 --- a/lib/app/modules/settings/global_settings_controller.dart +++ b/lib/app/modules/settings/global_settings_controller.dart @@ -27,15 +27,19 @@ abstract class _GlobalSettingsControllerBase with Store { @computed ITheme get theme { + print(themeName?.value); return ThemeUtils.getTheme(themeName?.value); } + @computed ILocalization get locale { + print(localeKey?.value); return LocalizationUtils.getLocale(localeKey?.value); } @computed bool get isReady { + print(localeKey?.value); return themeName.value != null && country.value != null && localeKey.value != null; } @@ -48,6 +52,7 @@ abstract class _GlobalSettingsControllerBase with Store { @computed int get isChanged => + (country.value.name + themeName.value.toString() + localeKey.value).hashCode; @action @@ -82,12 +87,14 @@ abstract class _GlobalSettingsControllerBase with Store { if (localeKey!= this.localeKey.value) { this.localeKey = ObservableFuture.value(localeKey); localStorage.setLocale(localeKey); + } } @action void getLocale() { this.localeKey = localStorage.getLocale().asObservable(); + } diff --git a/lib/app/modules/settings/global_settings_controller.g.dart b/lib/app/modules/settings/global_settings_controller.g.dart index b7fc801..0d7dc9f 100644 --- a/lib/app/modules/settings/global_settings_controller.g.dart +++ b/lib/app/modules/settings/global_settings_controller.g.dart @@ -14,6 +14,11 @@ mixin _$GlobalSettingsController on _GlobalSettingsControllerBase, Store { @override ITheme get theme => (_$themeComputed ??= Computed(() => super.theme)).value; + Computed _$localeComputed; + + @override + ILocalization get locale => + (_$localeComputed ??= Computed(() => super.locale)).value; Computed _$isReadyComputed; @override @@ -42,6 +47,23 @@ mixin _$GlobalSettingsController on _GlobalSettingsControllerBase, Store { }, _$themeNameAtom, name: '${_$themeNameAtom.name}_set'); } + final _$localeKeyAtom = Atom(name: '_GlobalSettingsControllerBase.localeKey'); + + @override + ObservableFuture get localeKey { + _$localeKeyAtom.context.enforceReadPolicy(_$localeKeyAtom); + _$localeKeyAtom.reportObserved(); + return super.localeKey; + } + + @override + set localeKey(ObservableFuture value) { + _$localeKeyAtom.context.conditionallyRunInAction(() { + super.localeKey = value; + _$localeKeyAtom.reportChanged(); + }, _$localeKeyAtom, name: '${_$localeKeyAtom.name}_set'); + } + final _$countryAtom = Atom(name: '_GlobalSettingsControllerBase.country'); @override @@ -117,10 +139,32 @@ mixin _$GlobalSettingsController on _GlobalSettingsControllerBase, Store { } } + @override + void setLocale(String localeKey) { + final _$actionInfo = + _$_GlobalSettingsControllerBaseActionController.startAction(); + try { + return super.setLocale(localeKey); + } finally { + _$_GlobalSettingsControllerBaseActionController.endAction(_$actionInfo); + } + } + + @override + void getLocale() { + final _$actionInfo = + _$_GlobalSettingsControllerBaseActionController.startAction(); + try { + return super.getLocale(); + } finally { + _$_GlobalSettingsControllerBaseActionController.endAction(_$actionInfo); + } + } + @override String toString() { final string = - 'themeName: ${themeName.toString()},country: ${country.toString()},theme: ${theme.toString()},isReady: ${isReady.toString()},isChanged: ${isChanged.toString()}'; + 'themeName: ${themeName.toString()},localeKey: ${localeKey.toString()},country: ${country.toString()},theme: ${theme.toString()},locale: ${locale.toString()},isReady: ${isReady.toString()},isChanged: ${isChanged.toString()}'; return '{$string}'; } } diff --git a/lib/app/modules/settings/settings_page.dart b/lib/app/modules/settings/settings_page.dart index 7f47d05..808328b 100644 --- a/lib/app/modules/settings/settings_page.dart +++ b/lib/app/modules/settings/settings_page.dart @@ -1,6 +1,7 @@ import 'package:autocomplete_textfield/autocomplete_textfield.dart'; import 'package:corona_data/app/app_controller.dart'; -import 'package:corona_data/app/modules/settings/widgets/theme_dropdown.dart'; +import 'package:corona_data/app/modules/settings/widgets/locale/locale_row.dart'; +import 'package:corona_data/app/modules/settings/widgets/theme/theme_row.dart'; import 'package:corona_data/app/shared/models/country_model.dart'; import 'package:corona_data/app/shared/utils/constants.dart'; import 'package:corona_data/app/shared/utils/snackbar_util.dart'; @@ -9,6 +10,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_modular/flutter_modular.dart'; import 'package:google_fonts/google_fonts.dart'; +import 'package:i18n_extension/i18n_widget.dart'; import 'package:mobx/mobx.dart'; import 'settings_controller.dart'; @@ -25,7 +27,7 @@ class _SettingsPageState extends ModularState { AppController appController = Modular.get(); - GlobalKey> key = GlobalKey(); + GlobalKey> _autoCompleteKey = GlobalKey(); final GlobalKey _scaffoldKey = GlobalKey(); SnackBarUtil snackbar; @@ -44,6 +46,8 @@ class _SettingsPageState disposer = reaction((_) => appController.globalSettingsController.isChanged, (value) { + print("reaction"); + print(I18n.of(context).locale); snackbar.enqueueMessage( message: 'Settings has been changed!', color: appController.globalSettingsController.theme.extraPallete.success, @@ -57,7 +61,7 @@ class _SettingsPageState @override Widget build(BuildContext context) { - key = GlobalKey(); + _autoCompleteKey = GlobalKey(); return Scaffold( key: _scaffoldKey, backgroundColor: Theme.of(context).primaryColorDark, @@ -78,34 +82,14 @@ class _SettingsPageState Container( height: 10, ), - Container( - padding: EdgeInsets.symmetric(horizontal: 20.0), - color: Theme.of(context).primaryColor, - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text( - 'Tema', - style: GoogleFonts.robotoSlab( - color: Theme.of(context).accentColor, fontSize: 16), - ), - Observer(builder: (_) { - - return ThemeDropdown( - value: appController - .globalSettingsController.themeName.value - .toUpperCase(), - onChanged: (String newValue) { - appController.globalSettingsController.setTheme(newValue); - }, - ); - }), - ], - ), + ThemeRow(appController: appController), + Padding( + padding: const EdgeInsets.only(top:20), + child: LocaleRow(appController:appController), ), Observer(builder: (context) { return Padding( - padding: const EdgeInsets.only(top: 10.0), + padding: const EdgeInsets.only(top: 20.0), child: Container( padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 5.0), color: Theme.of(context).primaryColor, @@ -114,7 +98,7 @@ class _SettingsPageState children: [ SimpleAutoCompleteTextField( style: TextStyle(color: Theme.of(context).accentColor), - key: key, + key: _autoCompleteKey, decoration: InputDecoration( errorText: null, labelText: "País", @@ -165,3 +149,5 @@ class _SettingsPageState } + + diff --git a/lib/app/modules/settings/widgets/locale/locale_dropdown.dart b/lib/app/modules/settings/widgets/locale/locale_dropdown.dart new file mode 100644 index 0000000..2db249b --- /dev/null +++ b/lib/app/modules/settings/widgets/locale/locale_dropdown.dart @@ -0,0 +1,46 @@ +import 'package:corona_data/app/shared/utils/localization/localization_utils.dart'; +import 'package:corona_data/app/shared/utils/theme/theme_utils.dart'; +import 'package:flutter/material.dart'; +import 'package:google_fonts/google_fonts.dart'; + +class LocaleDropDown extends StatelessWidget { + + final String value; + final void Function(String newValue) onChanged; + + const LocaleDropDown({Key key, this.value, this.onChanged}) : super(key: key); + + @override + Widget build(BuildContext context) { + return Container(child: + DropdownButton( + value: value, + iconEnabledColor: Theme.of(context).primaryColor, + focusColor: Theme.of(context).primaryColor, + icon: Icon( + Icons.arrow_downward, + color: Theme.of(context).accentColor, + ), + iconSize: 15, + elevation: 16, + style: GoogleFonts.robotoSlab( + color: Theme.of(context).accentColor, fontSize: 16, fontWeight: FontWeight.w600), + underline: Container( + height: 1, + color: Theme.of(context).accentColor, + ), + onChanged: onChanged, + items: LocalizationUtils.getLocaleNamesPretty() + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text( + value, + + ), + ); + }).toList(), + ), + ); + } +} \ No newline at end of file diff --git a/lib/app/modules/settings/widgets/locale/locale_row.dart b/lib/app/modules/settings/widgets/locale/locale_row.dart new file mode 100644 index 0000000..cf96e4c --- /dev/null +++ b/lib/app/modules/settings/widgets/locale/locale_row.dart @@ -0,0 +1,41 @@ +import 'package:corona_data/app/app_controller.dart'; +import 'package:corona_data/app/modules/settings/widgets/locale/locale_dropdown.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_mobx/flutter_mobx.dart'; +import 'package:google_fonts/google_fonts.dart'; + +class LocaleRow extends StatelessWidget { + const LocaleRow({ + Key key, + @required this.appController, + }) : super(key: key); + + final AppController appController; + + @override + Widget build(BuildContext context) { + + return Container( + padding: EdgeInsets.symmetric(horizontal: 20.0), + color: Theme.of(context).primaryColor, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'Language', + style: GoogleFonts.robotoSlab( + color: Theme.of(context).accentColor, fontSize: 16), + ), + Observer(builder: (_) { + return LocaleDropDown( + value: appController.globalSettingsController.localeKey.value.toUpperCase(), + onChanged: (String newValue) { + appController.globalSettingsController.setLocale(newValue); + }, + ); + }), + ], + ), + ); + } +} diff --git a/lib/app/modules/settings/widgets/theme_dropdown.dart b/lib/app/modules/settings/widgets/theme/theme_dropdown.dart similarity index 100% rename from lib/app/modules/settings/widgets/theme_dropdown.dart rename to lib/app/modules/settings/widgets/theme/theme_dropdown.dart diff --git a/lib/app/modules/settings/widgets/theme/theme_row.dart b/lib/app/modules/settings/widgets/theme/theme_row.dart new file mode 100644 index 0000000..ba7cb3c --- /dev/null +++ b/lib/app/modules/settings/widgets/theme/theme_row.dart @@ -0,0 +1,43 @@ +import 'package:corona_data/app/app_controller.dart'; +import 'package:corona_data/app/modules/settings/widgets/theme/theme_dropdown.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_mobx/flutter_mobx.dart'; +import 'package:google_fonts/google_fonts.dart'; + +class ThemeRow extends StatelessWidget { + const ThemeRow({ + Key key, + @required this.appController, + }) : super(key: key); + + final AppController appController; + + @override + Widget build(BuildContext context) { + return Container( + padding: EdgeInsets.symmetric(horizontal: 20.0), + color: Theme.of(context).primaryColor, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'Tema', + style: GoogleFonts.robotoSlab( + color: Theme.of(context).accentColor, fontSize: 16), + ), + Observer(builder: (_) { + + return ThemeDropdown( + value: appController + .globalSettingsController.themeName.value + .toUpperCase(), + onChanged: (String newValue) { + appController.globalSettingsController.setTheme(newValue); + }, + ); + }), + ], + ), + ); + } +} \ No newline at end of file diff --git a/lib/app/shared/repositories/local_storage_hive.dart b/lib/app/shared/repositories/local_storage_hive.dart index feaec9d..fb81f89 100644 --- a/lib/app/shared/repositories/local_storage_hive.dart +++ b/lib/app/shared/repositories/local_storage_hive.dart @@ -68,9 +68,10 @@ class LocalStorageHive implements ILocalStorage{ var box = await _instance.future; var value = await box.get(_localeKey); + value ??= LocalizationUtils.defaultLocaleKey; - return value.toString(); + return value; } @override diff --git a/lib/app/shared/utils/localization/locales/en_us_locale.dart b/lib/app/shared/utils/localization/locales/en_us_locale.dart index 69402bd..c1a8a51 100644 --- a/lib/app/shared/utils/localization/locales/en_us_locale.dart +++ b/lib/app/shared/utils/localization/locales/en_us_locale.dart @@ -10,6 +10,6 @@ class ENUSLocale extends ILocalization{ @override String get name => 'United States'; - static const String key = "United States"; + static const String key = "en_us"; } \ No newline at end of file diff --git a/lib/app/shared/utils/localization/locales/pt_br_locale.dart b/lib/app/shared/utils/localization/locales/pt_br_locale.dart index e776e52..74f95d1 100644 --- a/lib/app/shared/utils/localization/locales/pt_br_locale.dart +++ b/lib/app/shared/utils/localization/locales/pt_br_locale.dart @@ -13,7 +13,7 @@ class PTBRLocale extends ILocalization{ // TODO: implement name String get name => 'Brazil'; - static const String key = "BRAZIL"; + static const String key = "pt_br"; diff --git a/lib/app/shared/utils/localization/localization_utils.dart b/lib/app/shared/utils/localization/localization_utils.dart index 2ad7704..19ce819 100644 --- a/lib/app/shared/utils/localization/localization_utils.dart +++ b/lib/app/shared/utils/localization/localization_utils.dart @@ -1,5 +1,5 @@ import 'package:corona_data/app/shared/config/config.dart'; -import 'package:corona_data/app/shared/utils/localization/locales/en_us_locale.dart'; +import 'package:corona_data/app/shared/utils/localization/locales/pt_br_locale.dart'; import 'package:corona_data/app/shared/utils/localization/localization_interface.dart'; class LocalizationUtils { @@ -7,7 +7,7 @@ class LocalizationUtils { static ILocalization getLocale(String locale) { ILocalization themeInstance; locale = locale != null?locale.toLowerCase():''; - + print(locale); if(locale == null){ themeInstance = locales[defaultLocaleKey]; } @@ -21,9 +21,10 @@ class LocalizationUtils { return themeInstance; } - static List getThemeNamesPretty(){ + static List getLocaleNamesPretty(){ + print(locales.keys.map((name)=>name.toUpperCase()).toList().toString()); return locales.keys.map((name)=>name.toUpperCase()).toList(); } - static const defaultLocaleKey=ENUSLocale.key; + static const defaultLocaleKey=PTBRLocale.key; } \ No newline at end of file From 18db537ecf151c7ec77c0a36e8a7b02274523e58 Mon Sep 17 00:00:00 2001 From: allan Date: Fri, 1 May 2020 17:23:05 -0300 Subject: [PATCH 5/6] resolved #22 internationalizations api ready --- lib/app/app_widget.dart | 3 +- .../world_cases/world_cases_controller.dart | 2 +- .../home/translations/home_page.i18n.dart | 4 + .../settings/global_settings_controller.dart | 8 +- .../modules/settings/settings_controller.dart | 2 +- lib/app/modules/settings/settings_page.dart | 5 +- .../widgets/locale/locale_dropdown.dart | 96 ++++++++++++------- .../settings/widgets/locale/locale_row.dart | 1 + lib/app/shared/config/config.dart | 1 - .../localization/locales/pt_br_locale.dart | 3 - .../localization/localization_utils.dart | 3 +- test/app/mocks/local_storage_mock.dart | 4 + .../modules/home/home_controller_test.dart | 28 ++++-- test/app/modules/home/home_page_test.dart | 4 +- 14 files changed, 103 insertions(+), 61 deletions(-) diff --git a/lib/app/app_widget.dart b/lib/app/app_widget.dart index d9a331b..91afbc4 100644 --- a/lib/app/app_widget.dart +++ b/lib/app/app_widget.dart @@ -67,8 +67,7 @@ class _AppWidgetState extends State { @override void dispose() { disposers.forEach((disposer) => disposer()); - print(disposers); - print("disposados"); + super.dispose(); } } diff --git a/lib/app/modules/charts/widgets/world_cases/world_cases_controller.dart b/lib/app/modules/charts/widgets/world_cases/world_cases_controller.dart index 360fc90..c91f7c2 100644 --- a/lib/app/modules/charts/widgets/world_cases/world_cases_controller.dart +++ b/lib/app/modules/charts/widgets/world_cases/world_cases_controller.dart @@ -16,7 +16,7 @@ abstract class _WorldCasesControllerBase with Store implements IChartController final HistoricalRepository historicalRepository; _WorldCasesControllerBase(this.historicalRepository){ - print("W---"); + fetchGraphData(); } diff --git a/lib/app/modules/home/translations/home_page.i18n.dart b/lib/app/modules/home/translations/home_page.i18n.dart index 05bf5be..04c19ec 100644 --- a/lib/app/modules/home/translations/home_page.i18n.dart +++ b/lib/app/modules/home/translations/home_page.i18n.dart @@ -7,6 +7,10 @@ extension Localization on String { "en_us": "Brazil", "pt_br": "Brasil", }+ + { + "en_us": "United States", + "pt_br": "Estados Unidos", + }+ { "en_us": "World", "pt_br": "Mundo", diff --git a/lib/app/modules/settings/global_settings_controller.dart b/lib/app/modules/settings/global_settings_controller.dart index 38ace0d..7869245 100644 --- a/lib/app/modules/settings/global_settings_controller.dart +++ b/lib/app/modules/settings/global_settings_controller.dart @@ -4,7 +4,6 @@ import 'package:corona_data/app/shared/utils/localization/localization_interface import 'package:corona_data/app/shared/utils/localization/localization_utils.dart'; import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; import 'package:corona_data/app/shared/utils/theme/theme_utils.dart'; -import 'package:flutter/material.dart'; import 'package:flutter_modular/flutter_modular.dart'; import 'package:mobx/mobx.dart'; @@ -27,19 +26,18 @@ abstract class _GlobalSettingsControllerBase with Store { @computed ITheme get theme { - print(themeName?.value); return ThemeUtils.getTheme(themeName?.value); } - + @computed ILocalization get locale { - print(localeKey?.value); + return LocalizationUtils.getLocale(localeKey?.value); } @computed bool get isReady { - print(localeKey?.value); + return themeName.value != null && country.value != null && localeKey.value != null; } diff --git a/lib/app/modules/settings/settings_controller.dart b/lib/app/modules/settings/settings_controller.dart index 9b1ba52..9eb105f 100644 --- a/lib/app/modules/settings/settings_controller.dart +++ b/lib/app/modules/settings/settings_controller.dart @@ -23,6 +23,6 @@ abstract class _SettingsControllerBase with Store implements Disposable { @override void dispose() { - print("Settings dispose"); + // print("Settings dispose"); } } diff --git a/lib/app/modules/settings/settings_page.dart b/lib/app/modules/settings/settings_page.dart index 808328b..2e45d4a 100644 --- a/lib/app/modules/settings/settings_page.dart +++ b/lib/app/modules/settings/settings_page.dart @@ -10,7 +10,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_modular/flutter_modular.dart'; import 'package:google_fonts/google_fonts.dart'; -import 'package:i18n_extension/i18n_widget.dart'; import 'package:mobx/mobx.dart'; import 'settings_controller.dart'; @@ -46,8 +45,6 @@ class _SettingsPageState disposer = reaction((_) => appController.globalSettingsController.isChanged, (value) { - print("reaction"); - print(I18n.of(context).locale); snackbar.enqueueMessage( message: 'Settings has been changed!', color: appController.globalSettingsController.theme.extraPallete.success, @@ -116,7 +113,7 @@ class _SettingsPageState textSubmitted: (text) { final int idx = countriesNames.indexOf(text); if (countriesNames.indexOf(text) != -1) { - print(COUNTRIES[idx]); + appController.globalSettingsController .setCountry(CountryModel.fromJson(COUNTRIES[idx])); diff --git a/lib/app/modules/settings/widgets/locale/locale_dropdown.dart b/lib/app/modules/settings/widgets/locale/locale_dropdown.dart index 2db249b..9c3b4f8 100644 --- a/lib/app/modules/settings/widgets/locale/locale_dropdown.dart +++ b/lib/app/modules/settings/widgets/locale/locale_dropdown.dart @@ -1,46 +1,74 @@ +import 'package:corona_data/app/shared/config/config.dart'; +import 'package:corona_data/app/shared/utils/localization/localization_interface.dart'; import 'package:corona_data/app/shared/utils/localization/localization_utils.dart'; -import 'package:corona_data/app/shared/utils/theme/theme_utils.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_svg/svg.dart'; import 'package:google_fonts/google_fonts.dart'; class LocaleDropDown extends StatelessWidget { - final String value; + final String countryCode; final void Function(String newValue) onChanged; - const LocaleDropDown({Key key, this.value, this.onChanged}) : super(key: key); - + const LocaleDropDown({Key key, this.value, this.onChanged, this.countryCode}) : super(key: key); + @override Widget build(BuildContext context) { - return Container(child: - DropdownButton( - value: value, - iconEnabledColor: Theme.of(context).primaryColor, - focusColor: Theme.of(context).primaryColor, - icon: Icon( - Icons.arrow_downward, - color: Theme.of(context).accentColor, - ), - iconSize: 15, - elevation: 16, - style: GoogleFonts.robotoSlab( - color: Theme.of(context).accentColor, fontSize: 16, fontWeight: FontWeight.w600), - underline: Container( - height: 1, - color: Theme.of(context).accentColor, - ), - onChanged: onChanged, - items: LocalizationUtils.getLocaleNamesPretty() - .map>((String value) { - return DropdownMenuItem( - value: value, - child: Text( - value, - - ), - ); - }).toList(), - ), + return Container( + + + child: DropdownButton( + value: value, + iconEnabledColor: Theme.of(context).primaryColor, + focusColor: Theme.of(context).primaryColor, + icon: null, + iconSize: 15, + elevation: 16, + style: GoogleFonts.robotoSlab( + color: Theme.of(context).accentColor, + fontSize: 16, + fontWeight: FontWeight.w600), + underline: Container( + height: 0, + color: Theme.of(context).accentColor, + ), + onChanged: onChanged, + selectedItemBuilder: _selectedItemBuilder, + items: _dropdownMenuItem(), + ), ); } -} \ No newline at end of file + + List _selectedItemBuilder(context) { + return LocalizationUtils.getLocaleNamesPretty() + .map( + (locale) => Container( + alignment: Alignment.centerLeft, + height: 35, + width: 35, + child: SvgPicture.asset( + "assets/flags/$countryCode.svg", + ), + ), + ) + .toList(); + } + + List> _dropdownMenuItem(){ + return locales.keys.map>((String key) { + ILocalization localeTemp = LocalizationUtils.getLocale(key); + return DropdownMenuItem( + value: key.toUpperCase(), + child: Container( + padding: EdgeInsets.only(top:5), + height: 40, + width: 40, + child: SvgPicture.asset( + "assets/flags/${localeTemp.countryCode.toLowerCase()}.svg", + ), + ), + ); + }).toList(); + + } +} diff --git a/lib/app/modules/settings/widgets/locale/locale_row.dart b/lib/app/modules/settings/widgets/locale/locale_row.dart index cf96e4c..07c0ee5 100644 --- a/lib/app/modules/settings/widgets/locale/locale_row.dart +++ b/lib/app/modules/settings/widgets/locale/locale_row.dart @@ -29,6 +29,7 @@ class LocaleRow extends StatelessWidget { Observer(builder: (_) { return LocaleDropDown( value: appController.globalSettingsController.localeKey.value.toUpperCase(), + countryCode: appController.globalSettingsController.locale.countryCode.toLowerCase(), onChanged: (String newValue) { appController.globalSettingsController.setLocale(newValue); }, diff --git a/lib/app/shared/config/config.dart b/lib/app/shared/config/config.dart index 4ab3048..a814983 100644 --- a/lib/app/shared/config/config.dart +++ b/lib/app/shared/config/config.dart @@ -5,7 +5,6 @@ import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; import 'package:corona_data/app/shared/utils/theme/themes/theme_dark.dart'; import 'package:corona_data/app/shared/utils/theme/themes/theme_dracula.dart'; import 'package:corona_data/app/shared/utils/theme/themes/theme_light.dart'; -import 'package:flutter/material.dart'; final Map themes = { "light": ThemeLight(), diff --git a/lib/app/shared/utils/localization/locales/pt_br_locale.dart b/lib/app/shared/utils/localization/locales/pt_br_locale.dart index 74f95d1..708b52c 100644 --- a/lib/app/shared/utils/localization/locales/pt_br_locale.dart +++ b/lib/app/shared/utils/localization/locales/pt_br_locale.dart @@ -2,15 +2,12 @@ import 'package:corona_data/app/shared/utils/localization/localization_interface class PTBRLocale extends ILocalization{ @override - // TODO: implement countryCode String get countryCode => "BR"; @override - // TODO: implement languageCode String get languageCode => 'pt'; @override - // TODO: implement name String get name => 'Brazil'; static const String key = "pt_br"; diff --git a/lib/app/shared/utils/localization/localization_utils.dart b/lib/app/shared/utils/localization/localization_utils.dart index 19ce819..8355667 100644 --- a/lib/app/shared/utils/localization/localization_utils.dart +++ b/lib/app/shared/utils/localization/localization_utils.dart @@ -7,7 +7,7 @@ class LocalizationUtils { static ILocalization getLocale(String locale) { ILocalization themeInstance; locale = locale != null?locale.toLowerCase():''; - print(locale); + if(locale == null){ themeInstance = locales[defaultLocaleKey]; } @@ -22,7 +22,6 @@ class LocalizationUtils { } static List getLocaleNamesPretty(){ - print(locales.keys.map((name)=>name.toUpperCase()).toList().toString()); return locales.keys.map((name)=>name.toUpperCase()).toList(); } diff --git a/test/app/mocks/local_storage_mock.dart b/test/app/mocks/local_storage_mock.dart index 0f143be..2ae94ae 100644 --- a/test/app/mocks/local_storage_mock.dart +++ b/test/app/mocks/local_storage_mock.dart @@ -1,5 +1,6 @@ import 'package:corona_data/app/shared/models/country_model.dart'; import 'package:corona_data/app/shared/repositories/local_storage_interface.dart'; +import 'package:corona_data/app/shared/utils/localization/localization_utils.dart'; import 'package:mockito/mockito.dart'; class LocalStorageMock extends Mock implements ILocalStorage { @@ -10,5 +11,8 @@ class LocalStorageMock extends Mock implements ILocalStorage { when(this.getCountry()).thenAnswer( (_) async => Future.value( CountryModel(name: "Brazil", code: "br")), ); + when(this.getLocale()).thenAnswer( + (_) async => Future.value( LocalizationUtils.defaultLocaleKey), + ); } } diff --git a/test/app/modules/home/home_controller_test.dart b/test/app/modules/home/home_controller_test.dart index 5de38aa..6d64944 100644 --- a/test/app/modules/home/home_controller_test.dart +++ b/test/app/modules/home/home_controller_test.dart @@ -1,20 +1,36 @@ import 'package:corona_data/app/app_module.dart'; import 'package:corona_data/app/modules/country/country_module.dart'; +import 'package:corona_data/app/modules/settings/global_settings_controller.dart'; import 'package:corona_data/app/modules/states_map/states_map_module.dart'; import 'package:corona_data/app/modules/world/world_module.dart'; +import 'package:corona_data/app/shared/repositories/local_storage_interface.dart'; import 'package:flutter_modular/flutter_modular.dart'; import 'package:flutter_modular/flutter_modular_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:corona_data/app/modules/home/home_controller.dart'; import 'package:corona_data/app/modules/home/home_module.dart'; +import 'package:mobx/mobx.dart' as mobx; + +import '../../mocks/local_storage_mock.dart'; void main() { - initModule(AppModule()); + LocalStorageMock covidRepositoryMock = LocalStorageMock(); + initModule(AppModule(),changeBinds: [ + Bind((i) => covidRepositoryMock), + ]); + // initModule(HomeModule()); + + initModule(HomeModule()); HomeController controller; - setUp(() { + GlobalSettingsController globalSettingsController; + + setUp(() async { + globalSettingsController = Modular.get(); + globalSettingsController.init(); + await mobx.asyncWhen((_) => globalSettingsController.isReady); controller = Modular.get(); }); @@ -22,7 +38,7 @@ void main() { test("Inicialization", () { expect(controller.selectedIndex, 0); expect(controller.page.runtimeType, CountryModule().runtimeType); - expect(controller.title, 'country'); + expect(controller.title, 'Brazil'); }); test("Set Page to World", () { @@ -30,14 +46,14 @@ void main() { expect(controller.selectedIndex, 1); expect(controller.page.runtimeType, WorldModule().runtimeType); - expect(controller.title, 'Mundo'); + expect(controller.title, 'World'); }); test("Set Page to Map", () { controller.setPage(2); expect(controller.selectedIndex, 2); expect(controller.page.runtimeType, StatesMapModule().runtimeType); - expect(controller.title, 'Mapa'); + expect(controller.title, 'Map'); }); test("Set Page to Country", () { @@ -45,7 +61,7 @@ void main() { expect(controller.selectedIndex, 0); expect(controller.page.runtimeType, CountryModule().runtimeType); - expect(controller.title, 'country'); + expect(controller.title, 'Brazil'); }); }); } diff --git a/test/app/modules/home/home_page_test.dart b/test/app/modules/home/home_page_test.dart index a61fc26..19f23c4 100644 --- a/test/app/modules/home/home_page_test.dart +++ b/test/app/modules/home/home_page_test.dart @@ -58,7 +58,7 @@ main() { final widgetFinder = find.byType(WorldPage); expect(widgetFinder, findsOneWidget); - final titleFinder = find.widgetWithText(AppBar, 'Mundo'); + final titleFinder = find.widgetWithText(AppBar, 'World'); expect(titleFinder, findsOneWidget); }); @@ -73,7 +73,7 @@ main() { final widgetFinder = find.byType(StatesMapPage); expect(widgetFinder, findsOneWidget); - final titleFinder = find.widgetWithText(AppBar, 'Mapa'); + final titleFinder = find.widgetWithText(AppBar, 'Map'); expect(titleFinder, findsOneWidget); }); }); From c0937b8e8cc1e6ceaa59d5107a4bcfc0f425594e Mon Sep 17 00:00:00 2001 From: allan Date: Fri, 1 May 2020 20:52:13 -0300 Subject: [PATCH 6/6] resolved #81 --- lib/app/app_widget.dart | 2 ++ .../home/translations/home_page.i18n.dart | 4 +++ .../widgets/locale/locale_dropdown.dart | 2 +- lib/app/shared/config/config.dart | 25 ++++++------------- .../repositories/local_storage_hive.dart | 3 ++- .../shared/utils/localization/constants.dart | 13 ++++++++++ .../localization/locales/en_us_locale.dart | 1 - .../localization/locales/pt_br_locale.dart | 5 ---- .../localization/localization_utils.dart | 12 ++++----- lib/app/shared/utils/theme/constants.dart | 15 +++++++++++ lib/app/shared/utils/theme/theme_utils.dart | 12 +++++---- test/app/mocks/local_storage_mock.dart | 3 ++- 12 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 lib/app/shared/utils/localization/constants.dart diff --git a/lib/app/app_widget.dart b/lib/app/app_widget.dart index 91afbc4..0404acb 100644 --- a/lib/app/app_widget.dart +++ b/lib/app/app_widget.dart @@ -1,3 +1,5 @@ +import 'package:corona_data/app/shared/config/config.dart'; +import 'package:corona_data/app/shared/utils/localization/constants.dart'; import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_modular/flutter_modular.dart'; diff --git a/lib/app/modules/home/translations/home_page.i18n.dart b/lib/app/modules/home/translations/home_page.i18n.dart index 04c19ec..ea4841d 100644 --- a/lib/app/modules/home/translations/home_page.i18n.dart +++ b/lib/app/modules/home/translations/home_page.i18n.dart @@ -11,6 +11,10 @@ extension Localization on String { "en_us": "United States", "pt_br": "Estados Unidos", }+ + { + "en_us": "Map", + "pt_br": "Mapa", + }+ { "en_us": "World", "pt_br": "Mundo", diff --git a/lib/app/modules/settings/widgets/locale/locale_dropdown.dart b/lib/app/modules/settings/widgets/locale/locale_dropdown.dart index 9c3b4f8..4c14855 100644 --- a/lib/app/modules/settings/widgets/locale/locale_dropdown.dart +++ b/lib/app/modules/settings/widgets/locale/locale_dropdown.dart @@ -55,7 +55,7 @@ class LocaleDropDown extends StatelessWidget { } List> _dropdownMenuItem(){ - return locales.keys.map>((String key) { + return Configuration.locales.keys.map>((String key) { ILocalization localeTemp = LocalizationUtils.getLocale(key); return DropdownMenuItem( value: key.toUpperCase(), diff --git a/lib/app/shared/config/config.dart b/lib/app/shared/config/config.dart index a814983..394b639 100644 --- a/lib/app/shared/config/config.dart +++ b/lib/app/shared/config/config.dart @@ -1,20 +1,11 @@ -import 'package:corona_data/app/shared/utils/localization/locales/en_us_locale.dart'; -import 'package:corona_data/app/shared/utils/localization/locales/pt_br_locale.dart'; -import 'package:corona_data/app/shared/utils/localization/localization_interface.dart'; -import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; -import 'package:corona_data/app/shared/utils/theme/themes/theme_dark.dart'; -import 'package:corona_data/app/shared/utils/theme/themes/theme_dracula.dart'; -import 'package:corona_data/app/shared/utils/theme/themes/theme_light.dart'; +import 'package:corona_data/app/shared/utils/localization/constants.dart'; +import 'package:corona_data/app/shared/utils/theme/constants.dart'; -final Map themes = { - "light": ThemeLight(), - "dark": ThemeDark(), - "dracula": ThemeDracula(), -}; +class Configuration { + static final themes = kThemes; + static const defaultLocaleKey=kPTBR; + static final locales = kLocales; -final Map locales = { - PTBRLocale.key:PTBRLocale(), - ENUSLocale.key:ENUSLocale(), -}; - + const Configuration._(); +} diff --git a/lib/app/shared/repositories/local_storage_hive.dart b/lib/app/shared/repositories/local_storage_hive.dart index fb81f89..7e30685 100644 --- a/lib/app/shared/repositories/local_storage_hive.dart +++ b/lib/app/shared/repositories/local_storage_hive.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:io'; +import 'package:corona_data/app/shared/config/config.dart'; import 'package:corona_data/app/shared/models/country_model.dart'; import 'package:corona_data/app/shared/utils/localization/localization_utils.dart'; import 'package:hive/hive.dart'; @@ -69,7 +70,7 @@ class LocalStorageHive implements ILocalStorage{ var value = await box.get(_localeKey); - value ??= LocalizationUtils.defaultLocaleKey; + value ??= Configuration.defaultLocaleKey; return value; } diff --git a/lib/app/shared/utils/localization/constants.dart b/lib/app/shared/utils/localization/constants.dart new file mode 100644 index 0000000..149c19f --- /dev/null +++ b/lib/app/shared/utils/localization/constants.dart @@ -0,0 +1,13 @@ + + +import 'package:corona_data/app/shared/utils/localization/locales/en_us_locale.dart'; +import 'package:corona_data/app/shared/utils/localization/locales/pt_br_locale.dart'; +import 'package:corona_data/app/shared/utils/localization/localization_interface.dart'; + +const kPTBR = "pt_br"; +const kENUS = "en_us"; + +final Map kLocales = { + kPTBR: PTBRLocale(), + kENUS: ENUSLocale(), + }; diff --git a/lib/app/shared/utils/localization/locales/en_us_locale.dart b/lib/app/shared/utils/localization/locales/en_us_locale.dart index c1a8a51..149bf8e 100644 --- a/lib/app/shared/utils/localization/locales/en_us_locale.dart +++ b/lib/app/shared/utils/localization/locales/en_us_locale.dart @@ -10,6 +10,5 @@ class ENUSLocale extends ILocalization{ @override String get name => 'United States'; - static const String key = "en_us"; } \ No newline at end of file diff --git a/lib/app/shared/utils/localization/locales/pt_br_locale.dart b/lib/app/shared/utils/localization/locales/pt_br_locale.dart index 708b52c..0ad417d 100644 --- a/lib/app/shared/utils/localization/locales/pt_br_locale.dart +++ b/lib/app/shared/utils/localization/locales/pt_br_locale.dart @@ -10,9 +10,4 @@ class PTBRLocale extends ILocalization{ @override String get name => 'Brazil'; - static const String key = "pt_br"; - - - - } \ No newline at end of file diff --git a/lib/app/shared/utils/localization/localization_utils.dart b/lib/app/shared/utils/localization/localization_utils.dart index 8355667..6c35a0b 100644 --- a/lib/app/shared/utils/localization/localization_utils.dart +++ b/lib/app/shared/utils/localization/localization_utils.dart @@ -9,21 +9,21 @@ class LocalizationUtils { locale = locale != null?locale.toLowerCase():''; if(locale == null){ - themeInstance = locales[defaultLocaleKey]; + themeInstance = Configuration.locales[Configuration.defaultLocaleKey]; } - else if (locales.containsKey(locale)) { - themeInstance = locales[locale]; + else if (Configuration.locales.containsKey(locale)) { + themeInstance = Configuration.locales[locale]; } else { - themeInstance = locales[defaultLocaleKey]; + themeInstance = Configuration.locales[Configuration.defaultLocaleKey]; } return themeInstance; } static List getLocaleNamesPretty(){ - return locales.keys.map((name)=>name.toUpperCase()).toList(); + return Configuration.locales.keys.map((name)=>name.toUpperCase()).toList(); } - static const defaultLocaleKey=PTBRLocale.key; + } \ No newline at end of file diff --git a/lib/app/shared/utils/theme/constants.dart b/lib/app/shared/utils/theme/constants.dart index e69de29..2ee4abf 100644 --- a/lib/app/shared/utils/theme/constants.dart +++ b/lib/app/shared/utils/theme/constants.dart @@ -0,0 +1,15 @@ +import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; +import 'package:corona_data/app/shared/utils/theme/themes/theme_dark.dart'; +import 'package:corona_data/app/shared/utils/theme/themes/theme_dracula.dart'; +import 'package:corona_data/app/shared/utils/theme/themes/theme_light.dart'; + +const String kLightThemeKey = "light"; +const String kDarkThemeKey = "dark"; +const String kDraculaThemeKey = "dracula"; + +final Map kThemes = { + kLightThemeKey: ThemeLight(), + kDarkThemeKey: ThemeDark(), + kDraculaThemeKey: ThemeDracula(), + }; + diff --git a/lib/app/shared/utils/theme/theme_utils.dart b/lib/app/shared/utils/theme/theme_utils.dart index 150884b..4c2b1dc 100644 --- a/lib/app/shared/utils/theme/theme_utils.dart +++ b/lib/app/shared/utils/theme/theme_utils.dart @@ -1,4 +1,5 @@ import 'package:corona_data/app/shared/config/config.dart'; +import 'package:corona_data/app/shared/utils/theme/constants.dart'; import 'package:corona_data/app/shared/utils/theme/theme_interface.dart'; class ThemeUtils { @@ -8,19 +9,20 @@ class ThemeUtils { themeName = themeName != null?themeName.toLowerCase():''; if(themeName == null){ - themeInstance = themes['dark']; + themeInstance = Configuration.themes[kDarkThemeKey]; + } - else if (themes.containsKey(themeName)) { - themeInstance = themes[themeName]; + else if (Configuration.themes.containsKey(themeName)) { + themeInstance = Configuration.themes[themeName]; } else { - themeInstance = themes['dark']; + themeInstance = Configuration.themes[kDarkThemeKey]; } return themeInstance.initialize(); } static List getThemeNamesPretty(){ - return themes.keys.map((name)=>name.toUpperCase()).toList(); + return Configuration.themes.keys.map((name)=>name.toUpperCase()).toList(); } } \ No newline at end of file diff --git a/test/app/mocks/local_storage_mock.dart b/test/app/mocks/local_storage_mock.dart index 2ae94ae..8850894 100644 --- a/test/app/mocks/local_storage_mock.dart +++ b/test/app/mocks/local_storage_mock.dart @@ -1,3 +1,4 @@ +import 'package:corona_data/app/shared/config/config.dart'; import 'package:corona_data/app/shared/models/country_model.dart'; import 'package:corona_data/app/shared/repositories/local_storage_interface.dart'; import 'package:corona_data/app/shared/utils/localization/localization_utils.dart'; @@ -12,7 +13,7 @@ class LocalStorageMock extends Mock implements ILocalStorage { (_) async => Future.value( CountryModel(name: "Brazil", code: "br")), ); when(this.getLocale()).thenAnswer( - (_) async => Future.value( LocalizationUtils.defaultLocaleKey), + (_) async => Future.value(Configuration.defaultLocaleKey), ); } }