如何在Flutter中启动链接?

xytpbqjk  于 2023-02-05  发布在  Flutter
关注(0)|答案(1)|浏览(173)

我尝试在ElevatedButton中启动URL链接,但如果按下按钮,它将不起作用,它不会执行任何操作,是否有其他操作可用于ElevatedButton,或者我是否应该编写其他标签

import 'package:url_launcher/url_launcher.dart';
import 'package:flutter/material.dart';

class MyAppThree extends StatefulWidget {
  const MyAppThree({super.key});

  @override
  State<StatefulWidget> createState() => _MyAppThreeState();
}
class _MyAppThreeState extends State<StatefulWidget> {
_launchURL() async {
    const url = 'https://www.youtube.com/';
    final uri = Uri.parse(url);
    if (await canLaunchUrl(uri)) {
      await launchUrl(uri);
    } else {
      throw 'Could not launch $url';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: const Text(''),
      ),
      body: Container(
            child: Column(
            children: <Widget>[
                   ElevatedButton(
                onPressed: _launchURL,
                child: Text(
                  'TAG Here',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 20,
                  ),
                ),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(
                      const Color.fromARGB(255, 55, 55, 55)),
                ),
              ),
rggaifut

rggaifut1#

从Android 11(API 30)开始,您必须在Android清单中添加一个元素作为根元素的子元素:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourapp">

<queries>
  <!-- If your app checks for https -->
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>

</queries>

/* rest of the manifest */

</manifest>

相关问题