2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Flutter can open a URL in the browser after clicking on the text, for example "www.baidu.com". This can be done by using url_launcher
package to do this. First, you need topubspec.yaml
Add to the fileurl_launcher
rely.
url_launcher: ^6.3.0
Here is the sample code
- import 'package:flutter/material.dart';
- import 'package:url_launcher/url_launcher.dart';
-
- final Uri _url = Uri.parse('https://www.baidu.com');
-
- void main() => runApp(
- const MaterialApp(
- home: Material(
- child: Center(
- child: ElevatedButton(
- onPressed: _launchUrl,
- child: Text('点击打开百度'),
- ),
- ),
- ),
- ),
- );
-
- Future<void> _launchUrl() async {
- if (!await launchUrl(_url)) {
- throw Exception('Could not launch $_url');
- }
- }