当前位置: 首页 > 知识库问答 >
问题:

颤振导航推送,同时保持相同的Appbar

燕航
2023-03-14

我目前正在构建一个Flutter应用程序,我正在努力找出实现导航的最佳方式。

    null

因此,行为是正确的,因为单击按钮后,ProfilePage会移到主页上。

我的问题是,图形上的appbar应该保持不变,但当个人资料页面被推送时,动画清楚地表明它不是同一个appbar。

>

  • 是否可以动画配置文件页面的条目,而不动画AppBar的重建?

    当第二个路由建立在第一个路由之上时,一个新的Appbar建立在前一个路由之上。但是如果我需要appbar保持不变呢?

  • 共有1个答案

    锺离韬
    2023-03-14

    可以使用Navigator类创建子导航器。

    我在当前项目中创建了一个路由库(routes.dart),用于在仍显示BottomNavigationBar时导航到其他屏幕。使用同样的思想,您可以在使用相同的appbar的同时执行导航。

    以下是您的场景的示例代码。

    import 'package:flutter/material.dart';
    import 'package:flutter2sample/routes.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          navigatorKey: Routes.rootNavigatorKey,
          initialRoute: Routes.PAGE_INITIAL,
          onGenerateRoute: Routes.onGenerateRoute,
        );
      }
    }
    
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:flutter2sample/pages/home_page.dart';
    import 'package:flutter2sample/pages/initial_page.dart';
    import 'package:flutter2sample/pages/main_page.dart';
    import 'package:flutter2sample/pages/profile_page.dart';
    
    class Routes {
      Routes._();
    
      static const String PAGE_INITIAL = '/';
      static const String PAGE_MAIN = '/main';
      static const String PAGE_HOME = '/home';
      static const String PAGE_PROFILE = '/profile';
    
      static final GlobalKey<NavigatorState> rootNavigatorKey =
          GlobalKey<NavigatorState>();
      static final GlobalKey<NavigatorState> mainNavigatorKey =
          GlobalKey<NavigatorState>();
    
      static String currentSubNavigatorInitialRoute;
    
      static CupertinoPageRoute<Widget> onGenerateRoute(RouteSettings settings) {
        Widget page;
    
        switch (settings.name) {
          case PAGE_INITIAL:
            page = InitialPage();
            break;
          case PAGE_MAIN:
            page = MainPage();
            break;
          case PAGE_HOME:
            page = HomePage();
            break;
          case PAGE_PROFILE:
            page = ProfilePage();
            break;
        }
    
        if (settings.name == PAGE_INITIAL &&
            currentSubNavigatorInitialRoute != null) {
          // When current sub-navigator initial route is set,
          // do not display initial route because it is already displayed.
          return null;
        }
    
        return CupertinoPageRoute<Widget>(
          builder: (_) {
            if (currentSubNavigatorInitialRoute == settings.name) {
              return WillPopScope(
                onWillPop: () async => false,
                child: page,
              );
            }
    
            return page;
          },
          settings: settings,
        );
      }
    
      /// [MaterialApp] navigator key.
      ///
      ///
      static NavigatorState get rootNavigator => rootNavigatorKey.currentState;
    
      /// [PAGE_MAIN] navigator key.
      ///
      ///
      static NavigatorState get mainNavigator => mainNavigatorKey.currentState;
    
      /// Navigate to screen via [CupertinoPageRoute].
      ///
      /// If [navigator] is not set, it will use the [rootNavigator].
      static void push(Widget screen, {NavigatorState navigator}) {
        final CupertinoPageRoute<Widget> route = CupertinoPageRoute<Widget>(
          builder: (_) => screen,
        );
    
        if (navigator != null) {
          navigator.push(route);
          return;
        }
    
        rootNavigator.push(route);
      }
    
      /// Navigate to route name via [CupertinoPageRoute].
      ///
      /// If [navigator] is not set, it will use the [rootNavigator].
      static void pushNamed(
        String routeName, {
        NavigatorState navigator,
        Object arguments,
      }) {
        if (navigator != null) {
          navigator.pushNamed(routeName, arguments: arguments);
          return;
        }
    
        rootNavigator.pushNamed(routeName, arguments: arguments);
      }
    
      /// Pop current route of [navigator].
      ///
      /// If [navigator] is not set, it will use the [rootNavigator].
      static void pop<T extends Object>({
        NavigatorState navigator,
        T result,
      }) {
        if (navigator != null) {
          navigator.pop(result);
          return;
        }
    
        rootNavigator.pop(result);
      }
    }
    
    //--------------------------------------------------------------------------------
    /// A navigator widget who is a child of [MaterialApp] navigator.
    ///
    ///
    class SubNavigator extends StatelessWidget {
      const SubNavigator({
        @required this.navigatorKey,
        @required this.initialRoute,
        Key key,
      }) : super(key: key);
    
      final GlobalKey<NavigatorState> navigatorKey;
      final String initialRoute;
    
      @override
      Widget build(BuildContext context) {
        final _SubNavigatorObserver _navigatorObserver = _SubNavigatorObserver(
          initialRoute,
          navigatorKey,
        );
        Routes.currentSubNavigatorInitialRoute = initialRoute;
    
        return WillPopScope(
          onWillPop: () async {
            if (_navigatorObserver.isInitialPage) {
              Routes.currentSubNavigatorInitialRoute = null;
              await SystemNavigator.pop();
              return true;
            }
    
            final bool canPop = navigatorKey.currentState.canPop();
    
            if (canPop) {
              navigatorKey.currentState.pop();
            }
    
            return !canPop;
          },
          child: Navigator(
            key: navigatorKey,
            observers: <NavigatorObserver>[_navigatorObserver],
            initialRoute: initialRoute,
            onGenerateRoute: Routes.onGenerateRoute,
          ),
        );
      }
    }
    
    //--------------------------------------------------------------------------------
    /// [NavigatorObserver] of [SubNavigator] widget.
    ///
    ///
    class _SubNavigatorObserver extends NavigatorObserver {
      _SubNavigatorObserver(this._initialRoute, this._navigatorKey);
    
      final String _initialRoute;
      final GlobalKey<NavigatorState> _navigatorKey;
      final List<String> _routeNameStack = <String>[];
    
      bool _isInitialPage = false;
    
      /// Flag if current route is the initial page.
      ///
      ///
      bool get isInitialPage => _isInitialPage;
    
      @override
      void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
        _routeNameStack.add(route.settings.name);
        _isInitialPage = _routeNameStack.last == _initialRoute;
      }
    
      @override
      void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
        _routeNameStack.remove(route.settings.name);
        _isInitialPage = _routeNameStack.last == _initialRoute;
      }
    
      @override
      void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {
        _routeNameStack.remove(route.settings.name);
        _isInitialPage = _routeNameStack.last == _initialRoute;
      }
    
      @override
      void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
        _routeNameStack.remove(oldRoute.settings.name);
        _routeNameStack.add(newRoute.settings.name);
        _isInitialPage = _routeNameStack.last == _initialRoute;
      }
    }
    
    import 'package:flutter/material.dart';
    import 'package:flutter2sample/routes.dart';
    
    class InitialPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Initial Page'),
          ),
          body: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                const Text('This is INITIAL page'),
                TextButton(
                  onPressed: () => Routes.pushNamed(Routes.PAGE_MAIN),
                  child: const Text('To Main page'),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    import 'package:flutter/material.dart';
    import 'package:flutter2sample/routes.dart';
    
    class MainPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Main Page'),
          ),
          body: SubNavigator(
            navigatorKey: Routes.mainNavigatorKey,
            initialRoute: Routes.PAGE_HOME,
          ),
        );
      }
    }
    

    home_page.dart

    import 'package:flutter/material.dart';
    import 'package:flutter2sample/routes.dart';
    
    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.yellow,
          body: SafeArea(
            child: Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const Text('This is HOME page'),
                  TextButton(
                    onPressed: () => Routes.pushNamed(
                      Routes.PAGE_PROFILE,
                      navigator: Routes.mainNavigator,
                    ),
                    child: const Text('To Profile page'),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    

    profile_page.dart

    import 'package:flutter/material.dart';
    import 'package:flutter2sample/routes.dart';
    
    class ProfilePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.grey,
          body: SafeArea(
            child: Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const Text('This is PROFILE page'),
                  TextButton(
                    onPressed: () => Routes.pop(navigator: Routes.mainNavigator),
                    child: const Text('Back to Home page'),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    
     类似资料:
    • 使用不包含导航器的上下文请求的导航器操作 完整代码在这里 完全错误代码 : ══╡ 用手势捕捉异常╞═══════════════════════════════════════════════════════════════════ I/颤振(3551):处理手势时抛出以下断言:I/颤振(3551):使用不包含导航器的上下文请求的导航器操作。I/flatter(3551):用于从导航器推送或弹出

    • 我是新手。我正在努力实现这个用户界面 我还没有找到任何使用完整的解决方案来创建颤振中的透明底部导航栏。 我试过用 但这似乎行不通。请帮忙。

    • 我试图在不同的环境中构建我的应用程序,所以我遵循了这个教程和这个视频,当我正常运行flutter works时,一切都运行得很好,但是当我尝试构建时,我得到了这个错误: 所以我忘了添加一些口味,然后在我的build.gradle(android/app/build.gradle)中添加了:

    • 然后我将Android应用程序添加到staging firebase项目中。我为staging firebase项目下载了google-services.json文件,并将其交换到Android/app目录中。目前,Android生产应用程序和staging应用程序之间的唯一区别是google-services.json文件。一旦我这样做了,firebase就不为分期应用程序工作了。活动用户没有反