Flutter 颤振=>;showDialog/AlertDialog=>;未发现任何材料本地化
刚开始飞,但印象深刻。 如果PushNotification通过Firebase“onMessage”到达,我想显示一个对话框 但每次当我试图显示我的对话框时,都会出现一个异常“找不到MaterialLocalizations”。 为了进行测试,我添加了一个RaisedButton来显示此警报,但问题相同。 也许有人能帮我。 非常感谢 以下是我的小应用程序的全部代码:Flutter 颤振=>;showDialog/AlertDialog=>;未发现任何材料本地化,flutter,Flutter,刚开始飞,但印象深刻。 如果PushNotification通过Firebase“onMessage”到达,我想显示一个对话框 但每次当我试图显示我的对话框时,都会出现一个异常“找不到MaterialLocalizations”。 为了进行测试,我添加了一个RaisedButton来显示此警报,但问题相同。 也许有人能帮我。 非常感谢 以下是我的小应用程序的全部代码: import 'dart:async'; import 'package:firebase_messaging/firebas
import 'dart:async';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
void main() => runApp(Main());
class Main extends StatefulWidget {
@override
_MainState createState() => _MainState();
}
class _MainState extends State<Main> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
Widget _buildDialog(BuildContext context) {
print("_buildDialog");
return AlertDialog(
content: Text("Item has been updated"),
actions: <Widget>[
FlatButton(
child: const Text('CLOSE'),
onPressed: () {
Navigator.pop(context, false);
},
),
FlatButton(
child: const Text('SHOW'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
}
void _showPushDialog() {
print("DIALOG");
showDialog<bool>(
context: context,
builder: (_) => _buildDialog(context),
).then((bool shouldNavigate) {
if (shouldNavigate == true) {
_navigateToPushDetail();
}
});
}
void _navigateToPushDetail() {
print("TODO: Goto...");
}
@override
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
//_neverSatisfied();
_showPushDialog();
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_navigateToPushDetail();
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_navigateToPushDetail();
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
print("Push Messaging token: $token");
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
body: new Material(
child: Column(children: <Widget>[
Center(
child: Text('Hello World'),
),
RaisedButton(
onPressed: () {
print("pushed?");
_showPushDialog();
},
child: Text("press me"),
)
]),
),
),
);
}
}
导入'dart:async';
导入“package:firebase_messaging/firebase_messaging.dart”;
进口“包装:颤振/材料.省道”;
void main()=>runApp(main());
类Main扩展StatefulWidget{
@凌驾
_主状态createState()=>\u主状态();
}
类_MainState扩展状态{
最终FirebaseMessaging_FirebaseMessaging=FirebaseMessaging();
小部件构建对话框(构建上下文){
打印(“_buildDialog”);
返回警报对话框(
内容:文本(“项目已更新”),
行动:[
扁平按钮(
子项:const Text('CLOSE'),
已按下:(){
pop(上下文,false);
},
),
扁平按钮(
子项:常量文本('SHOW'),
已按下:(){
pop(上下文,true);
},
),
],
);
}
void_showPushDialog(){
打印(“对话框”);
显示对话框(
上下文:上下文,
生成器:()=>buildDialog(上下文),
).然后((布尔应该导航){
if(shouldNavigate==true){
_navigateToPushDetail();
}
});
}
void_navigateToPushDetail(){
打印(“TODO:Goto…”);
}
@凌驾
void initState(){
super.initState();
_firebaseMessaging.configure(
onMessage:(映射消息)异步{
打印(“onMessage:$message”);
//_永不满足();
_showPushDialog();
},
onLaunch:(映射消息)异步{
打印(“onLaunch:$message”);
_navigateToPushDetail();
},
onResume:(映射消息)异步{
打印(“onResume:$message”);
_navigateToPushDetail();
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(声音:true、徽章:true、警报:true));
_firebaseMessaging.onissettings已注册
.listen((IONotificationSettings){
打印(“已注册设置:$Settings”);
});
_firebaseMessaging.getToken().then((字符串标记){
断言(令牌!=null);
打印(“推送消息传递令牌:$token”);
});
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“欢迎来到弗利特”,
家:脚手架(
appBar:appBar(
标题:文本(“欢迎光临颤振”),
),
车身:新材料(
子项:列(子项:[
居中(
child:Text('Hello World'),
),
升起的按钮(
已按下:(){
打印(“按下?”);
_showPushDialog();
},
孩子:文本(“按我”),
)
]),
),
),
);
}
}
为了修复错误,您需要调用Main
类作为MaterialApp
的主参数,如下所示
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
debugShowCheckedModeBanner: false,
home: Main(),
);
}
}
&将Main
类中的生成方法更新为:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
body: Column(children: <Widget>[
Center(
child: Text('Hello World'),
),
RaisedButton(
onPressed: () {
print("pushed?");
_showPushDialog(context);
},
child: Text("press me"),
)
]),
);
}
@覆盖
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“欢迎光临颤振”),
),
正文:列(子项:[
居中(
child:Text('Hello World'),
),
升起的按钮(
已按下:(){
打印(“按下?”);
_showPushDialog(上下文);
},
孩子:文本(“按我”),
)
]),
);
}
flatter 1.0,Dart 2.x
此解决方案适用于无状态小部件
小部件和状态小部件
小部件
在顶部,您可以在声明中创建静态navKey
:
class MyApp extends StatefulWidget {
final String title; // sample var you want to pass to your widget
static final navKey = new GlobalKey<NavigatorState>();
const MyApp({Key navKey, this.title}) : super(key: navKey);
@override
State<StatefulWidget> createState() => _MyAppState();
}
因此,当您需要对话框或其他小部件的当前上下文时,在状态部分,您可以执行以下操作:
@override
void initState() {
final context = MyApp.navKey.currentState.overlay.context;
showMyCustomDialog(context);
...
super.initState();
}
检查您的
MaterialApp()
小部件,
如果您设置了localizationsDelegates
,您可能会遇到这个问题。
当我删除这些代码时,它会起作用
localizationsDelegates: [
DefaultCupertinoLocalizations.delegate,
],
我的密码
MaterialApp(
navigatorKey: navigatorKey,
title: 'test',
theme: ThemeData(
platform: TargetPlatform.iOS,
backgroundColor: Color(0xfff1f1f1),
accentColor: AppStyle.colorPrimary,
primaryColor: AppStyle.colorPrimary,
buttonColor: AppStyle.colorPrimary,
iconTheme: IconThemeData(color: Colors.white),
textTheme: TextTheme(
title: TextStyle(color: Colors.white),
),
primaryTextTheme: TextTheme(title: TextStyle(color: Colors.white)),
primaryIconTheme: const IconThemeData.fallback().copyWith(
color: Colors.white,
),
appBarTheme: AppBarTheme().copyWith(brightness: Brightness.dark),
),
home: LoginPage(),
debugShowCheckedModeBanner: false,
onGenerateRoute: AppRouter.router.generator,
)
但是你没有解释原因。。。原因是您在
Main
应用程序中收到的context
参数位于MaterialApp
小部件的上方,这是对话框使用的上下文,因此任何对本地化的搜索都将从MaterialApp
上方开始搜索,这是设置本地化的小部件。实际上,另一种解决方法是在两者之间放置一个Builder
小部件。
MaterialApp(
navigatorKey: navigatorKey,
title: 'test',
theme: ThemeData(
platform: TargetPlatform.iOS,
backgroundColor: Color(0xfff1f1f1),
accentColor: AppStyle.colorPrimary,
primaryColor: AppStyle.colorPrimary,
buttonColor: AppStyle.colorPrimary,
iconTheme: IconThemeData(color: Colors.white),
textTheme: TextTheme(
title: TextStyle(color: Colors.white),
),
primaryTextTheme: TextTheme(title: TextStyle(color: Colors.white)),
primaryIconTheme: const IconThemeData.fallback().copyWith(
color: Colors.white,
),
appBarTheme: AppBarTheme().copyWith(brightness: Brightness.dark),
),
home: LoginPage(),
debugShowCheckedModeBanner: false,
onGenerateRoute: AppRouter.router.generator,
)