AlertDialog 不显示在 Column 小部件颤动上

时间:2021-03-03 13:18:09

标签: flutter flutter-alertdialog

我是 Flutter 学习的新手,这是我从一些教程中编写的代码。我尝试这样做。当用户位于数组的第一个或最后一个索引但 AlertDialog 未显示在屏幕上时显示 Run pipeline

任何人都可以为此指导我,或者可以让我知道我做错了什么。

AlertDialog

我在 import 'package:flutter/material.dart'; import './question.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { @override State<StatefulWidget> createState() { return _MyAppState(); } } // IF WE DON'T WANT TO ALLOW OTHER CLASS TO ACCESS THIS CLASS THEN WE CAN MAKE IT PRIVATE BY ADDING _ AT THE STARTING OF FUNCTIONS & VARIABLE class _MyAppState extends State<MyApp> { var _questionIndex = 0; //MARK: - NEXT QUESTION TAPPED void _btnNextQuestionTapped() { setState(() { if (_questionIndex < 2) { _questionIndex = _questionIndex + 1; print("Index Updated ==> " + _questionIndex.toString()); } else { _questionIndex = _questionIndex; print("Index At Last ==> " + _questionIndex.toString()); showAlertDialog("Alert", "You are at the Last Position"); } }); } //MARK: - PREVIOUS QUESTION TAPPED void _btnPreviousQuestionTapped() { setState(() { if (_questionIndex == 0) { _questionIndex = 0; print("Index At First ==> " + _questionIndex.toString()); showAlertDialog("Alert", "You are at the First Position"); } else { _questionIndex = _questionIndex - 1; print("Index Updated ==> " + _questionIndex.toString()); } }); } Future<void> showAlertDialog(String title, String message) async { return showDialog<void>( context: context, barrierDismissible: true, // user must tap button! builder: (BuildContext context) { return AlertDialog( title: Text('AlertDialog Title'), content: SingleChildScrollView( child: ListBody( children: <Widget>[ Text(title), Text(message), ], ), ), actions: <Widget>[ TextButton( child: Text('OK'), onPressed: () { // Navigator.of(context).pop(); }, ), ], ); }, ); } @override Widget build(BuildContext context) { var arrQuestions = [ "Which Technology is Good?", "Which development tool is more user friendly?", "Which language is easy to Learn?" ]; return MaterialApp( home: Scaffold( appBar: AppBar( title: Text("State Management Demo"), ), body: Column( children: [ Question(arrQuestions[_questionIndex]), RaisedButton( child: Text("Next Question"), onPressed: () => _btnNextQuestionTapped(), ), RaisedButton( child: Text("Previous Question"), onPressed: () => _btnPreviousQuestionTapped(), ), ], // mainAxisAlignment: MainAxisAlignment.center, // verticalDirection: VerticalDirection.up, ), ), ); } } () 和 showAlertDialog();()

中调用了 _btnNextQuestionTapped

在终端中获取以下日志。

<块引用>

VERBOSE-2:ui_dart_state.cc(177)] 未处理的异常:否 找到 MaterialLocalizations。 MyApp 小部件需要 由 Localizations 小部件提供的 MaterialLocalizations 祖先。材料库使用 Localizations 生成 消息、标签和缩写。介绍一个 MaterialLocalizations,或者在你的根目录使用 MaterialApp 应用程序自动包含它们,或添加本地化 带有 MaterialLocalizations 委托的小部件。特定的小部件 找不到 MaterialLocalizations 的祖先是:MyApp 这个小部件的祖先是:[root] #0 debugCheckHasMaterialLocalizations。 (包:flutter/src/material/debug.dart:74:7) #1 debugCheckHasMaterialLocalizations (package:flutter/src/material/debug.dart:94:4) #2 showDialog (package:flutter/src/material/dialog.dart:987:10) #3 _MyAppState.showAlertDialog (package:state_management_demo/main.dart:49:12) #4 _MyAppState._btnNextQuestionTapped。 (pac<…>

1 个答案:

答案 0 :(得分:0)

如我所见,这里的上下文存在问题,您能否在 showAlertDialog 中传递上下文作为从调用它的位置传递上下文。 它会解决它 像这样:- Future showAlertDialog(String title, String message,context)