Flutter,从函数中显示 AlertDialog
Flutter, show AlertDialog from function
我有以下带有 _checkCreds 函数的代码。我想在按下该按钮时显示警报。
当我用 AlertDialog() 替换 print() 语句时,我收到“未找到 MaterialLocalizations”。
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
void _checkCreds(bool val) {
if(!val){
print("Warning, show alert here instead of print!");
return;
}
...
// Continue executing
}
Widget build(BuildContext context) {
return MaterialApp(
home: RaisedButton(
child: "Press me to trigger an alert!" ),
onPressed: () => _checkCreds(false),
);
}
}
我已经弄清楚了,因为我不知道“BuildContext”适合所有这些的 React 环境。经过进一步调查,我将当前上下文作为参数传递给调用警报的函数。
在 main() 之上:
Future<void> _ackAlert(BuildContext context) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Error'),
content: const Text('Please enter a valid text'),
actions: <Widget>[
FlatButton(
child: Text('Okay'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
在主要小部件内:
Widget build(BuildContext context) {
return MaterialApp(
home: RaisedButton(
child: "Press me to trigger an alert!" ),
// I've added a parameter that takes the current context
onPressed: () => _checkCreds(false, context),
);
}
我想这就是你想要的:
void _checkCreds(bool val){//put it inside 'MyAppState' class
if(!val){
showDialog(
barrierDismissible: true,//tapping outside dialog will close the dialog if set 'true'
context: context,
builder: (context){
return Dialog(
//Add code here
);
}
);
}
...
// Continue executing
}
AlertDialog
和 Dialog
具有相同的属性,除了 AlertDialog 具有 content
属性 而 Dialog 具有 child
属性。两者做同样的工作。
我有以下带有 _checkCreds 函数的代码。我想在按下该按钮时显示警报。
当我用 AlertDialog() 替换 print() 语句时,我收到“未找到 MaterialLocalizations”。
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
void _checkCreds(bool val) {
if(!val){
print("Warning, show alert here instead of print!");
return;
}
...
// Continue executing
}
Widget build(BuildContext context) {
return MaterialApp(
home: RaisedButton(
child: "Press me to trigger an alert!" ),
onPressed: () => _checkCreds(false),
);
}
}
我已经弄清楚了,因为我不知道“BuildContext”适合所有这些的 React 环境。经过进一步调查,我将当前上下文作为参数传递给调用警报的函数。
在 main() 之上:
Future<void> _ackAlert(BuildContext context) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Error'),
content: const Text('Please enter a valid text'),
actions: <Widget>[
FlatButton(
child: Text('Okay'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
在主要小部件内:
Widget build(BuildContext context) {
return MaterialApp(
home: RaisedButton(
child: "Press me to trigger an alert!" ),
// I've added a parameter that takes the current context
onPressed: () => _checkCreds(false, context),
);
}
我想这就是你想要的:
void _checkCreds(bool val){//put it inside 'MyAppState' class
if(!val){
showDialog(
barrierDismissible: true,//tapping outside dialog will close the dialog if set 'true'
context: context,
builder: (context){
return Dialog(
//Add code here
);
}
);
}
...
// Continue executing
}
AlertDialog
和 Dialog
具有相同的属性,除了 AlertDialog 具有 content
属性 而 Dialog 具有 child
属性。两者做同样的工作。