flutter 在底部导航栏上,当点击图标时,不会导航到其他页面

cvxl0en2  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(162)

这是我的代码,我的主页,但我的导航是不工作时,我点击我的底部导航栏图标
请帮助我解决这个问题所有其他的东西在代码中工作正常,除了底部导航栏

import 'package:app_first/pages/Settings.dart';
import 'package:app_first/pages/about_us.dart';
import 'package:app_first/pages/contact_us.dart';
import 'package:app_first/pages/our_services.dart';
import 'package:app_first/utilities/route.dart';
import 'package:app_first/widgets/bottom_navigation.dart';
import 'package:app_first/widgets/drawer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;
  
  final List<Widget> _children = [
    MyHomePage(),
    Ourservices(),
    Aboutus(),
    Contactus(),
    SettingPage(),
  ];

  void onTappedBar(int index)
  {
    setState(() {
      _currentIndex = index;
    });
  }

  
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        title:Center(
         child: Text("FOODIES@CU"),
        ),
      ),
      body:  SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(height: 40, width: double.infinity,),
            Image.asset(
              "assets/images/background.jpg",
              fit: BoxFit.cover,
              width: 500,
            ),

            SizedBox(height: 40,),
            Container(
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(width: 2,color: Colors.orangeAccent,),
                )
              ),
              child: Text(
                "TOP RATED SHOPS",
                style: TextStyle(
                  
                  height: 2,
                    fontSize: 20,
                    color: Colors.deepOrangeAccent,
                    fontWeight: FontWeight.bold,
                   // backgroundColor: Colors.black,
                    ),
              ),
            ),
            SizedBox(height: 35,),
            Container
            (decoration: BoxDecoration(
              border: Border.all(width: 4),
            ),
              child: 
            Image.asset("assets/images/fr.jpg", fit: BoxFit.cover, width: 300, height: 200,),
            ),

            SizedBox(height: 20,),
            GestureDetector(
              onTap: (){
               Navigator.pushNamed(context, MyRoute.loginroute);
              },
              child: Container(
                child: Text("FOOD REPUBLIC" , style: 
                TextStyle(
                  color: Colors.brown,
                  fontSize: 20,
                ),),
              ),
            ),

             SizedBox(height: 35,),
            Container
            (decoration: BoxDecoration(
              border: Border.all(width: 4),
            ),
              child: 
            Image.asset("assets/images/fr.jpg", fit: BoxFit.cover, width: 300, height: 200,),
            ),

            SizedBox(height: 20,),
            GestureDetector(
              onTap: (){
               Navigator.pushNamed(context, MyRoute.loginroute);
              },
              child: Container(
                child: Text("FOOD REPUBLIC" , style: 
                TextStyle(
                  color: Colors.brown,
                  fontSize: 20,
                ),),
              ),
            )
          ],
        ),
          ),

           bottomNavigationBar: BottomNavigationBar(

        onTap: onTappedBar,
        currentIndex: _currentIndex,
        fixedColor: Colors.black,
        elevation: 100,
        items: [
          BottomNavigationBarItem(icon: Icon(EvaIcons.home, color: Colors.indigo,),
          label: 'Home',
          backgroundColor: Colors.white70
          ),

          BottomNavigationBarItem(icon: Icon(EvaIcons.shoppingCart, color: Colors.indigo,),
          label: 'Our services',
          backgroundColor: Colors.white70
          ),

          BottomNavigationBarItem(icon: Icon(EvaIcons.bookOpen, color: Colors.indigo,),
          label: 'About us',
          backgroundColor: Colors.white70
          ),

          BottomNavigationBarItem(icon: Icon(EvaIcons.phoneCall, color: Colors.indigo,),
          label: 'Contact us',
          backgroundColor: Colors.white70
          ),

          BottomNavigationBarItem(icon: Icon(EvaIcons.settings, color: Colors.indigo,),
          label: 'Settings',
          backgroundColor: Colors.white70
          ),
        ]
        ),     
      drawer: MyDrawer(),



      
      );
  }
}

在底部的导航栏,我正在使用onTapped导航,请看看它,并帮助我这个请!
我得到这个错误后,添加正文:_children[_currentindex];
image of the error I am facing

piv4azn7

piv4azn71#

您没有在body中指定页面
Scaffold中的body更改为

body: _children[_currentIndex];

最终代码:

import 'package:app_first/pages/Settings.dart';
import 'package:app_first/pages/about_us.dart';
import 'package:app_first/pages/contact_us.dart';
import 'package:app_first/pages/our_services.dart';
import 'package:app_first/utilities/route.dart';
import 'package:app_first/widgets/bottom_navigation.dart';
import 'package:app_first/widgets/drawer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;
  
  final List<Widget> _children = [
    MyHomePage(),
    Ourservices(),
    Aboutus(),
    Contactus(),
    SettingPage(),
  ];

  void onTappedBar(int index)
  {
    setState(() {
      _currentIndex = index;
    });
  }

  
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        title:Center(
         child: Text("FOODIES@CU"),
        ),
      ),
      body: _children[_currentIndex];
      bottomNavigationBar: BottomNavigationBar(

        onTap: onTappedBar,
        currentIndex: _currentIndex,
        fixedColor: Colors.black,
        elevation: 100,
        items: [
          BottomNavigationBarItem(icon: Icon(EvaIcons.home, color: Colors.indigo,),
          label: 'Home',
          backgroundColor: Colors.white70
          ),

          BottomNavigationBarItem(icon: Icon(EvaIcons.shoppingCart, color: Colors.indigo,),
          label: 'Our services',
          backgroundColor: Colors.white70
          ),

          BottomNavigationBarItem(icon: Icon(EvaIcons.bookOpen, color: Colors.indigo,),
          label: 'About us',
          backgroundColor: Colors.white70
          ),

          BottomNavigationBarItem(icon: Icon(EvaIcons.phoneCall, color: Colors.indigo,),
          label: 'Contact us',
          backgroundColor: Colors.white70
          ),

          BottomNavigationBarItem(icon: Icon(EvaIcons.settings, color: Colors.indigo,),
          label: 'Settings',
          backgroundColor: Colors.white70
          ),
        ]
        ),     
      drawer: MyDrawer(),
      
      );
  }
}

相关问题