From 4f41aff572e752a13fc5b850c3377af75804633e Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 30 Jun 2025 23:25:56 +0200 Subject: [PATCH] change button language --- covas_mobile/l10n.yaml | 4 +++ covas_mobile/lib/classes/MyDrawer.dart | 39 ++++++++++++++++++++++++ covas_mobile/lib/l10n/app_en.arb | 4 +++ covas_mobile/lib/l10n/app_fr.arb | 4 +++ covas_mobile/lib/locale_provider.dart | 21 +++++++++++++ covas_mobile/lib/main.dart | 10 +++++- covas_mobile/lib/pages/ListItemMenu.dart | 21 +++++++++---- covas_mobile/pubspec.lock | 16 ++++++++++ covas_mobile/pubspec.yaml | 1 + 9 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 covas_mobile/l10n.yaml create mode 100644 covas_mobile/lib/l10n/app_en.arb create mode 100644 covas_mobile/lib/l10n/app_fr.arb create mode 100644 covas_mobile/lib/locale_provider.dart diff --git a/covas_mobile/l10n.yaml b/covas_mobile/l10n.yaml new file mode 100644 index 0000000..1437ccc --- /dev/null +++ b/covas_mobile/l10n.yaml @@ -0,0 +1,4 @@ +arb-dir: lib/l10n +template-arb-file: app_en.arb +output-localization-file: app_localizations.dart +output-class: AppLocalizations diff --git a/covas_mobile/lib/classes/MyDrawer.dart b/covas_mobile/lib/classes/MyDrawer.dart index 27cfe5e..8fea215 100644 --- a/covas_mobile/lib/classes/MyDrawer.dart +++ b/covas_mobile/lib/classes/MyDrawer.dart @@ -13,6 +13,9 @@ import '../pages/LoginDemo.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv import 'package:encrypt_shared_preferences/provider.dart'; +import 'package:provider/provider.dart'; +import '../locale_provider.dart'; + class MyDrawer extends StatelessWidget with ShowAlertDialog { Future logout(BuildContext context) async { SharedPreferences prefs = await SharedPreferences.getInstance(); @@ -135,6 +138,42 @@ class MyDrawer extends StatelessWidget with ShowAlertDialog { builder: (_) => EditProfile())); // Close the drawer }, ), + ListTile( + leading: Icon(Icons.language), + title: Text('Language'), + onTap: () { + showDialog( + context: context, + builder: (_) => AlertDialog( + title: Text('Select Language'), + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + ListTile( + leading: Icon(Icons.flag), + title: Text('Français'), + onTap: () { + Provider.of(context, listen: false) + .setLocale(const Locale('fr')); + Navigator.of(context).pop(); + }, + ), + ListTile( + leading: Icon(Icons.flag_outlined), + title: Text('English'), + onTap: () { + Provider.of(context, listen: false) + .setLocale(const Locale('en')); + Navigator.of(context).pop(); + }, + ), + ], + ), + ), + ); + }, + ), + ListTile( leading: Icon(Icons.info), title: Text('About'), diff --git a/covas_mobile/lib/l10n/app_en.arb b/covas_mobile/lib/l10n/app_en.arb new file mode 100644 index 0000000..a4abd5e --- /dev/null +++ b/covas_mobile/lib/l10n/app_en.arb @@ -0,0 +1,4 @@ +{ + "@@locale": "en", +"menu_list": "Event list menu" +} \ No newline at end of file diff --git a/covas_mobile/lib/l10n/app_fr.arb b/covas_mobile/lib/l10n/app_fr.arb new file mode 100644 index 0000000..ca19c58 --- /dev/null +++ b/covas_mobile/lib/l10n/app_fr.arb @@ -0,0 +1,4 @@ +{ + "@@locale": "fr", +"menu_list": "Liste d'évènement" +} \ No newline at end of file diff --git a/covas_mobile/lib/locale_provider.dart b/covas_mobile/lib/locale_provider.dart new file mode 100644 index 0000000..b8c9c7a --- /dev/null +++ b/covas_mobile/lib/locale_provider.dart @@ -0,0 +1,21 @@ +import 'package:flutter/material.dart'; + +class LocaleProvider with ChangeNotifier { + Locale _locale = const Locale('en'); + + Locale get locale => _locale; + + void setLocale(Locale locale) { + if (!L10n.all.contains(locale)) return; + + _locale = locale; + notifyListeners(); + } +} + +class L10n { + static final all = [ + const Locale('en'), + const Locale('fr'), + ]; +} diff --git a/covas_mobile/lib/main.dart b/covas_mobile/lib/main.dart index 76ea006..9f4634a 100644 --- a/covas_mobile/lib/main.dart +++ b/covas_mobile/lib/main.dart @@ -1,12 +1,20 @@ import 'package:flutter/material.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; +import 'package:provider/provider.dart'; + import 'pages/LoginDemo.dart'; +import 'locale_provider.dart'; // <-- à adapter selon ton arborescence void main() async { WidgetsFlutterBinding.ensureInitialized(); await MobileAds.instance.initialize(); - runApp(MyApp()); + runApp( + ChangeNotifierProvider( + create: (_) => LocaleProvider(), + child: MyApp(), + ), + ); } class MyApp extends StatelessWidget { diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index 521816e..42bd752 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -20,6 +20,9 @@ import '../classes/ad_helper.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; import '../classes/auth_service.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:provider/provider.dart'; +import '../locale_provider.dart'; // Créé plus loin void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -27,7 +30,10 @@ void main() async { await MobileAds.instance.initialize(); await initializeDateFormatting("fr_FR", null); - runApp(const MyApp()); + runApp(ChangeNotifierProvider( + create: (_) => LocaleProvider(), + child: const MyApp(), + )); } class MyApp extends StatelessWidget { @@ -35,16 +41,17 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { + final localeProvider = Provider.of(context); return MaterialApp( localizationsDelegates: [ + AppLocalizations.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], - supportedLocales: [ - const Locale('fr', 'FR'), - ], - home: const ListItemMenu(), + supportedLocales: [const Locale('fr', 'FR'), const Locale('en')], + locale: localeProvider.locale, + home: Builder(builder: (context) => ListItemMenu()), debugShowCheckedModeBanner: false, ); } @@ -608,9 +615,11 @@ class _MyHomePageState extends State { @override Widget build(BuildContext context) { + final loc = AppLocalizations.of(context); + final localeProvider = Provider.of(context); return Scaffold( appBar: AppBar( - title: const Text("Item list menu"), + title: Text(loc?.menu_list ?? "Item list menu"), backgroundColor: Colors.blue, foregroundColor: Colors.white, ), diff --git a/covas_mobile/pubspec.lock b/covas_mobile/pubspec.lock index 292dc71..311394d 100644 --- a/covas_mobile/pubspec.lock +++ b/covas_mobile/pubspec.lock @@ -565,6 +565,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.6" + nested: + dependency: transitive + description: + name: nested + sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20" + url: "https://pub.dev" + source: hosted + version: "1.0.0" path: dependency: "direct main" description: @@ -701,6 +709,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.9.1" + provider: + dependency: "direct main" + description: + name: provider + sha256: "4abbd070a04e9ddc287673bf5a030c7ca8b685ff70218720abab8b092f53dd84" + url: "https://pub.dev" + source: hosted + version: "6.1.5" shared_preferences: dependency: "direct main" description: diff --git a/covas_mobile/pubspec.yaml b/covas_mobile/pubspec.yaml index 6e0737c..614bc53 100644 --- a/covas_mobile/pubspec.yaml +++ b/covas_mobile/pubspec.yaml @@ -55,6 +55,7 @@ dependencies: mapbox_gl: ^0.16.0 google_mobile_ads: ^5.3.1 encrypt_shared_preferences: ^0.8.8 + provider: ^6.1.2 # ou la dernière version dev_dependencies: flutter_test: