如何在Flutter中通过快速操作导航到其他屏幕?

时间:2019-11-10 14:10:02

标签: flutter dart

我正在编写一个简单的优惠券应用程序,但是我在快速行动包方面挣扎。

我想做的是允许用户使用快速操作导航到屏幕。我创建了一个快速操作,并尝试导航到它,但是它不起作用。

当我单击快速操作时,它会将我重定向到应用程序的主屏幕。这不是我想要的。

Quick action

import 'package:flutter/material.dart';
import './route_generator/route_generator.dart';
import 'package:quick_actions/quick_actions.dart';

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final QuickActions quickActions = QuickActions();
    quickActions.initialize((shortcutType) {
      if (shortcutType == 'cheeseburger') {
        Navigator.of(context).pushNamed('/generatedMailCouponScreen', arguments: 'assets/images/without_logo_coupon_cheeseburger.png');
      }
    });

    quickActions.setShortcutItems([
      ShortcutItem(
        type: 'cheeseburger', localizedTitle: 'Cheeseburger'
      )
    ]);

    return MaterialApp(
      initialRoute: '/',
      onGenerateRoute: RouteGenerator.generateRoute,
      theme: _theme()
    );
  }
}



ThemeData _theme() {
  const LargeTextSize = 26.0;
  const MediumTextSize = 20.0;
  const Body1TextSize = 16.0;
  const Body2TextSize = 12.0;

  const String FontNameDefault = 'Lato';

  return ThemeData(
    pageTransitionsTheme: PageTransitionsTheme(
      builders: {
        TargetPlatform.android: CupertinoPageTransitionsBuilder(),
        TargetPlatform.iOS: CupertinoPageTransitionsBuilder()
      }
    ),
    appBarTheme: AppBarTheme(
      color: Colors.blueGrey[700],
      textTheme: TextTheme(
        title: TextStyle(
          fontFamily: FontNameDefault,
          fontWeight: FontWeight.w300,
          fontSize: LargeTextSize
        )
      )
    ),
    tabBarTheme: TabBarTheme(
      labelStyle: TextStyle(
        fontFamily: FontNameDefault,
        fontWeight: FontWeight.w300,
        fontSize: Body1TextSize,
      ),
      unselectedLabelStyle: TextStyle(
        fontFamily: FontNameDefault,
        fontWeight: FontWeight.w300,
        fontSize: Body1TextSize
      )
    ),
    indicatorColor: Colors.blueGrey[100],
    scaffoldBackgroundColor: Colors.blueGrey[600],
    textTheme: TextTheme(
      title: TextStyle(
        fontFamily: FontNameDefault,
        fontWeight: FontWeight.w600,
        fontSize: LargeTextSize,
        color: Colors.white
      ),
      body1: TextStyle(
        fontFamily: FontNameDefault,
        fontWeight: FontWeight.w300,
        fontSize: Body1TextSize,
        color: Colors.white
      ),
      body2: TextStyle(
        fontFamily: FontNameDefault,
        fontWeight: FontWeight.w300,
        fontSize: Body2TextSize,
        color: Colors.white
      )
    )
  );
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用全局导航键。
首先像这样设置全局导航键:

wkhtmltopdf --encoding utf-8 --page-size A4 --margin-left 5mm --margin-right 5mm --margin-top 5mm --margin-bottom 5mm Hibernate_User_Guide.html Hibernate_User_Guide.pdf

并将navigatorKey添加到MaterialApp:

...
class App extends StatelessWidget {
  static final navigatorKey = GlobalKey<NavigatorState>();
  ...

设置完成后,您可以使用导航键在屏幕之间进行导航,如下所示:

...
return MaterialApp(
  initialRoute: '/',
  onGenerateRoute: RouteGenerator.generateRoute,
  navigatorKey: navigatorKey,
  theme: _theme()
);
...