如何从某些特定路线中删除持久的底部导航栏?

Dan*_*l.V 6 dart flutter

我希望在我的整个应用程序中都有持久的底部导航栏,所以在搜索了几个小时后,我找到了一个解决方案。我从这篇博文中受到启发,并编写了我的解决方案代码Flutter?-?navigating off the charts

import 'package:flutter/material.dart';
import './login/login.dart';
import './alerts/alerts.dart';
import './home/home.dart';
import './Theme.dart';
import './settings/settings.dart';
import './enroll/enroll.dart';
import './add_device/add_device.dart';
import './eachDevice/index.dart';
import './device_settings/device_settings.dart';
import 'splash_screen/splash_screen.dart';
import './geofences/geofence_list.dart';
import './geofences/draw_geofence.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import './home/second_navigation_bar.dart';
import 'dart:io';
import 'package:path/path.dart';
void main() {
  GlobalKey<NavigatorState> navigator = new GlobalKey<NavigatorState>();
  HttpOverrides.global = new AppHttpOverrides();
  Map<String, WidgetBuilder> _routes = <String, WidgetBuilder>{
    "/alerts": (BuildContext context) => new Alerts(),
    "/login": (BuildContext context) => new LoginPage(),
    "/settings": (BuildContext context) => new Settings(),
    "/enroll": (BuildContext context) => new Enroll(),
    "/add_device": (BuildContext context) => new AddDevice(),
    "/history": (BuildContext context) => new History(),
    "/home": (BuildContext context) => new Home(),
    "/device_settings": (BuildContext context) => new DeviceSettings(),
    "/geofence_list": (BuildContext context) => new GeofenceList(),
    "/draw_geofence": (BuildContext context) => new DrawGeofence(),
  };

  runApp(new MaterialApp(
    navigatorKey: navigator,
    home: new SplashScreen(),
    builder: (context, child) {
      return new Scaffold(
          body: child,
          bottomNavigationBar:myBottomNavigationBar(),
          resizeToAvoidBottomPadding: false
      );
    },
    theme: buildTheme(),
    routes: _routes,
  ));
}
Run Code Online (Sandbox Code Playgroud)

这段代码运行良好,我在所有应用程序页面中都有静态底部导航栏,但是我想在某些路由(如登录页面)中排除底部导航栏,如何使用这种方法排除某些特定页面的底部导航栏。

小智 7

Navigator.of(context, rootNavigator: true).pushReplacement(MaterialPageRoute(builder: (context) => new LoginActivity()));
Run Code Online (Sandbox Code Playgroud)


小智 0

声明variablebottomNavigationBar content类似

var navContent;
Run Code Online (Sandbox Code Playgroud)

创建排除底部导航栏的方法

excludeBottomNavigationBar(){
    return Container(
      height: 0.0,
    );
  } 
Run Code Online (Sandbox Code Playgroud)

现在,您需要根据您的要求分配bottomNavigationBar内容,排除登录页面的bottomNavigationBar

import 'package:flutter/material.dart';
  import './login/login.dart';
  import './alerts/alerts.dart';
  import './home/home.dart';
  import './Theme.dart';
  import './settings/settings.dart';
  import './enroll/enroll.dart';
  import './add_device/add_device.dart';
  import './eachDevice/index.dart';
  import './device_settings/device_settings.dart';
  import 'splash_screen/splash_screen.dart';
  import './geofences/geofence_list.dart';
  import './geofences/draw_geofence.dart';
  import 'package:firebase_messaging/firebase_messaging.dart';
  import './home/second_navigation_bar.dart';
  import 'dart:io';
  import 'package:path/path.dart';
  void main() {
    GlobalKey<NavigatorState> navigator = new GlobalKey<NavigatorState>();
    HttpOverrides.global = new AppHttpOverrides();
    var navContent;

    excludeBottomNavigationBar(){
      return Container(
        height: 0.0,
      );
    }

    Map<String, WidgetBuilder> _routes = <String, WidgetBuilder>{
      "/alerts": (BuildContext context){
        navContent = myBottomNavigationBar();
        new Alerts();
      },
      "/login": (BuildContext context){
        navContent = excludeBottomNavigationBar();
        new LoginPage();
      },
      "/settings": (BuildContext context){
        navContent = myBottomNavigationBar();
        new Settings();
      },
      "/enroll": (BuildContext context){
        navContent = myBottomNavigationBar();
        new Enroll();
      },
      "/add_device": (BuildContext context){
        navContent = myBottomNavigationBar();
        new AddDevice();
      },
      "/history": (BuildContext context){
        navContent = myBottomNavigationBar();
        new History();
      },
      "/home": (BuildContext context){
        navContent = myBottomNavigationBar();
        new Home();
      },
      "/device_settings": (BuildContext context){
        navContent = myBottomNavigationBar();
        new DeviceSettings()
      },
      "/geofence_list": (BuildContext context){
        navContent = myBottomNavigationBar();
        new GeofenceList()
      },
      "/draw_geofence": (BuildContext context){
        navContent = myBottomNavigationBar();
        new DrawGeofence()
      },
    };

    runApp(new MaterialApp(
      navigatorKey: navigator,
      home: new SplashScreen(),
      builder: (context, child) {
        return new Scaffold(
            body: child,
            bottomNavigationBar:navContent,
            resizeToAvoidBottomPadding: false
        );
      },
      theme: buildTheme(),
      routes: _routes,
    ));
  }
Run Code Online (Sandbox Code Playgroud)