请我是颤振的初学者,我想在我的应用程序中添加本地化 - 我创建了一个下拉按钮,用户可以使用它来选择一种语言,并且应用程序将被翻译成该特定语言。从下拉按钮中选择一种语言后,我不断收到此错误:
“未找到 MaterialLocalizations。要引入 MaterialLocalizations,请在应用程序的根目录中使用 MaterialApp 来自动包含它们,或者添加一个带有 MaterialLocalizations 委托的本地化小部件。”
我试图将 MaterialApp 移动到我的应用程序的根目录,但我无法在那里实现提供程序。
这是我的 main.dart :
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import'package:fud_chatapp/controller/language_controller.dart';
import 'package:fud_chatapp/helper/helperfunctions.dart';
import 'package:fud_chatapp/l10n/l10n.dart';
import 'package:fud_chatapp/services/locale_provider.dart';
import 'package:fud_chatapp/services/signin_with_google.dart';
import 'package:fud_chatapp/view/choose_location_time.dart';
import 'package:fud_chatapp/view/home_time.dart';
import 'package:fud_chatapp/view/splashscreen.dart';
import 'package:provider/provider.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
(BuildContext context, AsyncSnapshot snapshot) {
return;
};
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool userIsLoggedIn;
@override
void initState() {
getLoggedInState();
super.initState();
}
getLoggedInState() async {
await HelperFunctions.getUserLoggedInSharedPreference().then((val) {
setState(() {
userIsLoggedIn = val;
});
});
}
@override
Widget build(BuildContext context)=> ChangeNotifierProvider(
create: (context) => LocaleProvider(),
builder: (context, child) {
final provider = Provider.of<LocaleProvider>(context);
return MaterialApp(
locale: provider.locale,
supportedLocales: L10n.all,
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
routes: {
//'/': (context) => LoadingPage(),
'/home': (context) => HomeTime(),
'/location': (context) => ChooseLocationTime(),
},
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.teal,
scaffoldBackgroundColor: Color(0xFF404040),
primarySwatch: Colors.teal,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: SplashScreen(),
);
});
}
这是 language_picker_widget.dart :
import 'package:flutter/material.dart';
import 'package:fud_chatapp/l10n/l10n.dart';
import 'package:fud_chatapp/services/locale_provider.dart';
import 'package:provider/provider.dart';
class LanguagePickerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final provider = Provider.of<LocaleProvider>(context);
final locale = provider.locale ?? Locale('en');
return DropdownButtonHideUnderline(
child: DropdownButton(
value: locale,
icon: Container(width: 12),
items: L10n.all.map(
(locale) {
final flag = L10n.getFlag(locale.languageCode);
return DropdownMenuItem(
child: Center(
child: Text(
flag,
style: TextStyle(fontSize: 32),
),
),
value: locale,
onTap: () {
final provider =
Provider.of<LocaleProvider>(context, listen: false);
provider.setLocale(locale);
},
);
},
).toList(),
onChanged: (_) {},
),
);
}
}
这是我的 appBar 小部件:
class appBarMain extends StatelessWidget implements PreferredSizeWidget
{
final double barHeight = 50.0;
final String title;
const appBarMain({Key key, this.title}) : super(key: key);
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight + barHeight);
@override
Widget build(BuildContext context) {
return AppBar(
title: SizedBox(
height: kToolbarHeight,
child: Image.asset(
"assets/images/logo.png",
),
),
toolbarHeight: kToolbarHeight,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(bottom:
Radius.circular(48.0))),
actions: [
LanguagePickerWidget(),
const SizedBox(width: 12,),
],
backgroundColor: Colors.teal,
//shape: CustomShapeBorder(),
centerTitle: true,
elevation: 0,
);
}
}
这是我的调试控制台:
No MaterialLocalizations found.
_DropdownMenu<Locale> widgets require MaterialLocalizations to be provided by a Localizations
widget ancestor.
The material library uses Localizations to generate messages, labels, and abbreviations.
To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application
to include them automatically, or add a Localization widget with a MaterialLocalizations
delegate.
The specific widget that could not find a MaterialLocalizations ancestor was:
_DropdownMenu<Locale>
dirty
dependencies: [ScrollConfiguration, _LocalizationsScope-[GlobalKey#1b759], _InheritedTheme]
state: _DropdownMenuState<Locale>#88cef
The ancestors of this widget were:
: MaterialApp
state: _MaterialAppState#9cf6a
: ChangeNotifierProvider<LocaleProvider>
value: Instance of 'LocaleProvider'
listening to value
: MyApp
state: _MyAppState#0fbda
...
The relevant error-causing widget was:
MaterialApp MaterialApp:file:///C:/apps/fud_chatapp/lib/main.dart:54:12
When the exception was thrown, this was the stack:
#0 debugCheckHasMaterialLocalizations.<anonymous closure>
(package:flutter/src/material/debug.dart:76:7)
#1 debugCheckHasMaterialLocalizations (package:flutter/src/material/debug.dart:96:4)
#2 _DropdownMenuState.build (package:flutter/src/material/dropdown.dart:269:12)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4782:27)
#4 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4665:15)
#5 StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4840:11)
#6 Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
#7 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:33)
#8 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#9 RendererBinding._handlePersistentFrameCallback
(package:flutter/src/rendering/binding.dart:319:5)
#10 SchedulerBinding._invokeFrameCallback
(package:flutter/src/scheduler/binding.dart:1143:15)
#11 SchedulerBinding.handleDrawFrame
(package:flutter/src/scheduler/binding.dart:1080:9)
#12 SchedulerBinding._handleDrawFrame
(package:flutter/src/scheduler/binding.dart:996:5)
#16 _invoke (dart:ui/hooks.dart:166:10)
#17 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#18 _drawFrame (dart:ui/hooks.dart:129:31)
(elided 3 frames from dart:async)
这是登录页面: