|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// ignore_for_file: public_member_api_docs |
| 6 | + |
| 7 | +import 'dart:async'; |
| 8 | +import 'package:flutter/material.dart'; |
| 9 | +import 'package:url_launcher/url_launcher.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + runApp(MyApp()); |
| 13 | +} |
| 14 | + |
| 15 | +class MyApp extends StatelessWidget { |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return MaterialApp( |
| 19 | + title: 'URL Launcher', |
| 20 | + theme: ThemeData( |
| 21 | + primarySwatch: Colors.blue, |
| 22 | + ), |
| 23 | + home: MyHomePage(title: 'URL Launcher'), |
| 24 | + ); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class MyHomePage extends StatefulWidget { |
| 29 | + MyHomePage({Key key, this.title}) : super(key: key); |
| 30 | + final String title; |
| 31 | + |
| 32 | + @override |
| 33 | + _MyHomePageState createState() => _MyHomePageState(); |
| 34 | +} |
| 35 | + |
| 36 | +class _MyHomePageState extends State<MyHomePage> { |
| 37 | + Future<void> _launched; |
| 38 | + String _phone = ''; |
| 39 | + |
| 40 | + Future<void> _launchInBrowser(String url) async { |
| 41 | + if (await canLaunch(url)) { |
| 42 | + await launch( |
| 43 | + url, |
| 44 | + forceSafariVC: false, |
| 45 | + forceWebView: false, |
| 46 | + headers: <String, String>{'my_header_key': 'my_header_value'}, |
| 47 | + ); |
| 48 | + } else { |
| 49 | + throw 'Could not launch $url'; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Future<void> _launchInWebViewOrVC(String url) async { |
| 54 | + if (await canLaunch(url)) { |
| 55 | + await launch( |
| 56 | + url, |
| 57 | + forceSafariVC: true, |
| 58 | + forceWebView: true, |
| 59 | + headers: <String, String>{'my_header_key': 'my_header_value'}, |
| 60 | + ); |
| 61 | + } else { |
| 62 | + throw 'Could not launch $url'; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Future<void> _launchInWebViewWithJavaScript(String url) async { |
| 67 | + if (await canLaunch(url)) { |
| 68 | + await launch( |
| 69 | + url, |
| 70 | + forceSafariVC: true, |
| 71 | + forceWebView: true, |
| 72 | + enableJavaScript: true, |
| 73 | + ); |
| 74 | + } else { |
| 75 | + throw 'Could not launch $url'; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + Future<void> _launchInWebViewWithDomStorage(String url) async { |
| 80 | + if (await canLaunch(url)) { |
| 81 | + await launch( |
| 82 | + url, |
| 83 | + forceSafariVC: true, |
| 84 | + forceWebView: true, |
| 85 | + enableDomStorage: true, |
| 86 | + ); |
| 87 | + } else { |
| 88 | + throw 'Could not launch $url'; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Future<void> _launchUniversalLinkIos(String url) async { |
| 93 | + if (await canLaunch(url)) { |
| 94 | + final bool nativeAppLaunchSucceeded = await launch( |
| 95 | + url, |
| 96 | + forceSafariVC: false, |
| 97 | + universalLinksOnly: true, |
| 98 | + ); |
| 99 | + if (!nativeAppLaunchSucceeded) { |
| 100 | + await launch( |
| 101 | + url, |
| 102 | + forceSafariVC: true, |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + Widget _launchStatus(BuildContext context, AsyncSnapshot<void> snapshot) { |
| 109 | + if (snapshot.hasError) { |
| 110 | + return Text('Error: ${snapshot.error}'); |
| 111 | + } else { |
| 112 | + return const Text(''); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + Future<void> _makePhoneCall(String url) async { |
| 117 | + if (await canLaunch(url)) { |
| 118 | + await launch(url); |
| 119 | + } else { |
| 120 | + throw 'Could not launch $url'; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @override |
| 125 | + Widget build(BuildContext context) { |
| 126 | + const String toLaunch = 'https://www.cylog.org/headers/'; |
| 127 | + return Scaffold( |
| 128 | + appBar: AppBar( |
| 129 | + title: Text(widget.title), |
| 130 | + ), |
| 131 | + body: ListView( |
| 132 | + children: <Widget>[ |
| 133 | + Column( |
| 134 | + mainAxisAlignment: MainAxisAlignment.center, |
| 135 | + children: <Widget>[ |
| 136 | + Padding( |
| 137 | + padding: const EdgeInsets.all(16.0), |
| 138 | + child: TextField( |
| 139 | + onChanged: (String text) => _phone = text, |
| 140 | + decoration: const InputDecoration( |
| 141 | + hintText: 'Input the phone number to launch')), |
| 142 | + ), |
| 143 | + RaisedButton( |
| 144 | + onPressed: () => setState(() { |
| 145 | + _launched = _makePhoneCall('tel:$_phone'); |
| 146 | + }), |
| 147 | + child: const Text('Make phone call'), |
| 148 | + ), |
| 149 | + const Padding( |
| 150 | + padding: EdgeInsets.all(16.0), |
| 151 | + child: Text(toLaunch), |
| 152 | + ), |
| 153 | + RaisedButton( |
| 154 | + onPressed: () => setState(() { |
| 155 | + _launched = _launchInBrowser(toLaunch); |
| 156 | + }), |
| 157 | + child: const Text('Launch in browser'), |
| 158 | + ), |
| 159 | + const Padding(padding: EdgeInsets.all(16.0)), |
| 160 | + RaisedButton( |
| 161 | + onPressed: () => setState(() { |
| 162 | + _launched = _launchInWebViewOrVC(toLaunch); |
| 163 | + }), |
| 164 | + child: const Text('Launch in app'), |
| 165 | + ), |
| 166 | + RaisedButton( |
| 167 | + onPressed: () => setState(() { |
| 168 | + _launched = _launchInWebViewWithJavaScript(toLaunch); |
| 169 | + }), |
| 170 | + child: const Text('Launch in app(JavaScript ON)'), |
| 171 | + ), |
| 172 | + RaisedButton( |
| 173 | + onPressed: () => setState(() { |
| 174 | + _launched = _launchInWebViewWithDomStorage(toLaunch); |
| 175 | + }), |
| 176 | + child: const Text('Launch in app(DOM storage ON)'), |
| 177 | + ), |
| 178 | + const Padding(padding: EdgeInsets.all(16.0)), |
| 179 | + RaisedButton( |
| 180 | + onPressed: () => setState(() { |
| 181 | + _launched = _launchUniversalLinkIos(toLaunch); |
| 182 | + }), |
| 183 | + child: const Text( |
| 184 | + 'Launch a universal link in a native app, fallback to Safari.(Youtube)'), |
| 185 | + ), |
| 186 | + const Padding(padding: EdgeInsets.all(16.0)), |
| 187 | + FutureBuilder<void>(future: _launched, builder: _launchStatus), |
| 188 | + ], |
| 189 | + ), |
| 190 | + ], |
| 191 | + ), |
| 192 | + ); |
| 193 | + } |
| 194 | +} |
0 commit comments