Flutter 创建警报对话框
我试图打开警报,但出现错误,怎么了 未处理的异常:未找到MaterialLocalization。MyApp小部件 要求本地化小部件提供MaterialLocalization 祖先Flutter 创建警报对话框,flutter,Flutter,我试图打开警报,但出现错误,怎么了 未处理的异常:未找到MaterialLocalization。MyApp小部件 要求本地化小部件提供MaterialLocalization 祖先 导入“包装:颤振/材料.省道”; void main()=>runApp(MyApp()); 类MyApp扩展了StatefulWidget{ @凌驾 _MyAppState createState()=>\u MyAppState(); } 类MyAppState扩展了状态{ void\u getcsvdocum
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
void\u getcsvdocument(BuildContext ctx)异步{
如果(ctx!=null)_showAlert(ctx,“Hello world”);
}
Future\u showAlert(BuildContext上下文,字符串消息)异步{
返回显示对话框(
上下文:上下文,
子:新建警报对话框(
标题:新文本(消息),
行动:[
新扁平按钮(
onPressed:()=>Navigator.pop(上下文),子级:新文本('Ok'))
],
));
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“插件示例应用程序”),
),
正文:中(
子:列(
儿童:[
扁平按钮(
子项:文本(“显示警报”),
按下:()=>\u获取CSVDocument(
上下文),//显示来自_getcsvdocument的警报
),
],
),
),
),
);
}
}
在您的showDialog
小部件中包含builder:
Builder(
builder: (context) {
return FlatButton(
child: Text('Show alert'),
onPressed: () => _getCsvDocunent(context),
);
},
)
物料警报的示例代码
Future<void> _materialAlertInit(BuildContext context) async {
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return _materialAlert(context);
},
);
}
_materialAlert(BuildContext context) {
return AlertDialog(
title: Text('Material Alert'),
content: Text('Android material designa alert'),
actions: <Widget>[
FlatButton(
child: Text('Yes'),
onPressed: () {
Navigator.pop(context);
},
),
FlatButton(
child: Text('No'),
onPressed: () {
Navigator.pop(context);
},
)
],
elevation: 24.0,
backgroundColor: Colors.white,
);
}
Future\u materialalertini(构建上下文)异步{
返回显示对话框(
上下文:上下文,
禁止:错误,
生成器:(BuildContext上下文){
返回材料列表(上下文);
},
);
}
_materialAlert(构建上下文){
返回警报对话框(
标题:文本(“材料警报”),
内容:文本(“安卓材质设计警报”),
行动:[
扁平按钮(
子项:文本('Yes'),
已按下:(){
Navigator.pop(上下文);
},
),
扁平按钮(
child:Text('No'),
已按下:(){
Navigator.pop(上下文);
},
)
],
标高:24.0,
背景颜色:Colors.white,
);
}
这是因为在返回showDialog
时传递的上下文
无法找到MaterialLocalization
小部件。正如错误所说,MyApp
小部件需要MaterialLocalization
,这意味着我们需要在MyApp
类中返回MaterialApp
,而不是在您提供的构建方法中返回。工作代码示例如下:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Test",
home: Home(),
);
}
}
class Home extends StatefulWidget {
@override
State createState() => _HomeState();
}
class _HomeState extends State<Home> {
void _getCsvDocunent(BuildContext ctx) async {
if (ctx != null) _showAlert(ctx, 'Hello world');
}
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“测试”,
home:home(),
);
}
}
类Home扩展了StatefulWidget{
@凌驾
State createState()=>_HomeState();
}
类(HomeState扩展状态){
void\u getcsvdocument(BuildContext ctx)异步{
如果(ctx!=null)_showAlert(ctx,“Hello world”);
}
这将按预期显示对话框:
使用
Builder
小部件包装FlatButton
Builder(
builder: (context) {
return FlatButton(
child: Text('Show alert'),
onPressed: () => _getCsvDocunent(context),
);
},
)
带有两个按钮的警报对话框
confirmationPopup(BuildContext context1) {
showDialog(
context: context1,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Alert"),
content: Text("Are you sure you want to logout?"),
actions: <Widget>[
FlatButton(
child: Text("No"),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text("Yes"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
确认弹出窗口(BuildContext上下文1){
显示对话框(
上下文:context1,
生成器:(BuildContext上下文){
返回警报对话框(
标题:文本(“警报”),
内容:文本(“您确定要注销吗?”),
行动:[
扁平按钮(
子项:文本(“否”),
已按下:(){
Navigator.of(context.pop();
},
),
扁平按钮(
子项:文本(“是”),
已按下:(){
Navigator.of(context.pop();
},
)
],
);
});
}
这可能有助于您将代码放在何处?@A.Shukri检查回购协议的实施情况如果您有空闲时间,请在此链接上帮助我。。