0

如何修复 Flutter 中的底部导航?

使用上下文调用的 MediaQuery.Of() 不包含 MediaQuery.Bottom 导航不起作用。

此代码显示错误什么是使用上下文调用的不包含 MediaQuery 的 MediaQuery.Of()?

import 'package:flutter/material.dart';
void main(){
  runApp(Home());
}
class Home extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => _HomeState();



}
class _HomeState extends State<Home>{
  int currindex=0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Bottom Nav "),
      ),
      body: Container(),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currindex,
        items:[BottomNavigationBarItem(
    icon: Icon(Icons.home),
    title: Text("Home"),
    backgroundColor: Colors.blue

    ),
          BottomNavigationBarItem(
              icon: Icon(Icons.search),
              title: Text("Search"),
              backgroundColor: Colors.blue

          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.person),
              title: Text("Profile"),
              backgroundColor: Colors.blue

          ),],
        onTap: (index){
          setState(() {
            currindex=index;

          });
        },

    ));
  }


}

截屏

4

1 回答 1

0

这是因为你没有使用MaterialApp(),你需要MaterialApp()在你的小部件树中使用

看看这个演示......

    void main() => runApp(Home());

    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: //your Title,
          theme: //your theme,
          home: HomePage(),
        );
      }
    }

    class HomePage extends StatefulWidget{
      @override
      State<StatefulWidget> createState() => _HomePageState();
    }

//and your _HomePageState widget goes here
于 2020-03-20T07:22:13.910 回答