当前位置:网站首页>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();
}
},
),
),
);
}
}
边栏推荐
- Xcode abnormal pictures cause IPA packet size problems
- 自动控制原理快速入门+理解
- Node mongodb installation
- 深度学习 神经网络案例(手写数字识别)
- Exploration and practice of eventbridge in the field of SaaS enterprise integration
- Leetcode T48: rotating images
- 炒股网上开户安全吗?会不会被骗。
- Query optimizer for SQL optimization
- A keepalived high availability accident made me learn it again
- C language personal address book management system
猜你喜欢

Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?

Kubernets pod exists finalizers are always in terminating state

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

Is BigDecimal safe to calculate the amount? Look at these five pits~~

LVGL 8.2 LED

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

How to build a technical team that will bring down the company?

Digi重启XBee-Pro S2C生产,有些差别需要注意

Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)

Gin integrated Alipay payment
随机推荐
LVGL 8.2 List
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
First experience of ViewModel
Details of FPGA underlying resources
Respect others' behavior
深度学习 神经网络案例(手写数字识别)
LVGL 8.2 LED
智能客服赛道:网易七鱼、微洱科技打法迥异
Introduction to asynchronous task capability of function calculation - task trigger de duplication
LVGL 8.2 Sorting a List using up and down buttons
Compile oglpg-9th-edition source code with clion
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
LVLG 8.2 circular scrolling animation of a label
C language achievement management system for middle school students
LVGL 8.2 keyboard
PLC Analog input analog conversion FC s_ ITR (CoDeSys platform)
Quick introduction to automatic control principle + understanding
LVGL 8.2 Line wrap, recoloring and scrolling
5G电视难成竞争优势,视频资源成中国广电最后武器