简体   繁体   English

如何修复“使用不包含 MediaQuery 的上下文调用 MediaQuery.of()”的错误?

[英]How can I fix the error of “MediaQuery.of() called with a context that does not contain a MediaQuery.”?

I don't know why this error comes out when I use "Tab" like below.我不知道为什么当我使用下面的“Tab”时会出现这个错误。 Whenever I tried to build the app the error screen comes out.每当我尝试构建应用程序时,都会出现错误屏幕。 Please let me know how to fix it.请让我知道如何解决它。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Tab'),
          bottom: TabBar(
            tabs: <Widget>[
              Tab(icon: Icon(Icons.tag_faces),),
              Tab(text: 'menu',),
              Tab(icon: Icon(Icons.info),text: 'menu',),
            ],
          ),
        ),
        body: TabBarView(
          children: <Widget>[
            Container(color: Colors.purple,),
            Container(color: Colors.purple,),
            Container(color: Colors.purple,),
          ],
        ),
      ),
    );
  }
}

You need to wrap DefaultTabController in MaterialApp.您需要将 DefaultTabController 包装在 MaterialApp 中。 If you visit the Scaffold class then you can see that it needs MediaQuery.如果您访问Scaffold class,那么您可以看到它需要 MediaQuery。 MediaQuery is defined in WidgetsApp class. MediaQuery 在WidgetsApp class 中定义。 Since the MaterialApp inherits from WidgetsApp hence wrapping our widget tree will allow the Scaffold to access MediaQuery class.由于 MaterialApp 继承自 WidgetsApp,因此包装我们的小部件树将允许 Scaffold 访问 MediaQuery class。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            title: Text('Tab'),
            bottom: TabBar(
              tabs: <Widget>[
                Tab(icon: Icon(Icons.tag_faces),),
                Tab(text: 'menu',),
                Tab(icon: Icon(Icons.info),text: 'menu',),
              ],
            ),
          ),
          body: TabBarView(
            children: <Widget>[
              Container(color: Colors.purple,),
              Container(color: Colors.purple,),
              Container(color: Colors.purple,),
            ],
          ),
        ),
      ),
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。)错误 - (MediaQuery.of() called with a context that does not contain a MediaQuery.) error 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 错误 - MediaQuery.of() called with a context that does not contain a MediaQuery. error 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 如何修复此 dart 代码? - MediaQuery.of() called with a context that does not contain a MediaQuery. How to fix this dart code? 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 (紧急援助) - MediaQuery.of() called with a context that does not contain a MediaQuery. (emergency aid) 使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - MediaQuery.of() called with a context that does not contain MediaQuery MediaQuery.of()使用不包含MediaQuery的上下文调用 - MediaQuery.of() called with a context that does not contain a MediaQuery Flutter 错误:使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery 在使用不包含 MediaQuery 的上下文调用的 flutter MediaQuery.of() 中出现错误 - Getting Error in flutter MediaQuery.of() called with a context that does not contain a MediaQuery 使用上下文调用的 Mediaquery.of 不包含 mediaquery 错误 - Mediaquery.of called with a context does not contain a mediaquery error Flutter 错误:使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - Flutter Error : MediaQuery.of() called with a context that does not contain a MediaQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM