当前位置:网站首页>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();
}
},
),
),
);
}
}
边栏推荐
- SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!
- C language book rental management system
- 毕业季-个人总结
- LVGL 8.2 keyboard
- C language course design questions
- Gin integrated Alipay payment
- IO流:节点流和处理流详细归纳。
- Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
- 产业互联网则具备更大的发展潜能,具备更多的行业场景
- Memory management summary
猜你喜欢
【C语言】指针笔试题
Nowcoder reverse linked list
LVGL 8.2 LED
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
Codeforce:c. sum of substrings
近一亿美元失窃,Horizon跨链桥被攻击事件分析
软件测试之测试评估
深度学习 神经网络案例(手写数字识别)
Practical puzzle solving | how to extract irregular ROI regions in opencv
Five minutes of machine learning every day: why do we need to normalize the characteristics of numerical types?
随机推荐
LVGL 8.2 Line wrap, recoloring and scrolling
深度学习 网络正则化
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!
如何搭建一支搞垮公司的技术团队?
Talk about 10 tips to ensure thread safety
No servers available for service: xxxx
【C语言】指针笔试题
Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design
Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
Ultrasonic distance meter based on 51 single chip microcomputer
Openresty current limiting
LVGL 8.2 keyboard
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]
开发中常见问题总结
炒股网上开户安全吗?会不会被骗。
Five minutes of machine learning every day: why do we need to normalize the characteristics of numerical types?
LVGL 8.2 Draw label with gradient color