Newer
Older
flutterBaseApp / lib / utils / navigator_util.dart
StephanieGitHub on 9 Feb 2021 1 KB first commit
import 'package:base_app/ui/pages/user/user_login_page.dart';
import 'package:base_app/ui/widgets/common/web_scaffold.dart';
import 'package:base_app/utils/login_utils.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:url_launcher/url_launcher.dart';

class NavigatorUtil {
  // 打开页面
  static void pushPage(
    BuildContext context,
    Widget page, {
    String pageName,
    bool needLogin = false,
  }) async {
    if (context == null || page == null) return;
    bool isLogin = await LoginUtils.isLogin();
    if (needLogin && isLogin == false) {
      pushPage(context, UserLoginPage());
      return;
    }
    Navigator.push(
        context, new CupertinoPageRoute<void>(builder: (ctx) => page));
  }

  // 打开网页
  static void pushWeb(BuildContext context,
      {String title, String titleId, String url}) {
    if (context == null || ObjectUtil.isEmpty(url)) return;
    if (url.endsWith(".apk")) {
      launchInBrowser(url, title: title ?? titleId);
    } else {
      Navigator.push(
          context,
          new CupertinoPageRoute<void>(
              builder: (ctx) => new WebScaffold(
                    title: title,
                    titleId: titleId,
                    url: url,
                  )));
    }
  }

  // 打开浏览器
  static Future<Null> launchInBrowser(String url, {String title}) async {
    if (await canLaunch(url)) {
      await launch(url, forceSafariVC: false, forceWebView: false);
    } else {
      throw 'Could not launch $url';
    }
  }
}