【问题标题】:On bottom navigation bar when clicking on icons it is not navigating to other page在底部导航栏上单击图标时不会导航到其他页面
【发布时间】:2023-01-27 00:06:29
【问题描述】:

这是我的主页代码,但是当我点击底部导航栏图标时,我的导航不起作用

请帮我解决这个问题 除底部导航栏外,代码中的所有其他内容均正常工作

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 进行导航,请查看它 请帮我解决这个问题!

添加 body:_children[_currentindex] 后出现此错误;

image of the error I am facing

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您没有在 body 中指定页面

    Scaffold中的body更改为

    body: _children[_currentIndex];
    

    【讨论】:

    • 请参考更新后的答案,这绝对有效,确保您已删除 SingleChildScrollView 小部件并使用以下代码更新正文
    • 在哪里使用这个?正文:_children[_currentIndex];关于上面的代码你能告诉我吗!
    • 在您的代码中,您是否看到 body: SingleChildScrollView (.........) ?移除整个东西,并确保你的身体:只有这条线body: _children[_currentIndex];
    猜你喜欢
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多