当前位置:网站首页>flutter 报错 No MediaQuery widget ancestor found.
flutter 报错 No MediaQuery widget ancestor found.
2022-07-04 13:30:00 【刘刘小博】
报错如图

问题代码
import 'package:flutter/material.dart';
void main() {
runApp(SampleAppPage());
}
class SampleAppPage extends StatefulWidget {
const SampleAppPage({
Key? key}) : super(key: key);
@override
_SampleAppPageState createState() => _SampleAppPageState();
}
class _SampleAppPageState extends State<SampleAppPage>
with SingleTickerProviderStateMixin {
late AnimationController controller;
late CurvedAnimation curve;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: const Duration(milliseconds: 2000),
vsync: this,
);
curve = CurvedAnimation(
parent: controller,
curve: Curves.easeIn,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GestureDetector(
child: RotationTransition(
turns: curve,
child: const FlutterLogo(size: 200.0),
),
onDoubleTap: () {
if (controller.isCompleted) {
controller.reverse();
} else {
controller.forward();
}
},
),
),
);
}
}
原因: flutter 界面的根组件必须是MaterialApp
修复之后的代码
import 'package:flutter/material.dart';
void main() {
runApp(SampleApp());
}
class SampleApp extends StatelessWidget {
// This widget is the root of your application.
const SampleApp({
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Sample App',
home: SampleAppPage(),
);
}
}
class SampleAppPage extends StatefulWidget {
const SampleAppPage({
Key? key}) : super(key: key);
@override
_SampleAppPageState createState() => _SampleAppPageState();
}
class _SampleAppPageState extends State<SampleAppPage>
with SingleTickerProviderStateMixin {
late AnimationController controller;
late CurvedAnimation curve;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: const Duration(milliseconds: 2000),
vsync: this,
);
curve = CurvedAnimation(
parent: controller,
curve: Curves.easeIn,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GestureDetector(
child: RotationTransition(
turns: curve,
child: const FlutterLogo(size: 200.0),
),
onDoubleTap: () {
if (controller.isCompleted) {
controller.reverse();
} else {
controller.forward();
}
},
),
),
);
}
}
边栏推荐
- Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
- The performance of major mainstream programming languages is PK, and the results are unexpected
- Kubernets pod exists finalizers are always in terminating state
- Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design
- Node mongodb installation
- LVLG 8.2 circular scrolling animation of a label
- LVGL 8.2 Line wrap, recoloring and scrolling
- IO流:节点流和处理流详细归纳。
- Talk about 10 tips to ensure thread safety
- Halo effect - who says that those with light on their heads are heroes
猜你喜欢

UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks

Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态

An overview of 2D human posture estimation

各大主流编程语言性能PK,结果出乎意料

深度学习 神经网络案例(手写数字识别)

Scratch Castle Adventure Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022

03-存储系统

SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!

Ali was laid off employees, looking for a job n day, headhunters came bad news

如何搭建一支搞垮公司的技术团队?
随机推荐
Exploration and practice of eventbridge in the field of SaaS enterprise integration
Introduction to modern control theory + understanding
An overview of 2D human posture estimation
[algorithm leetcode] interview question 04.03 Specific depth node linked list (Multilingual Implementation)
EventBridge 在 SaaS 企业集成领域的探索与实践
Ali was laid off employees, looking for a job n day, headhunters came bad news
Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
LVGL 8.2 Draw label with gradient color
Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
Data Lake (13): spark and iceberg integrate DDL operations
各大主流编程语言性能PK,结果出乎意料
Query optimizer for SQL optimization
Summary of common problems in development
[C language] Pointer written test questions
How to build a technical team that will bring down the company?
Is it safe to open an account online for stock speculation? Will you be cheated.
LVGL 8.2 Line wrap, recoloring and scrolling
Leetcode 61: rotating linked list
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
Sqlserver functions, creation and use of stored procedures