大家好,
我正在创建一个应用程序,当您打开 flutter 应用程序时,它会在 chrome 应用程序中启动网站的 URL。但是当我从 chrome 应用程序中按回时,我还需要杀死 flutter 应用程序。
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher_string.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
websitelaunch();
super.initState();
}
bool x = false;
void websitelaunch() async {
const url = 'https://www.google.com';
if (await canLaunchUrlString(url.toString())) {
x = await launchUrlString(
url,
mode: LaunchMode.externalApplication,
);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Website launch from logo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Container(),
),
);
}
}
这是我当前的 main.dart 文件代码,我使用了包 URL 启动器插件作为启动 URL。
url_launcher: ^6.1.4
如您所见,应用程序在 Chrome 中启动了 google URL。但是,当后退按钮按下 Flutter 应用程序的初始白屏时,它仍然处于打开状态。
当我们再次返回 flutter 应用程序时,请帮助我如何杀死 flutter 应用程序。
最佳答案
试试这个。有关更多详细信息,请查看 url_launcher
包示例 url_launcher_package example和 Documentation
void websitelaunch() async {
final Uri url = Uri(
scheme: 'https', host: 'www.google.com', path:'');
if (!await launchUrl(url,
mode: LaunchMode.externalApplication)) {
throw 'Could not launch $url';
}
}
如果您的网站带有 header ( https://www.google.com/abc/def/ ),则必须将 header 添加到路径
,如下所示,
void websitelaunch() async {
final Uri url = Uri(
scheme: 'https', host: 'www.google.com', path: '/abc/def/');
if (!await launchUrl(url,
mode: LaunchMode.externalApplication)) {
throw 'Could not launch $url';
}
}
如果您有网站的后端响应,您可以检查 this
关于android - 单击 Flutter 应用程序图标时在外部 Chrome 应用程序中启动 URL,并按下“返回”按钮关闭 Flutter 和 Chrome 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72746652/