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

问题描述

我是 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

解决方法

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...