How to Redirect to a Other App from your Flutter App.
Published January 01, 2022You started creating your app, but then you also want your apps to link to other apps. You want to Redirect to another app when you press a button. How do you do that??
I'll be teaching you how in today's tutorial
Let's get Started
Step 1: Create Flutter project
Step 2: we need to add this package to our pubspec.yaml file external_app_launcher
|
Step 3: Then we create a stateful widget. Why are you using a stateful widget you may ask. We need to initialize the launcher
Import the package and create a widget like the one below. import 'package:flutter/material.dart'; import 'package:external_app_launcher/external_app_launcher.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() { // TODO: implement initState super.initState(); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Open External Apps'), ), ), ); } } |
Now we need to add our body properties. We will create an elevated button which will be centered
body: Center( child: ElevatedButton( child: const Text('Open External Apps'), ),
|
Now as you can see our button is missing an on pressed function. We will creating that now. The function will be asynchronous as we know that the function won't fire off immediately. Then we add the package name of the app we want to open in our function.
How do I get my package name?
Well, it's simple. You just open Google play on your computer or use desktop mode on your phone browser(open in incognito if it redirects to Playstore). Below is a sample link
https://play.google.com/store/apps/details?id=com.android.chrome |
Everything after the id is the package name e.g. com.android.chrome.
And below is the on pressed function
body: Center( child: ElevatedButton( onPressed: () async { await LaunchApp.openApp( androidPackageName: 'com.android.chrome', ); }, child: const Text('Open External Apps'), ),
|
What if you don't have the app you want to redirect to installed on your phone. You can add a code to redirect you to Playstore
body: Center( child: ElevatedButton( onPressed: () async { await LaunchApp.openApp( androidPackageName: 'com.android.chrome', openStore: true, ); }, child: const Text('Open External Apps'), )
|
And this is how you open other apps from your Flutter app
Article Contributed By :
|
|
|
|
2407 Views |