flutter开发实战-readmore长文本展开和收缩控件

flutter开发实战-readmore长文本展开和收缩控件

当长文本展开和收缩控件,我们需要使用readmore来处理长文本展开和收缩,方便阅读

一、引入readmore

在工程的pubspec.yaml中引入插件

复制代码
  readmore: ^2.1.0

ReadMoreText的属性如下

复制代码
const ReadMoreText(
    this.data, {
    Key? key,
    this.preDataText,
    this.postDataText,
    this.preDataTextStyle,
    this.postDataTextStyle,
    this.trimExpandedText = 'show less',
    this.trimCollapsedText = 'read more',
    this.colorClickableText,
    this.trimLength = 240,
    this.trimLines = 2,
    this.trimMode = TrimMode.Length,
    this.style,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.textScaleFactor,
    this.semanticsLabel,
    this.moreStyle,
    this.lessStyle,
    this.delimiter = _kEllipsis + ' ',
    this.delimiterStyle,
    this.callback,
  }) : super(key: key);

二、使用ReadMoreText

在长文本使用展开和收缩显示控件的

复制代码
ReadMoreText(
          'Flutter is Google's mobile UI open source framework to build high-quality native (super fast) interfaces for iOS and Android apps with the unified codebase.',
          trimLines: 2,
          colorClickableText: Colors.pink,
          trimMode: TrimMode.Line,
          trimCollapsedText: 'Show more',
          trimExpandedText: 'Show less',
          moreStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
        ),

完整代码如下

复制代码
import 'package:flutter/material.dart';
import 'package:readmore/readmore.dart';

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

  @override
  State<ReadMorePage> createState() => _ReadMorePageState();
}

class _ReadMorePageState extends State<ReadMorePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('HeroPage'),
      ),
      body: Center(
        child: ReadMoreText(
          'Flutter is Google's mobile UI open source framework to build high-quality native (super fast) interfaces for iOS and Android apps with the unified codebase.',
          trimLines: 2,
          colorClickableText: Colors.pink,
          trimMode: TrimMode.Line,
          trimCollapsedText: 'Show more',
          trimExpandedText: 'Show less',
          moreStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }
}

三、小结

flutter开发实战-readmore长文本展开和收缩控件

学习记录,每天不停进步。

相关推荐
吴Wu涛涛涛涛涛Tao4 分钟前
Flutter 弹窗解析:从系统 Dialog 到完全自定义
flutter·ios
拾光拾趣录25 分钟前
Flutter跨平台、性能优化与安全
前端·flutter
木叶丸1 天前
AI三大核心概念通俗指南:AIGC、Agent、MCP
人工智能·flutter·架构
火柴就是我1 天前
每日见闻之Container Decoration
android·flutter
古希腊被code拿捏的神2 天前
【Flutter】面试记录
flutter·面试·职场和发展
nc_kai2 天前
Flutter 之 table_calendar 控件
flutter
0wioiw02 天前
Flutter基础(前端教程⑨-图片)
前端·flutter
Engandend2 天前
Flutter与iOS混合开发交互
flutter·ios·程序员
浅忆无痕2 天前
Flutter抓包
前端·flutter
火柴就是我2 天前
每日见闻之尝试大白话说清Flutter的事件传递
flutter