android - 使用底部导航在flutter中的dart文件之间切换

标签 android flutter dart bottomnavigationview tap

我已经使用此示例代码来设计底部导航栏,并且现在已经可以使用,当我点击显示不同的dart文件的每个项目时,默认情况下显示第一页,但是在此代码中,我现在想要使用任何页面的显示按钮知道我该怎么做?

谢谢你们

这是代码:

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:store/pages/category.dart';
import 'package:store/pages/index_shop.dart'; 


void main() {
 runApp(
 MaterialApp(localizationsDelegates: [
  GlobalMaterialLocalizations.delegate,
  GlobalWidgetsLocalizations.delegate,
], supportedLocales: [
  Locale("en", "US"),
  Locale("fa", "IR"), // OR Locale('ar', 'AE') OR Other RTL locales
], debugShowCheckedModeBanner: false, home: Home()),
);
}

class Home extends StatefulWidget {
 @override
 State<StatefulWidget> createState() {
  return HomeState();
 }
 }

 class HomeState extends State<Home> {
   bool clickedCentreFAB = false; 
   int selectedIndex = 0; //to handle which item is currently selected in the bottom app bar
   String text = "Home";

  void updateTabSelection(int index, String buttonText) {
    setState(() {
    selectedIndex = index;
    text = buttonText;
    });
    }

   @override
   Widget build(BuildContext context) {
    return Scaffold(
     body: Stack(
     children: <Widget>[
       Align(
        alignment: FractionalOffset.center,

        child: RaisedButton(
          child: Text(text),
          onPressed: () {},
        ),
      ),

      Align(
        alignment: FractionalOffset.bottomCenter,
        child: AnimatedContainer(
          duration: Duration(milliseconds: 250),
          //if clickedCentreFAB == true, the first parameter is used. If it's false, the second.
          height:
          clickedCentreFAB ? MediaQuery.of(context).size.height : 10.0,
          width: clickedCentreFAB ? MediaQuery.of(context).size.height : 10.0,
          decoration: BoxDecoration(
              borderRadius:
              BorderRadius.circular(clickedCentreFAB ? 0.0 : 300.0),
              color: Colors.blue),
            ),
            )
            ],
            ),
            floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, 
            floatingActionButton: FloatingActionButton(
             onPressed: () {
             setState(() {
             clickedCentreFAB = !clickedCentreFAB; //to update the animated container
            });
             },
            tooltip: "Centre FAB",
            child: Container(
            margin: EdgeInsets.all(15.0),
            child: Icon(Icons.live_tv),
             ),
            elevation: 4.0,
             ),
             bottomNavigationBar: BottomAppBar(
             child: Container(
              margin: EdgeInsets.only(left: 12.0, right: 12.0),
            child: Row(
            mainAxisSize: MainAxisSize.max,
             mainAxisAlignment: MainAxisAlignment.spaceBetween,
             children: <Widget>[
           IconButton(
            //update the bottom app bar view each time an item is clicked
            onPressed: () {
              updateTabSelection(0, "Home");
            },
            iconSize: 27.0,
            icon: Icon(
              Icons.shopping_cart,
              //darken the icon if it is selected or else give it a different color
              color: selectedIndex == 0
                  ? Colors.blue.shade900
                  : Colors.grey.shade400,
            ),
          ),
          IconButton(
            onPressed: () {
              updateTabSelection(1, "Outgoing");
            },
            iconSize: 27.0,
            icon: Icon(
              Icons.dashboard,
              color: selectedIndex == 1
                  ? Colors.blue.shade900
                  : Colors.grey.shade400,
            ),
          ),
          //to leave space in between the bottom app bar items and below the FAB
          SizedBox(
            width: 50.0,
          ),
          IconButton(
            onPressed: () {
              updateTabSelection(2, "Incoming");
            },
            iconSize: 27.0,
            icon: Icon(
              Icons.shopping_basket,
              color: selectedIndex == 2
                  ? Colors.blue.shade900
                  : Colors.grey.shade400,
            ),
          ),
          IconButton(
            onPressed: () {
              updateTabSelection(3, "Settings");
            },
            iconSize: 27.0,
            icon: Icon(
              Icons.person,
              color: selectedIndex == 3
                  ? Colors.blue.shade900
                  : Colors.grey.shade400,
            ),
          ),
        ],
      ),
    ),
    //to add a space between the FAB and BottomAppBar
    shape: CircularNotchedRectangle(),
    //color of the BottomAppBar
    color: Colors.white,
  ),
);
 }
  }

最佳答案

您可以在“图标按钮”的“onPressed”方法上使用导航器
步骤1在MaterialApp上创建onGenerateRoute

return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: APP_NAME,
      home: GetStarted(),
      onGenerateRoute: (RouteSettings settings) {
        switch (settings.name) {
          case "category":
            return CupertinoPageRoute(
              builder: (BuildContext context) {
                return Category();
              },
              settings: settings,
            );
            break;

          case "index":
            return CupertinoPageRoute(
                builder: (BuildContext context) {
                  return Index();
                },
                settings: settings);
           break;
         }
      }
   );

步骤2在IconButton onPressed方法中添加导航

        Navigator.of(context).pushNamed("category");

关于android - 使用底部导航在flutter中的dart文件之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60441531/

相关文章:

Android - Kotlin - 对象必须声明为抽象或实现抽象成员

android - 获取我的个人数据联系人

Android:以阿拉伯语格式显示数字

java - 从任何网络Flutter访问HTTP服务器

android - 当父 Activity 调用 finishActivity() 时,结果代码为零

flutter - 在 ListTile 中打开一个新 View - Flutter

unit-testing - 为什么 `#import(" Dart :unittest")` can' t run?

dart - 如何在 flutter 中将泛型类型作为参数传递给 Future

swift - 将数据从flutter传递到原生IOS

flutter - 如何防止在父小部件 GestureDetector 上触发 onTapDown?