Skip to content

Commit

Permalink
feat: Adding different locales that switch according to the given tit…
Browse files Browse the repository at this point in the history
…le. #4
  • Loading branch information
LuchoTurtle committed May 9, 2023
1 parent 5755e01 commit 0dd3783
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
42 changes: 42 additions & 0 deletions lib/app_localization.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:app/settings.dart';
import 'package:flutter/material.dart';

/// Utils class for app localization with delegate
class AppLocalization {
late final Locale _locale;

AppLocalization(this._locale);

static AppLocalization of(BuildContext context) {
return Localizations.of<AppLocalization>(context, AppLocalization)!;
}

String getMenuItemTitle(MenuItemInfo item) {
final Map<String, dynamic> title = item.title;

return title[_locale.languageCode] ?? "";
}

static const LocalizationsDelegate<AppLocalization> delegate = _AppLocalizationDelegate();
}

/// Private overriden delegate class
class _AppLocalizationDelegate extends LocalizationsDelegate<AppLocalization> {
const _AppLocalizationDelegate();

// It will check if the user's locale is supported by our App or not
@override
bool isSupported(Locale locale) {
return ["en", "pt"].contains(locale.languageCode);
}

// It will load the equivalent json file requested by the user
@override
Future<AppLocalization> load(Locale locale) async {
AppLocalization appLocalization = AppLocalization(locale);
return appLocalization;
}

@override
bool shouldReload(_AppLocalizationDelegate old) => false;
}
5 changes: 3 additions & 2 deletions lib/dynamic_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:ui';

import 'package:flutter/material.dart';

import 'app_localization.dart';
import 'settings.dart';

/// Proxy decorator function that overrides the background color
Expand Down Expand Up @@ -146,7 +147,7 @@ class _MenuItemState extends State<MenuItem> {
child: ListTile(
contentPadding: EdgeInsets.only(left: widget.leftPadding),
leading: widget.info.getIcon(),
title: Text(widget.info.title,
title: Text(AppLocalization.of(context).getMenuItemTitle(widget.info),
style: TextStyle(
fontSize: 25,
color: widget.info.textColor,
Expand All @@ -164,7 +165,7 @@ class _MenuItemState extends State<MenuItem> {
// so they can be reordered on the same level.
child: ExpansionTile(
tilePadding: EdgeInsets.only(left: widget.leftPadding),
title: Text(widget.info.title,
title: Text(AppLocalization.of(context).getMenuItemTitle(widget.info),
style: TextStyle(
fontSize: 25,
color: widget.info.textColor,
Expand Down
20 changes: 20 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'app_localization.dart';
import 'menu.dart';

const iconKey = Key("menu_icon");
Expand All @@ -23,6 +25,24 @@ class App extends StatelessWidget {
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
supportedLocales: const [
Locale('en', 'US'),
Locale('pt', 'PT'),
],
localeResolutionCallback: (deviceLocale, supportedLocales) {
for (var locale in supportedLocales) {
if (locale.languageCode == deviceLocale!.languageCode && locale.countryCode == deviceLocale.countryCode) {
return deviceLocale;
}
}
return supportedLocales.first;
},
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
AppLocalization.delegate
],
home: const HomePage());
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class MenuItemInfoIcon {
}
}

/// Class holding the information of the tile
/// Class holding the information of the menu item tile
class MenuItemInfo {
late int id;
late int indexInLevel;
late String title;
late Map<String, dynamic> title;
late Color textColor;
late MenuItemInfoIcon? _icon;
late List<MenuItemInfo> tiles;
Expand Down Expand Up @@ -118,8 +118,8 @@ class MenuItemInfo {
});
}

_icon = null;
// Decoding `icon` field
_icon = null;
if (json['icon'] != null) {
_icon = MenuItemInfoIcon.fromJson(json['icon']);
}
Expand Down Expand Up @@ -154,7 +154,7 @@ class MenuItemInfo {
Future<List<MenuItemInfo>> loadMenuItems() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();

//await prefs.remove(storageKey);
await prefs.remove(storageKey);

final String? jsonStringFromLocalStorage = prefs.getString(storageKey);

Expand Down
13 changes: 13 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.1"
flutter_localizations:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -192,6 +197,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
intl:
dependency: transitive
description:
name: intl
sha256: "910f85bce16fb5c6f614e117efa303e85a1731bb0081edf3604a2ae6e9a3cc91"
url: "https://pub.dev"
source: hosted
version: "0.17.0"
js:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter


# The following adds the Cupertino Icons font to your application.
Expand Down

0 comments on commit 0dd3783

Please sign in to comment.