如何在颤动中修复底部导航?

问题描述 投票:0回答:1

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

MediaQuery.Of()与上下文一起调用,其中不包含MediaQuery.Bottom Navigation不起作用。

此代码显示错误,在不包含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;

          });
        },

    ));
  }


}

Screenshot

flutter flutter-layout flutter-bottomnavigation
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
© www.soinside.com 2019 - 2024. All rights reserved.