Skip to content

Commit

Permalink
🌐 add flutter widget i18n support
Browse files Browse the repository at this point in the history
  • Loading branch information
H2Sxxa committed Aug 1, 2024
1 parent dfa9317 commit 64e89a0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
17 changes: 14 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import 'package:arche/arche.dart';
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:superuser/superuser.dart';
import 'package:system_fonts/system_fonts.dart';
import 'package:wslconfigurer/i18n/i18n.dart';
import 'package:wslconfigurer/models/config.dart';
import 'package:wslconfigurer/models/key.dart';
import 'package:wslconfigurer/views/pages/doctor.dart';
import 'package:wslconfigurer/views/pages/manage.dart';
import 'package:wslconfigurer/views/pages/settings.dart';
import 'package:wslconfigurer/views/widgets/basic.dart';
Expand Down Expand Up @@ -68,6 +70,7 @@ class MyAppState extends State<MyApp> with RefreshMountedStateMixin {
@override
Widget build(BuildContext context) {
var configs = ArcheBus.bus.of<AppConfigs>();
var locale = configs.locale.getOr("en_US").split("_");
return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) => MaterialApp(
theme: ThemeData(
Expand All @@ -88,6 +91,16 @@ class MyAppState extends State<MyApp> with RefreshMountedStateMixin {
key: rootKey,
),
),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale("en", "US"),
Locale("zh", "CN"),
],
locale: Locale(locale.first, locale.last),
debugShowCheckedModeBanner: false,
themeMode: configs.themeMode.getOrWrite(ThemeMode.system),
),
Expand Down Expand Up @@ -173,9 +186,7 @@ class HomePageState extends State<HomePage> with RefreshMountedStateMixin {
).toItem(icon: const Icon(Icons.apps)),
PageContainer(
title: context.i18n.getOrKey("doctor"),
child: const Center(
child: Text("TODO"),
),
child: const DoctorPage(),
).toItem(icon: const Icon(FontAwesomeIcons.userDoctor)),
PageContainer(
title: context.i18n.getOrKey("settings"),
Expand Down
15 changes: 15 additions & 0 deletions lib/views/pages/doctor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter/material.dart';

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

@override
State<StatefulWidget> createState() => _DoctorPageState();
}

class _DoctorPageState extends State<DoctorPage> {
@override
Widget build(BuildContext context) {
return const Column();
}
}
2 changes: 1 addition & 1 deletion lib/views/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class _SettingsPageState extends State<SettingsPage> {
var i18n = I18n();
await i18n.init(value);
ArcheBus.bus.replace<I18n>(i18n);
rootKey.currentState?.setState(() {});
appKey.currentState?.refreshMounted();
setState(() {});
},
itemBuilder: (context) => context.i18n.avaiableLanguages.entries
Expand Down
13 changes: 13 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.0"
flutter_localizations:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_markdown:
dependency: "direct main"
description:
Expand Down Expand Up @@ -200,6 +205,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0+1"
intl:
dependency: transitive
description:
name: intl
sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf
url: "https://pub.dev"
source: hosted
version: "0.19.0"
leak_tracker:
dependency: transitive
description:
Expand Down
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ environment:
dependencies:
flutter:
sdk: flutter

flutter_localizations:
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.6
Expand Down

0 comments on commit 64e89a0

Please sign in to comment.