|
| 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_platform_interface/url_launcher_platform_interface.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 | + |
| 39 | + Future<void> _launchInBrowser(String url) async { |
| 40 | + if (await UrlLauncherPlatform.instance.canLaunch(url)) { |
| 41 | + await UrlLauncherPlatform.instance.launch( |
| 42 | + url, |
| 43 | + useSafariVC: false, |
| 44 | + useWebView: false, |
| 45 | + enableJavaScript: false, |
| 46 | + enableDomStorage: false, |
| 47 | + universalLinksOnly: false, |
| 48 | + headers: <String, String>{}, |
| 49 | + ); |
| 50 | + } else { |
| 51 | + throw 'Could not launch $url'; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + Widget _launchStatus(BuildContext context, AsyncSnapshot<void> snapshot) { |
| 56 | + if (snapshot.hasError) { |
| 57 | + return Text('Error: ${snapshot.error}'); |
| 58 | + } else { |
| 59 | + return const Text(''); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + Widget build(BuildContext context) { |
| 65 | + const String toLaunch = 'https://www.cylog.org/headers/'; |
| 66 | + return Scaffold( |
| 67 | + appBar: AppBar( |
| 68 | + title: Text(widget.title), |
| 69 | + ), |
| 70 | + body: ListView( |
| 71 | + children: <Widget>[ |
| 72 | + Column( |
| 73 | + mainAxisAlignment: MainAxisAlignment.center, |
| 74 | + children: <Widget>[ |
| 75 | + const Padding( |
| 76 | + padding: EdgeInsets.all(16.0), |
| 77 | + child: Text(toLaunch), |
| 78 | + ), |
| 79 | + RaisedButton( |
| 80 | + onPressed: () => setState(() { |
| 81 | + _launched = _launchInBrowser(toLaunch); |
| 82 | + }), |
| 83 | + child: const Text('Launch in browser'), |
| 84 | + ), |
| 85 | + const Padding(padding: EdgeInsets.all(16.0)), |
| 86 | + FutureBuilder<void>(future: _launched, builder: _launchStatus), |
| 87 | + ], |
| 88 | + ), |
| 89 | + ], |
| 90 | + ), |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
0 commit comments