How do I open an external url in flutter web in new tab or in same tab

后端 未结 5 873
梦如初夏
梦如初夏 2021-02-18 23:16

I have a simple web app I have created with flutter web. I would like to know how I can open new an external url either in a new tab or in the sa

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-18 23:45

    You can use the url_launcher plugin

    Then in your code

    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    
    void main() {
      runApp(Scaffold(
        body: Center(
          child: RaisedButton(
            onPressed: _launchURL,
            child: Text('Show Flutter homepage'),
          ),
        ),
      ));
    }
    
    _launchURL() async {
      const url = 'https://flutter.io';
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    }
    

    Example taken from the package site

提交回复
热议问题