Flutter文本展开收缩插件flutter_expandable_text的使用

发布于 1周前 作者 eggper 来自 Flutter

Flutter Expandable Text

A customisable Flutter package that allows the expanding and collapsing of long text.

Getting Started

Adding the Package

Add flutter_expandable_text to your pubspec.yaml file:

dependencies:
    flutter_expandable_text: ^1.0.0

Then import it in your Dart files:

import 'package:flutter_expandable_text/expandable_text.dart';

Usage Snippet

Trim based on number of characters in text

ExpandableText(
    _text,
    trimType: TrimType.characters,
    trim: 20, // trims if text exceeds 20 characters
);

Trim based on number of lines

ExpandableText(
    _text,
    trimType: TrimType.lines,
    trim: 2, // trims if text exceeds more than 2 lines
    onLinkPressed: (expanded) {
        // Callback function when a link is pressed
    },
);

Demo Image

Parameters

Name Description Is It Required Default Value
text Input text that is displayed Yes -
readMoreText Clickable text to display that expands text No read more
readLessText Clickable text to display that collapses text No read less
linkTextStyle TextStyle for both readMoreText and readLessText No TextStyle(color:Colors.blue)
style TextStyle for text No TextStyle(color:Colors.black)
trim Maximum amount of lines or characters allowable before the text is collapsed No 2
trimType Whether to trim text by lines or characters No TrimType.lines
onLinkPressed Callback function when a link is pressed. Returns a boolean No null

Contributions

Feel free to contribute to this project. If you find a bug or want a feature, but don’t know how to fix/implement it, please fill an issue. If you fixed a bug or implemented a new feature, please send a pull request.

Complete Example Demo

Here’s a complete example demonstrating various features of flutter_expandable_text.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_expandable_text/flutter_expandable_text.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const Home(),
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  final String _text =
      'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do '
      'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim '
      'ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut '
      'aliquip ex ea commodo consequat. Duis aute irure dolor';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Flutter Expandable Text')),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              'Trim after 20 characters',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 15,
              ),
            ),
            const SizedBox(height: 10),
            ExpandableText(
              _text,
              trimType: TrimType.characters,
              trim: 20,
            ),
            const SizedBox(height: 20),
            const Text(
              'Trim after 2 lines',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 15,
              ),
            ),
            const SizedBox(height: 10),
            ExpandableText(
              _text,
              trimType: TrimType.lines,
              trim: 2,
              onLinkPressed: (expanded) {
                if (kDebugMode) {
                  if (expanded) {
                    print('Was Expanded');
                  } else {
                    print('Was Collapsed');
                  }
                }
              },
            ),
            const SizedBox(height: 20),
            const Text(
              'Custom styling',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 15,
              ),
            ),
            const SizedBox(height: 10),
            ExpandableText(
              _text,
              trimType: TrimType.lines,
              trim: 2,
              readLessText: 'customised collapse',
              readMoreText: 'customised expand',
              linkTextStyle: const TextStyle(
                  color: Colors.green, fontWeight: FontWeight.bold),
              onLinkPressed: (expanded) {
                if (kDebugMode) {
                  if (expanded) {
                    print('Was Expanded');
                  } else {
                    print('Was Collapsed');
                  }
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}

This example showcases different ways to use the flutter_expandable_text plugin, including trimming by character count, line count, and customizing the appearance and behavior of the expandable text.


更多关于Flutter文本展开收缩插件flutter_expandable_text的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter文本展开收缩插件flutter_expandable_text的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何使用 flutter_expandable_text 插件的 Flutter 代码示例。这个插件允许你在应用中实现文本的展开和收缩功能。

首先,你需要在 pubspec.yaml 文件中添加依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_expandable_text: ^2.0.0  # 请确保使用最新版本

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

接下来,在你的 Dart 文件中,你可以使用 ExpandableText 小部件来实现文本的展开和收缩功能。以下是一个完整的示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Expandable Text Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Expandable Text Demo'),
        ),
        body: Center(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: ExpandableText(
              // 初始显示的文本
              initialText: '这是一段示例文本。点击展开以查看更多内容。',
              // 完整文本
              expandedText: '''
这是一段更长的文本内容。
你可以在这里放置更多的信息或描述。
用户可以通过点击展开按钮来查看这段文本的全部内容,
并通过点击收缩按钮来隐藏多余的内容。
              ''',
              // 展开按钮的文本
              expandText: '展开',
              // 收缩按钮的文本
              collapseText: '收缩',
              // 展开和收缩动画的持续时间
              animationDuration: Duration(milliseconds: 300),
              // 展开和收缩动画的曲线
              animationCurve: Curves.easeInOut,
              // 展开后的文本样式
              expandedTextStyle: TextStyle(fontSize: 16),
              // 收缩后的文本样式(即初始显示的文本样式)
              collapsedTextStyle: TextStyle(fontSize: 16, color: Colors.grey),
              // 展开和收缩按钮的样式
              expandCollapseIcon: Icon(
                Icons.expand_more,
                color: Colors.blue,
              ),
              // 点击展开或收缩按钮时的回调(可选)
              onExpanded: () {
                print('Text expanded');
              },
              onCollapsed: () {
                print('Text collapsed');
              },
            ),
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的 Flutter 应用,其中包含一个 ExpandableText 小部件。这个小部件显示了一段初始文本,并且当用户点击展开按钮时,会显示完整的文本内容。当用户点击收缩按钮时,文本内容会再次被截断为初始文本。

你可以根据需要调整 ExpandableText 的参数,例如文本样式、按钮文本、动画持续时间和曲线等。这个插件非常灵活,适用于各种文本展开和收缩的场景。

回到顶部