Flutter如何在另一个页面上显示对话框



我想显示一个对话框,当我按下按钮时,我想被导航到另一个页面,然后我想显示一个对话框,表明我已经导航到那个页面。

所以我试试这段代码;

InkWell(
onTap: () async {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => BottomBarD()),
(route) => false);
Dialog(
child: Container(
child: Column(
children: [
Text(
"Navigated to Another Page",
style: TextStyle(
fontFamily: "Quando",
fontWeight: FontWeight.w500,
fontSize: 15,
),
),
TextButton(
onPressed: () async {
Navigator.of(context).pop();
},
child: Text(
"Okey",
style: TextStyle(
fontFamily: "Quando",
fontSize: 15,
),
),
),
],
),
),
);
},
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.orangeAccent, Colors.red],
begin: Alignment.bottomRight,
end: Alignment.centerLeft),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(112.0),
topLeft: Radius.circular(112.0),
bottomRight: Radius.circular(112.0),
topRight: Radius.circular(112.0),
),
),
height: MediaQuery.of(context).size.height * 0.065,
width: MediaQuery.of(context).size.width * 0.80,
margin: EdgeInsets.only(
top: 30.0,
bottom: 10.0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text(
"Navigate and ShowDialog Button",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 17,
fontFamily: "Quando"),
),
),
],
),
),
),

但是当我在对话框中按下ok按钮时,ok按钮不起作用并且它给出了那个错误;

未处理异常:Null检查操作符用于空值

这在DartPad上运行良好:

void _incrementCounter() {
Navigator.of(context).push(MaterialPageRoute(
builder: (c) => Scaffold(body: Text('new page'))
));
showDialog(context: context, 
builder: (c) => AlertDialog(content: Text('the dialog in it'),)
);
}

注意,.push()立即返回一个Future。之后显示的对话框显示在第2页的顶部,因为我们没有等待它。

我把对话框放在我的第二页BottomBarD()中,每次BottomBarD()加载时,我都根据一个条件字段显示它。

最新更新