flutter - 使用 rflutter 包时未找到 MaterialLocalizations
问题描述
我正在创建一个应用程序,该应用程序在到达列表末尾时显示弹出消息,但没有显示弹出消息我正在使用 rflutter 包来显示弹出消息。rflutter 包的链接 https://pub.dev/packages/rflutter_alert
你能帮忙吗 ?
代码
import 'package:demo/quiz_brain.dart';
import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
void main() {
runApp(QuizApp());
}
class QuizApp extends StatefulWidget {
const QuizApp({Key? key}) : super(key: key);
@override
_QuizAppState createState() => _QuizAppState();
}
List scorekeeper = [];
class _QuizAppState extends State {
QuizBrain quizObject = QuizBrain();
void checkAnswer(bool userPickedAnswer) {
bool correctanswer = quizObject.getquestionAnswer();
setState(() {
if (quizObject.isfinished() == true) {
Alert(
context: context,
title: 'Warning',
desc: 'You have reached the end of the questions',
).show();
quizObject.reset();
scorekeeper = [];
} else {
if (correctanswer == userPickedAnswer) {
scorekeeper.add(Icon(
Icons.check,
color: Colors.green,
));
} else {
scorekeeper.add(Icon(
Icons.close,
color: Colors.red,
));
}
quizObject.nextQuestion();
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.black,
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
flex: 5,
child: Center(
child: Text(
quizObject.getquestiontext(),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 30,
color: Colors.white,
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: TextButton(
onPressed: () {
checkAnswer(true);
},
style: TextButton.styleFrom(
backgroundColor: Colors.green,
),
child: Center(
child: Text(
'True',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: TextButton(
onPressed: () {
checkAnswer(false);
},
style: TextButton.styleFrom(
backgroundColor: Colors.red,
),
child: Center(
child: Text(
'False',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
),
),
),
),
Row(
children: scorekeeper,
),
],
),
),
),
);
}
}
错误
我在 android studio 上单击运行后收到此消息:
[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: No MaterialLocalizations found.
QuizApp widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
The material library uses Localizations to generate messages, labels, and abbreviations.
To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to include them automatically, or add a Localization widget with a MaterialLocalizations delegate.
The specific widget that could not find a MaterialLocalizations ancestor was:
QuizApp
The ancestors of this widget were:
[root]
#0 debugCheckHasMaterialLocalizations. (package:flutter/src/material/debug.dart:69:7)
#1 debugCheckHasMaterialLocalizations (package:flutter/src/material/debug.dart:89:4)
#2 MaterialLocalizations.of (package:flutter/src/material/material_localizations.dart:515:12)
#3 Alert.show (package:rflutter_alert/src/alert.dart:76:35)
#4 _QuizAppState.checkAnswer.<anonymous closure<…>
解决方案
您需要将 MaterialApp 移动到另一个小部件。因为 MaterialLocalizations 在 MaterialApp 中。您需要将 MaterialApp 的子项的上下文传递给 Alert。您提供的 QuizApp 的上下文是根上下文(通过 runApp(QuizApp()))。我们需要一个上下文,它的祖先是 MaterialApp。所以我们需要创建另一个小部件。
void main() {
runApp(QuizApp());
}
class QuizApp extends StatelessWidget {
const QuizApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: QuizHome(),
);
}
}
class QuizHome extends StatefulWidget {
const QuizHome({Key? key}) : super(key: key);
@override
_QuizHomeState createState() => _QuizHomeState();
}
class _QuizHomeState extends State<QuizHome> {
// Move rest of _QuizAppState to here
QuizBrain quizObject = QuizBrain();
...
推荐阅读
- matlab - 用于远程函数调用的最简单的网络协议?
- python - bazel 3.7.2 上用于在 MAC M1 上设置媒体管道的替代类型“darwin-arm64”可能是什么?
- php - 在 PHP 升级到 PHP8 之前识别类型杂耍问题
- php - 如何在同一网站的不同页面上使用具有相同密钥的 recaptcha?
- json - Azure DevOps 发布管道中如何使用基于环境的逻辑应用(单租户)项目的connections.json文件
- c++ - 查找适用于 HWND 的 RealTimeStylus
- ios - 如何从html字符串中删除所有标签?
- node.js - mongodb通过多个条件和一个结果nodejs多次查找
- python - 我正在尝试使用 python tkinter 模块将数据添加到 MysqlDB 中的表中。我创建了一个输入数据的用户界面。但它没有显示在数据库中
- load - promQL如何按核心数划分node_load