Flutter文本展开收起插件enhanced_read_more的使用
Flutter文本展开收起插件enhanced_read_more的使用
enhanced_read_more
是一个用于在 Flutter 应用中实现文本展开与收起功能的插件。它允许用户通过点击按钮来展开或收起一段较长的文本。该插件是对 jonataslaw/readmore
的扩展版本,增加了对展开和收起状态的控制。
使用
添加依赖
首先,在项目的 pubspec.yaml
文件中添加 enhanced_read_more
依赖:
dependencies:
enhanced_read_more: ^0.0.2
然后运行 flutter pub get
来获取最新的依赖。
导入库
在需要使用的 Dart 文件中导入 enhanced_read_more
库:
import 'package:enhanced_read_more/enhanced_read_more.dart';
基本示例
以下是一个基本的示例代码,展示了如何使用 enhanced_read_more
插件:
import 'package:flutter/material.dart';
import 'package:enhanced_read_more/enhanced_read_more.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: const Color(0xFF02BB9F),
primaryColorDark: const Color(0xFF167F67),
),
title: 'Read More Text',
home: DemoApp(),
);
}
}
class DemoApp extends StatelessWidget {
final collapsedNotifier = ValueNotifier<bool>(false);
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Read More Text',
style: TextStyle(color: Colors.white),
)),
body: DefaultTextStyle.merge(
style: const TextStyle(
fontSize: 16.0,
// fontFamily: 'monospace',
),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
key: const Key('showMore'),
padding: const EdgeInsets.all(16.0),
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.',
collapsed: true,
trimLines: 2,
preDataText: "AMANDA",
preDataTextStyle: TextStyle(fontWeight: FontWeight.w500),
style: TextStyle(color: Colors.black),
colorClickableText: Colors.pink,
trimMode: TrimMode.Line,
trimCollapsedText: '...Show more',
trimExpandedText: ' show less',
),
),
Divider(
color: const Color(0xFF167F67),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: ReadMoreText(
'Flutter(https://flutter.dev/) has its own UI components, along with an engine to render them on both the Android and iOS platforms. Most of those UI components, right out of the box, conform to the guidelines of Material Design.',
collapsed: true,
trimLines: 3,
style: TextStyle(color: Colors.black),
colorClickableText: Colors.pink,
trimMode: TrimMode.Line,
trimCollapsedText: '...Expand',
trimExpandedText: ' Collapse ',
onLinkPressed: (url) {
print(url);
},
),
),
Divider(
color: const Color(0xFF167F67),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: ReadMoreText(
'The Flutter framework builds its layout via the composition of widgets, everything that you construct programmatically is a widget and these are compiled together to create the user interface. ',
collapsed: true,
trimLines: 2,
style: TextStyle(color: Colors.black),
colorClickableText: Colors.pink,
trimMode: TrimMode.Line,
trimCollapsedText: '...Read more',
trimExpandedText: ' Less',
),
),
Divider(
color: const Color(0xFF167F67),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextButton(
onPressed: () {
collapsedNotifier.value = !collapsedNotifier.value;
},
child: Text('Click me to toggle expand or collapse'),
),
const SizedBox(height: 15),
ValueListenableBuilder(
valueListenable: collapsedNotifier,
builder: (context, bool value, _) {
return ReadMoreText(
'This text will collapse or expand if you tap or click the button above Laboris cillum id aliqua ullamco fugiat nulla consequat. Occaecat eu excepteur amet id aliqua ea esse excepteur. Eiusmod aliqua anim amet nisi nulla ipsum ut ullamco velit nisi sunt. Laborum eu in excepteur elit excepteur eu fugiat reprehenderit deserunt enim duis anim veniam. Reprehenderit deserunt laborum veniam eu amet minim esse dolore aliquip enim commodo. Non enim qui consequat sint dolor do exercitation consequat est. Deserunt cillum magna mollit veniam mollit id nulla culpa quis nisi cillum mollit sint dolore. Id fugiat adipisicing ullamco consequat elit reprehenderit dolore.',
collapsed: value,
trimLines: 2,
style: TextStyle(color: Colors.black),
colorClickableText: Colors.pink,
trimMode: TrimMode.Line,
trimCollapsedText: '...Read more',
trimExpandedText: ' Less',
);
},
),
],
),
),
],
),
),
),
);
}
}
更多关于Flutter文本展开收起插件enhanced_read_more的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter文本展开收起插件enhanced_read_more的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个使用 enhanced_read_more
Flutter 插件来实现文本展开和收起功能的代码案例。这个插件允许你在文本过长时显示一个“展开”按钮,用户点击后可以查看全部内容,再次点击“收起”按钮可以隐藏多余的内容。
首先,确保你的 pubspec.yaml
文件中包含了 enhanced_read_more
依赖:
dependencies:
flutter:
sdk: flutter
enhanced_read_more: ^2.0.0 # 请检查最新版本号
然后,运行 flutter pub get
来获取依赖。
接下来,在你的 Dart 文件中使用 EnhancedReadMoreText
组件。以下是一个完整的示例:
import 'package:flutter/material.dart';
import 'package:enhanced_read_more/enhanced_read_more.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Enhanced Read More Example'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: EnhancedReadMoreText(
text: '这是一个很长的文本示例,用于展示enhanced_read_more插件的使用。当你有很多文本需要显示,但不想一开始就全部展示给用户时,这个插件非常有用。它允许你展示文本的摘要,并提供一个“展开”按钮来查看全部内容。',
maxLines: 3, // 初始显示的最大行数
collapseText: '展开', // 展开按钮的文本
expandText: '收起', // 收起按钮的文本
onTap: () {
// 这里可以添加点击按钮后的处理逻辑,比如记录用户行为等
print('按钮被点击');
},
),
),
),
),
);
}
}
在这个示例中:
text
属性包含了要显示的文本内容。maxLines
属性设置了初始显示的最大行数。collapseText
属性设置了“展开”按钮的文本。expandText
属性设置了“收起”按钮的文本。onTap
是一个可选的回调函数,当按钮被点击时会调用这个函数。你可以在这里添加一些额外的逻辑,比如记录用户行为。
运行这个示例,你会看到一个包含部分文本和一个“展开”按钮的界面。点击“展开”按钮后,全部文本会显示出来,并且按钮文本会变为“收起”。再次点击按钮,文本会恢复到初始显示的状态。
希望这个代码案例能帮助你更好地理解和使用 enhanced_read_more
插件。