Flutter文本滑动浏览插件swipe_through_text的使用

Flutter文本滑动浏览插件SwipeThroughText的使用

预览

限制

目前该插件仅支持单行文本。溢出的文本将被裁剪并在末尾添加省略号 ...。这是一个暂时的限制,欢迎贡献以增加对多行文本的支持。

开始使用

安装

在你的 pubspec.yaml 文件中添加以下依赖:

dependencies:
  swipe_through_text: ^0.0.4 

然后在终端运行 flutter pub get

使用

首先,导入该包到你的Dart代码中:

import 'package:swipe_through_text/swipe_through_text.dart';

使用 SwipeThroughText 小部件时,可以传递必需的参数(如 texttextStyle)和可选参数(如 strikethroughColorstrikethroughLineHeightswipeThresholdonSwipeCompleteonSwipeCanceldashArray)。

SwipeThroughText(
  text: 'Swipe me!',
  textStyle: TextStyle(fontSize: 24),
  strikethroughColor: Colors.red,
  strikethroughLineHeight: 4,
  swipeThreshold: 0.8,
  onSwipeComplete: (fraction) {
    // 当滑动完成时执行的操作
  },
  onSwipeCancel: (fraction) {
    // 当滑动取消时执行的操作
  },
)

你还可以通过 dashArray 参数自定义删除线。dashArray 接受一个双精度值列表,用于确定删除线中虚线和间隙的长度。例如,[10, 5] 将创建一个虚线删除线,其中虚线长度为10像素,间隙长度为5像素。

示例

以下是一个完整的示例,展示了如何使用 SwipeThroughText 小部件:

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'SwipeThroughText Demo',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('SwipeThroughText Demo'),
        ),
        body: Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.all(24),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SwipeThroughText(
                text: 'Swipe Through me! limited to 1 line',
                textStyle: const TextStyle(fontSize: 24),
                strikethroughColor: Colors.red,
                strikethroughLineHeight: 4,
                swipeThreshold: 0.5,
                onSwipeComplete: (fraction) {
                  debugPrint('Swipe completed at $fraction');
                },
                onSwipeCancel: (fraction) {
                  debugPrint('Swipe cancelled at $fraction');
                },
                dashArray: const [], // 对于虚线使用:const [10, 5],
              ),
              const SizedBox(
                height: 10,
              ),
              SwipeThroughText(
                text: 'Swipe Through me! limited to 1 line',
                textStyle: const TextStyle(fontSize: 24),
                strikethroughColor: Colors.green,
                strikethroughLineHeight: 4,
                swipeThreshold: 0.5,
                onSwipeComplete: (fraction) {
                  debugPrint('Swipe completed at $fraction');
                },
                onSwipeCancel: (fraction) {
                  debugPrint('Swipe cancelled at $fraction');
                },
                dashArray: const [10, 5],
              ),
              const SizedBox(
                height: 10,
              ),
              SwipeThroughText(
                text: 'Swipe Through me! limited to 1 line',
                textStyle: const TextStyle(fontSize: 24),
                strikethroughColor: Colors.purple,
                strikethroughLineHeight: 4,
                swipeThreshold: 0.5,
                onSwipeComplete: (fraction) {
                  debugPrint('Swipe completed at $fraction');
                },
                onSwipeCancel: (fraction) {
                  debugPrint('Swipe cancelled at $fraction');
                },
                dashArray: const [], // 对于虚线使用:const [10, 5],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter文本滑动浏览插件swipe_through_text的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter文本滑动浏览插件swipe_through_text的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何使用 swipe_through_text Flutter 插件的示例代码。swipe_through_text 是一个允许用户通过水平滑动来浏览文本的插件。

首先,确保在你的 pubspec.yaml 文件中添加了对 swipe_through_text 的依赖:

dependencies:
  flutter:
    sdk: flutter
  swipe_through_text: ^最新版本号  # 请替换为当前最新版本号

然后运行 flutter pub get 来获取依赖。

接下来是一个完整的 Flutter 应用示例,展示了如何使用 swipe_through_text 插件:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Swipe Through Text Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Swipe Through Text Demo'),
        ),
        body: Center(
          child: SwipeThroughText(
            // 文本列表,每个元素代表一个可滑动的文本段落
            textList: [
              '这是第一个文本段落。',
              '这是第二个文本段落,内容更长一些。',
              '这是第三个文本段落,继续滑动查看更多。',
            ],
            // 文本样式
            textStyle: TextStyle(
              fontSize: 24,
              color: Colors.black,
            ),
            // 滑动动画时长
            animationDuration: Duration(milliseconds: 500),
            // 文本对齐方式
            textAlign: TextAlign.center,
            // 当滑动到最后一个文本时是否循环回第一个
            loop: true,
            // 滑动监听回调
            onTextChange: (index) {
              print('当前文本索引:$index');
            },
          ),
        ),
      ),
    );
  }
}

代码解释:

  1. 依赖导入:首先导入 flutterswipe_through_text 包。
  2. 主应用MyApp 是主应用类,它返回一个 MaterialApp,包含一个应用栏和一个居中的 SwipeThroughText 小部件。
  3. SwipeThroughText
    • textList:一个字符串列表,每个字符串代表一个可滑动的文本段落。
    • textStyle:用于设置文本的样式,例如字体大小和颜色。
    • animationDuration:滑动动画的时长。
    • textAlign:文本的对齐方式。
    • loop:当滑动到最后一个文本时是否循环回第一个文本。
    • onTextChange:当文本变化时的回调,返回当前文本的索引。

将上述代码复制到你的 Flutter 项目中并运行,你应该能看到一个支持文本滑动的界面。

希望这个示例对你有帮助!如果有其他问题,欢迎继续提问。

回到顶部