change button language

This commit is contained in:
Valentin CZERYBA 2025-06-30 23:25:56 +02:00
parent 1c21f59420
commit 4f41aff572
9 changed files with 113 additions and 7 deletions

4
covas_mobile/l10n.yaml Normal file
View File

@ -0,0 +1,4 @@
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
output-class: AppLocalizations

View File

@ -13,6 +13,9 @@ import '../pages/LoginDemo.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv
import 'package:encrypt_shared_preferences/provider.dart'; import 'package:encrypt_shared_preferences/provider.dart';
import 'package:provider/provider.dart';
import '../locale_provider.dart';
class MyDrawer extends StatelessWidget with ShowAlertDialog { class MyDrawer extends StatelessWidget with ShowAlertDialog {
Future<void> logout(BuildContext context) async { Future<void> logout(BuildContext context) async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
@ -135,6 +138,42 @@ class MyDrawer extends StatelessWidget with ShowAlertDialog {
builder: (_) => EditProfile())); // Close the drawer 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<LocaleProvider>(context, listen: false)
.setLocale(const Locale('fr'));
Navigator.of(context).pop();
},
),
ListTile(
leading: Icon(Icons.flag_outlined),
title: Text('English'),
onTap: () {
Provider.of<LocaleProvider>(context, listen: false)
.setLocale(const Locale('en'));
Navigator.of(context).pop();
},
),
],
),
),
);
},
),
ListTile( ListTile(
leading: Icon(Icons.info), leading: Icon(Icons.info),
title: Text('About'), title: Text('About'),

View File

@ -0,0 +1,4 @@
{
"@@locale": "en",
"menu_list": "Event list menu"
}

View File

@ -0,0 +1,4 @@
{
"@@locale": "fr",
"menu_list": "Liste d'évènement"
}

View File

@ -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'),
];
}

View File

@ -1,12 +1,20 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:provider/provider.dart';
import 'pages/LoginDemo.dart'; import 'pages/LoginDemo.dart';
import 'locale_provider.dart'; // <-- à adapter selon ton arborescence
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
await MobileAds.instance.initialize(); await MobileAds.instance.initialize();
runApp(MyApp()); runApp(
ChangeNotifierProvider(
create: (_) => LocaleProvider(),
child: MyApp(),
),
);
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {

View File

@ -20,6 +20,9 @@ import '../classes/ad_helper.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart';
import '../classes/auth_service.dart'; import '../classes/auth_service.dart';
import 'package:flutter_localizations/flutter_localizations.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 { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
@ -27,7 +30,10 @@ void main() async {
await MobileAds.instance.initialize(); await MobileAds.instance.initialize();
await initializeDateFormatting("fr_FR", null); await initializeDateFormatting("fr_FR", null);
runApp(const MyApp()); runApp(ChangeNotifierProvider(
create: (_) => LocaleProvider(),
child: const MyApp(),
));
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
@ -35,16 +41,17 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final localeProvider = Provider.of<LocaleProvider>(context);
return MaterialApp( return MaterialApp(
localizationsDelegates: [ localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate, GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate, GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate, GlobalCupertinoLocalizations.delegate,
], ],
supportedLocales: [ supportedLocales: [const Locale('fr', 'FR'), const Locale('en')],
const Locale('fr', 'FR'), locale: localeProvider.locale,
], home: Builder(builder: (context) => ListItemMenu()),
home: const ListItemMenu(),
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
); );
} }
@ -608,9 +615,11 @@ class _MyHomePageState extends State<ListItemMenu> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final loc = AppLocalizations.of(context);
final localeProvider = Provider.of<LocaleProvider>(context);
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text("Item list menu"), title: Text(loc?.menu_list ?? "Item list menu"),
backgroundColor: Colors.blue, backgroundColor: Colors.blue,
foregroundColor: Colors.white, foregroundColor: Colors.white,
), ),

View File

@ -565,6 +565,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.6" version: "1.0.6"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
path: path:
dependency: "direct main" dependency: "direct main"
description: description:
@ -701,6 +709,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.9.1" 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: shared_preferences:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -55,6 +55,7 @@ dependencies:
mapbox_gl: ^0.16.0 mapbox_gl: ^0.16.0
google_mobile_ads: ^5.3.1 google_mobile_ads: ^5.3.1
encrypt_shared_preferences: ^0.8.8 encrypt_shared_preferences: ^0.8.8
provider: ^6.1.2 # ou la dernière version
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: