Flutter链接处理插件linkable的使用

Flutter链接处理插件linkable的使用

linkable pub package

linkable 是一个Flutter插件,允许您将文本中的URL、电子邮件地址和电话号码转换为可点击的链接。默认情况下,TextRichText小部件会将其中的URL渲染为不可点击的普通文本,而Linkable小部件则是一个基于RichText的封装,支持所有RichText小部件的属性,并能够将文本中的链接解析为可点击的形式。

支持的链接类型

注意:您不需要指定URL模式(如mailto, tel等),该小部件会自动解析。

安装

要在项目中使用linkable,请按照以下步骤操作:

添加依赖项

在项目的pubspec.yaml文件中添加linkableurl_launcher依赖项:

dependencies:
  linkable: ^3.0.2
  url_launcher: ^6.1.14

然后运行flutter pub get以安装这些包。

Android配置

从API 30(Android 11)开始,您的应用程序需要列出它与之交互的所有应用程序。因此,请确保在AndroidManifest.xml中包含以下内容:

<manifest>
    <!-- Nest within the manifest element, not the application element -->
    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
        <intent>
            <action android:name="android.intent.action.DIAL" />
            <data android:scheme="tel" />
        </intent>
        <intent>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="*/*" />
        </intent>
    </queries>
</manifest>

iOS配置

对于iOS,在Info.plist文件中添加以下条目:

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>tel</string>
</array>

使用方法

基本用法

以下是如何使用Linkable小部件的基本示例:

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

void main() => runApp(new LinkableExample());

class LinkableExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Linkable example',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Linkable Example'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Linkable(
                text:
                    "Hi!\nI'm Anup.\n\nYou can email me at 1anuppanwar@gmail.com.\nOr just whatsapp me @ +91-8968894728.\n\nFor more info visit: \ngithub.com/anupkumarpanwar \nor\nhttps://www.linkedin.com/in/anupkumarpanwar/",
                style: TextStyle(fontSize: 18.0), // 设置整体文本样式
                textColor: Colors.black, // 设置非链接文本的颜色
                linkColor: Colors.blue, // 设置链接颜色
                textAlign: TextAlign.center, // 设置文本对齐方式
              ),
            ],
          ),
        ),
      ),
    );
  }
}

属性说明

属性名 描述
text 要显示的文本。
textColor 非链接文本的颜色,默认为黑色。
linkColor 链接的颜色,默认为蓝色。
style 应用于整个文本的TextStyle
textAlign 文本对齐方式,默认为TextAlign.start
textDirection 确定水平布局子元素的顺序。
maxLines 显示的最大行数。
textScaleFactor 每个逻辑像素对应的字体像素数。

以上就是linkable插件的使用指南,希望对您有所帮助!如果您有任何问题或需要进一步的帮助,请随时提问。


更多关于Flutter链接处理插件linkable的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter链接处理插件linkable的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中,linkable 插件通常用于处理应用内的链接(deep linking)。虽然 Flutter 官方并没有直接名为 linkable 的插件,但处理链接的常用方法是使用 url_launcherflutter_deeplinking 等插件,或者通过配置 Android 和 iOS 的 intent-filtersURL schemes 来实现。

以下是一个使用 url_launcher 插件来处理链接的示例,以及如何配置 Android 和 iOS 以支持 deep linking。

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 url_launcher 依赖:

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.0.3  # 请确保使用最新版本

然后运行 flutter pub get

2. 使用 url_launcher 打开链接

在你的 Dart 代码中,你可以使用 url_launcher 来打开链接:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('URL Launcher Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: _launchURL,
            child: Text('Open Website'),
          ),
        ),
      ),
    );
  }

  _launchURL() async {
    const url = 'https://www.example.com';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }
}

3. 配置 Android 以支持 Deep Linking

android/app/src/main/AndroidManifest.xml 文件中添加 intent-filter

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" android:host="www.example.com" />
        <data android:scheme="http" android:host="www.example.com" />
    </intent-filter>
</activity>

4. 配置 iOS 以支持 Deep Linking

ios/Runner/Info.plist 文件中添加 URL schemes 和 associated domains:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>com.example.app</string>
        </array>
    </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>https</string>
    <string>http</string>
</array>

并且,如果你希望应用能够响应来自特定域的链接,还需要添加 associated domains

<key>entitlements</key>
<dict>
    <key>applinks:www.example.com</key>
    <array>
        <dict>
            <key>apps</key>
            <array>
                <dict>
                    <key>app-id</key>
                    <string>TEAMID.com.example.app</string>
                    <key>paths</key>
                    <array>
                        <string>/*</string>
                    </array>
                </dict>
            </array>
        </dict>
    </array>
</dict>

注意:associated domains 的配置需要你的应用具有 Apple Developer Program 成员资格,并且需要在 Apple Developer Console 中配置应用前缀。

5. 处理 Deep Linking 路由

在 Flutter 中处理 deep linking 通常需要在 MaterialAppCupertinoApp 中配置 onGenerateRouteonUnknownRoute,以根据传入的 URL 路径进行路由。这通常涉及到解析传入的 URL 并根据路径导航到相应的页面。

由于这涉及到更复杂的路由逻辑,具体实现会根据应用的需求而有所不同,因此在这里不提供完整的代码示例。但你可以参考 Flutter 的路由文档和 url_launcher 插件的文档来进一步实现。

希望这些信息对你有所帮助!如果你有更具体的需求或问题,请随时提问。

回到顶部