gpt4 book ai didi

android - Flutter:未找到 MediaQuery 小部件祖先

转载 作者:行者123 更新时间:2023-12-04 00:52:32 25 4
gpt4 key购买 nike

import 'package:flutter/material.dart';

void main() {
runApp(Calculator());
}

class Calculator extends StatelessWidget {
final numpad_background_color = Color(0x212121);
final background_color = Colors.black;
final equal_button_background_color = Color(0xffbe00);

final textColor = Colors.white;
final operatorTextColor = Color(0xf3ba0e);

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: appbar(context),
body: Stack(
children: [Container(height: MediaQuery.of(context).size.height * 0.37), numpad(context)],
)));
}

Widget appbar(BuildContext context) {
return AppBar(title: Text("Rechner", style: TextStyle(color: textColor, fontSize: 15)), backgroundColor: background_color, leading: Icon(Icons.history));
}

Widget numpad(BuildContext context) {
return Container(decoration: BoxDecoration(borderRadius: BorderRadius.circular(5), color: numpad_background_color), child:
Column(children: [

],),);
}
}

Error: No MediaQuery ancestor could be found starting from the contextthat was passed to MediaQuery.of(). This can happen because you havenot added a WidgetsApp, CupertinoApp, or MaterialApp widget (thosewidgets introduce a MediaQuery), or it can happen if the context youuse comes from a widget above those widgets.


我不明白这个错误,我创建了一个 MaterialApp Widget 并从那里调用 MediaQuery,为什么会出现这个错误?

最佳答案

尝试创建另一个像这样的小部件

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Calculator(),
);
}
}
然后您的主要方法将如下所示
void main() {
runApp(MyApp());
}

关于android - Flutter:未找到 MediaQuery 小部件祖先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65399708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com