首页 > 解决方案 > 使用 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<…&gt;

标签: flutterpackage

解决方案


您需要将 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();
  ...

推荐阅读