I can’t understand why I shall duplicate the initialization of localizations in my app and my HomePage stateful widget I call (I tried also to change my HomePage as a StatelessWidget it’s the same problem).
If I call the intialization in my HomepAge only it does not work, if I call the initailization in the App it does not work..
The only way I found to solve it’s to duplicate code in both, else I have a null localization.
Any idea? I don’t like duplicate code 😉
My code with duplicate init:
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import '';
import 'navbar.dart';
void main() {
runApp(const PApp());
}
MyApp :
class PApp extends StatelessWidget {
const PApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
Locale('en'),
Locale('fr'),
],
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const PHomePage(title: ''),
);
}
}
Homepage :
class PHomePage extends StatefulWidget {
const PHomePage({super.key, required this.title});
final String title;
@override
State<PHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<PHomePage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale('en'),
Locale('fr'),
],
home: Scaffold(
drawer: Navbar(),
appBar: AppBar(
title: Text(AppLocalizations.of(context)!.maintitle),
),
body: const Center(
child: Text('',
style : TextStyle(fontSize: 40.0)
),
),
),
);
}
}
Andal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.