点击RaisedButton按钮,弹出一个对话框
来源:7-2 学习构建Flutter实例项目【他山之石可以攻玉】
慕移动9151266
2020-11-10
自己写的代码不报错,但是也弹不出对话框,网上找的都是局部代码,自己比较小白不知道怎么操作。以下为自己写的完整代码,安卓的,求找问题
import ‘package:flutter/material.dart’;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘’,
home: Scaffold(
body: Container(
child: RaisedButton(
child: Text(“对话框1”),
onPressed: () async {
//弹出对话框并等待其关闭
bool delete = await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(“提示”),
content: Text(“您确定要删除当前文件吗?”),
actions: [
FlatButton(
child: Text(“取消”),
onPressed: () => Navigator.of(context).pop(), // 关闭对话框
),
FlatButton(
child: Text(“删除”),
onPressed: () {
//关闭对话框并返回true
Navigator.of(context).pop(true);
},
),
],
);
},
);
if (delete == null) {
print("取消删除");
} else {
print("已确认删除");
//... 删除文件
}
},
),
)));
}
}
1回答
-
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: '', home: MyHomePage() ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return ( Scaffold( body: Container( child: RaisedButton( child: Text("对话框1"), onPressed: () async { //弹出对话框并等待其关闭 bool delete = await showDialog( context: context, builder: (context) { return AlertDialog( title: Text("提示"), content: Text("您确定要删除当前文件吗?"), actions: [ FlatButton( child: Text("取消"), onPressed: () => Navigator.of(context).pop(), // 关闭对话框 ), FlatButton( child: Text("删除"), onPressed: () { //关闭对话框并返回true Navigator.of(context).pop(true); }, ), ], ); }, ); if (delete == null) { print("取消删除"); } else { print("已确认删除"); //... 删除文件 } }, ), )) ); } }
把Scaffold单独抽成一个组件即可,实际是有报错的,`MyApp widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.`,参考链接https://stackoverflow.com/questions/56275595/no-materiallocalizations-found-myapp-widgets-require-materiallocalizations-to
112020-11-10
相似问题