【Flutter】URLを開く方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// url_launcher のインストール必要 | |
// Link: https://pub.dartlang.org/packages/url_launcher | |
import 'package:flutter/material.dart'; | |
import 'package:url_launcher/url_launcher.dart'; // 追加 | |
// void main {...} | |
// class MyApp extends StatelessWidget {...} | |
class _MyHomePageState extends State<MyHomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
floatingActionButton: FloatingActionButton( | |
onPressed: _launchURL, // ボタンが押されたとき | |
child: Icon(Icons.launch), | |
), | |
); | |
} | |
} | |
_launchURL() async { | |
const url = 'https://flutter.io'; | |
if (await canLaunch(url)) { | |
await launch(url); | |
} else { | |
throw 'Could not launch $url'; | |
} | |
} |
コメント
コメントを投稿