单击图标时,BottomNavigationBarItem打开相应的页面

时间:2018-12-20 08:51:43

标签: android dart flutter flutter-layout

当我单击浮动框中BottomNavigationBarItem中的Icon时,我正在寻找打开页面的方法。

我尝试使用索引编制,但我无法理解是我将所有图标都放在列表中然后如何获取。

 class CustomAppBar extends StatelessWidget {
 final List<BottomNavigationBarItem> bottomBarItems = [];

  final bottomNavigationBarItemStyle = TextStyle(fontStyle: 
    FontStyle.normal, color: Colors.black);

  CustomAppBar() {
   bottomBarItems.add(
   BottomNavigationBarItem(icon: Icon(Icons.home,color: Colors.brown,),
     title: Text("Explore", style: 
    bottomNavigationBarItemStyle.copyWith(color: Colors.green)),
     ),
  );
  bottomBarItems.add(new BottomNavigationBarItem(icon: new 
     Icon(Icons.favorite,color: Colors.black,),
      title: Text("Watchlist",style: bottomNavigationBarItemStyle,),
   ),
  );
   bottomBarItems.add(new BottomNavigationBarItem(icon: new 
      Icon(Icons.local_offer,color: Colors.black,),
       title: Text("Deals",style: bottomNavigationBarItemStyle,),
   ),
    );
  bottomBarItems.add(new BottomNavigationBarItem(icon: new 
   Icon(Icons.notifications,color: Colors.black,),
    title: Text("Notifications",style: bottomNavigationBarItemStyle,),
  ),
   );
   }

   @override
   Widget build(BuildContext context) {
   return Material(
     elevation: 15.0,
     child: BottomNavigationBar(
      items: bottomBarItems,
       type: BottomNavigationBarType.fixed,
      ),
     );
    }
   }

   /*class NewPage extends StatelessWidget {
  String title;
  NewPage(this.title);
   @override
   Widget build(BuildContext context) {
   return Scaffold(
   body: Center(child: Text(title),),

    );
    }
   }*/

4 个答案:

答案 0 :(得分:1)

我建议阅读这段简短的article,以了解如何使用BottomNavigationBar小部件。这是一个教程,可以帮助您入门。

简而言之,由于您要更改页面,因此必须向小部件提供状态。无状态窗口小部件将不知道当前哪个图标处于活动状态。 此外,您还必须向BottomNavigationBar小部件提供一个回调作为参数,该小部件将在点击图标时设置当前活动的索引,以便NavigationBar知道要显示哪个页面。

答案 1 :(得分:1)

这是您的解决方案,尽管我真的建议您查找教程或类似的东西以完全理解它。

import 'package:flutter/material.dart';

class YourApplication extends StatefulWidget {
  @override
  YourApplicationState createState() {
    return new YourApplicationState();
  }
}

class YourApplicationState extends State<YourApplication> {
  int index = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _getBody(index),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: index,
        onTap: (value) => setState(() => index = value),
        items: [
          BottomNavigationBarItem(
            icon: Icon(
              Icons.home,
            ),
            title: Text(
              "Explore",
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.favorite,
            ),
            title: Text(
              "Watchlist",
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.notifications,
            ),
            title: Text(
              "Notifications",
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.local_offer,
            ),
            title: Text(
              "Deals",
            ),
          ),
        ],
        type: BottomNavigationBarType.fixed,
      ),
    );
  }

  Widget _getBody(int index) {
    switch (index) {
      case 0:
        return _buildFirstPage(); // Create this function, it should return your first page as a widget
      case 1:
        return _buildSecondPage(); // Create this function, it should return your second page as a widget
      case 2:
        return _buildThirdPage(); // Create this function, it should return your third page as a widget
      case 3:
        return _buildFourthPage(); // Create this function, it should return your fourth page as a widget
    }

    return Center(child: Text("There is no page builder for this index."),);
  }
}

答案 2 :(得分:0)

使用 IconButton 而不是 Icon。举个例子:

BottomNavigationBarItem(
          icon: IconButton(
            icon: Icon(Icons.home),
            onPressed: () {
              Navigator.push(
               context,
                   MaterialPageRoute(builder: (context) => NewPage()),
              );
            },
          ),
        ),

答案 3 :(得分:0)

你也可以这样实现:

BottomNavigationBarItem(
  icon: IconButton(
   icon: Icon(Icons.home),
   onPressed: () {
     Navigator.of(context).push(
       MaterialPageRoute(
         builder: (context) => YourPage(),
       ),
     );
   },
 ),
 label: 'Home',
)