2

Hi Team,

I am creating an app in which when you open the flutter app it's launching the URL of the website in the chrome app. But when I pressed back from the chrome app I also need to kill the flutter app also.

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(),
      ),
    );
  }
}

This is my current main.dart file code and I used the package URL launcher plugin for the launch URL.

url_launcher: ^6.1.4

enter image description here enter image description here

As you can see the app launches the google URL in chrome. But when the back button pressed the initial white screen of the flutter app it's still open.

Please help me how to kill the flutter app when we came back again to the flutter app.

MORE AKASH
  • 283
  • 1
  • 8

1 Answers1

2

Try this. for more details check the url_launcher package example url_launcher_package example and the 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';
        }
  }

If you have website with headers (https://www.google.com/abc/def/) you have to added headers to the path as bellow,

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';
       }
  }

if you have backend response for the website you can check this

Sasitha Dilshan
  • 111
  • 2
  • 15