### Install readmore package Source: https://pub.dev/packages/readmore Add the dependency to your pubspec.yaml file. ```yaml readmore: ^3.0.0 ``` -------------------------------- ### Implementing dispose in ReadMoreTextState Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreTextState/dispose.html Example of overriding the dispose method to clean up recognizers and state controllers before calling the superclass implementation. ```dart @override void dispose() { _recognizer.dispose(); _isCollapsed?.dispose(); super.dispose(); } ``` -------------------------------- ### Invalid StatefulWidget build method pattern Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreTextState/build.html An example of an invalid pattern where a build method on a StatefulWidget could lead to stale closure captures. ```dart // (this is not valid Flutter code) class MyButton extends StatefulWidgetX { MyButton({super.key, required this.color}); final Color color; @override Widget build(BuildContext context, State state) { return SpecialWidget( handler: () { print('color: $color'); }, ); } } ``` -------------------------------- ### Annotation Constructor Source: https://pub.dev/documentation/readmore/latest/readmore/Annotation-class.html Creates an instance of an Annotation to define a pattern and its corresponding visual representation. ```APIDOC ## Annotation({required RegExp regExp, required TextSpan spanBuilder({required String text, TextStyle? textStyle})}) ### Description Creates a new Annotation instance. This defines a pattern to match within text and a builder function to generate the corresponding TextSpan. ### Parameters - **regExp** (RegExp) - Required - The regular expression pattern to match within the text. - **spanBuilder** (Function) - Required - A callback function that takes the matched text and an optional TextStyle, returning a TextSpan to be rendered. ``` -------------------------------- ### Implementing initState in ReadMoreTextState Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreTextState/initState.html Initializes the state by calling the superclass method and setting up the tap recognizer. ```dart @override void initState() { super.initState(); _recognizer.onTap = _onTap; } ``` -------------------------------- ### Annotation(RegExp regExp, Function spanBuilder) Source: https://pub.dev/documentation/readmore/latest/readmore/Annotation/Annotation.html Creates an Annotation instance to define how specific text patterns should be rendered. ```APIDOC ## Annotation Constructor ### Description Creates a new Annotation instance. This is used to map a regular expression to a builder function that returns a TextSpan. ### Parameters - **regExp** (RegExp) - Required - The regular expression pattern to match in the text. - **spanBuilder** (Function) - Required - A callback function that receives the matched text and optional text style, returning a TextSpan. ``` -------------------------------- ### Implement basic ReadMoreText Source: https://pub.dev/packages/readmore Create a basic expandable text widget with defined trim modes and styles. ```dart 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.', trimMode: TrimMode.Line, trimLines: 2, colorClickableText: Colors.pink, trimCollapsedText: 'Show more', trimExpandedText: 'Show less', moreStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold), ); ``` -------------------------------- ### Import readmore package Source: https://pub.dev/packages/readmore Import the library into your Dart files to access the ReadMoreText widget. ```dart import 'package:readmore/readmore.dart'; ``` -------------------------------- ### ReadMoreTextState build implementation Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreTextState/build.html The implementation of the build method for ReadMoreTextState using a ValueListenableBuilder. ```dart @override Widget build(BuildContext context) { return ValueListenableBuilder( valueListenable: _effectiveIsCollapsed, builder: _builder, ); } ``` -------------------------------- ### Use Annotations for interactive text Source: https://pub.dev/packages/readmore Define custom styles and tap interactions for specific text patterns using the annotations property. ```dart ReadMoreText( 'This is a sample text with a #hashtag, a mention <@123>, and a URL: https://example.com.', trimMode: TrimMode.Line, trimLines: 2, colorClickableText: Colors.pink, annotations: [ Annotation( regExp: RegExp(r'#([a-zA-Z0-9_]+)'), spanBuilder: ({required String text, TextStyle? textStyle}) => TextSpan( text: text, style: textStyle?.copyWith(color: Colors.blue), ), ), Annotation( regExp: RegExp(r'<@(\d+)>'), spanBuilder: ({required String text, TextStyle? textStyle}) => TextSpan( text: 'User123', style: textStyle?.copyWith(color: Colors.green), recognizer: TapGestureRecognizer()..onTap = () { // Handle tap, e.g., navigate to a user profile }, ), ), // Additional annotations for URLs... ], moreStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold), ); ``` -------------------------------- ### Valid State build method pattern Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreTextState/build.html The recommended pattern where the build method resides on the State object, ensuring closures correctly reference the updated widget instance. ```dart class MyButton extends StatefulWidget { const MyButton({super.key, this.color = Colors.teal}); final Color color; // ... } class MyButtonState extends State { // ... @override Widget build(BuildContext context) { return SpecialWidget( handler: () { print('color: ${widget.color}'); }, ); } } ``` -------------------------------- ### Annotation Constructor Implementation Source: https://pub.dev/documentation/readmore/latest/readmore/Annotation/Annotation.html The constructor requires a RegExp for matching and a spanBuilder function to define how text spans are constructed. ```dart const Annotation({ required this.regExp, required this.spanBuilder, }); ``` -------------------------------- ### Define moreStyle property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/moreStyle.html Defines the TextStyle for the expanded text content. ```dart final TextStyle? moreStyle; ``` -------------------------------- ### ReadMoreText Constructor Implementation Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/ReadMoreText.html The constructor for the ReadMoreText widget, defining all available configuration parameters for text trimming and styling. ```dart const ReadMoreText( this.data, { super.key, this.isCollapsed, 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.textScaler, this.semanticsLabel, this.moreStyle, this.lessStyle, this.delimiter = '$_kEllipsis ', this.delimiterStyle, this.annotations, this.isExpandable = true, }); ``` -------------------------------- ### ReadMoreText.new Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/ReadMoreText.html Creates a new instance of the ReadMoreText widget with various configuration options for text trimming and styling. ```APIDOC ## ReadMoreText.new ### Description Creates a widget that displays text with 'read more' or 'show less' functionality based on length or line count. ### Parameters - **data** (String) - Required - The text content to display. - **key** (Key?) - Optional - Widget key. - **isCollapsed** (ValueNotifier?) - Optional - Controls the collapsed state. - **preDataText** (String?) - Optional - Text to display before the main data. - **postDataText** (String?) - Optional - Text to display after the main data. - **preDataTextStyle** (TextStyle?) - Optional - Style for preDataText. - **postDataTextStyle** (TextStyle?) - Optional - Style for postDataText. - **trimExpandedText** (String) - Optional - Text for the 'show less' button (default: 'show less'). - **trimCollapsedText** (String) - Optional - Text for the 'read more' button (default: 'read more'). - **colorClickableText** (Color?) - Optional - Color for the clickable text. - **trimLength** (int) - Optional - Character limit for trimming (default: 240). - **trimLines** (int) - Optional - Line limit for trimming (default: 2). - **trimMode** (TrimMode) - Optional - Mode for trimming (default: TrimMode.Length). - **style** (TextStyle?) - Optional - Style for the main text. - **textAlign** (TextAlign?) - Optional - Alignment of the text. - **textDirection** (TextDirection?) - Optional - Direction of the text. - **locale** (Locale?) - Optional - Locale for the text. - **textScaler** (TextScaler?) - Optional - Scaler for the text. - **semanticsLabel** (String?) - Optional - Semantics label for accessibility. - **moreStyle** (TextStyle?) - Optional - Style for the 'read more' text. - **lessStyle** (TextStyle?) - Optional - Style for the 'show less' text. - **delimiter** (String) - Optional - Delimiter between text and button. - **delimiterStyle** (TextStyle?) - Optional - Style for the delimiter. - **annotations** (List?) - Optional - List of annotations. - **isExpandable** (bool) - Optional - Whether the text is expandable (default: true). ``` -------------------------------- ### Declare RegExp property Source: https://pub.dev/documentation/readmore/latest/readmore/Annotation/regExp.html Defines a final RegExp property for use in the class. ```dart final RegExp regExp; ``` -------------------------------- ### void dispose() Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreTextState/dispose.html Called when this object is removed from the tree permanently. Subclasses should override this method to release any resources retained by this object. ```APIDOC ## void dispose() ### Description Called when this object is removed from the tree permanently. The framework calls this method when this State object will never build again. Subclasses should override this method to release any resources retained by this object. ### Signature `void dispose()` ### Usage Implementations of this method should end with a call to the inherited method, as in `super.dispose()`. ``` -------------------------------- ### Implement spanBuilder property Source: https://pub.dev/documentation/readmore/latest/readmore/Annotation/spanBuilder.html Defines the function signature for the spanBuilder property. ```dart final TextSpan Function({required String text, TextStyle? textStyle}) spanBuilder; ``` -------------------------------- ### Implementation of trimExpandedText Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/trimExpandedText.html Defines the final String property used for the expanded text state. ```dart final String trimExpandedText; ``` -------------------------------- ### Define postDataTextStyle property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/postDataTextStyle.html Declaration of the postDataTextStyle property within the class. ```dart final TextStyle? postDataTextStyle; ``` -------------------------------- ### ReadMoreTextState createState implementation Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/createState.html Specific implementation of createState for the ReadMoreText widget. ```dart @override ReadMoreTextState createState() => ReadMoreTextState(); ``` -------------------------------- ### Define lessStyle property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/lessStyle.html The property is defined as a nullable TextStyle. ```dart final TextStyle? lessStyle; ``` -------------------------------- ### Implementation of trimLines property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/trimLines.html Defines the trimLines integer property used for line-based text truncation. ```dart final int trimLines; ``` -------------------------------- ### Generic StatefulWidget createState implementation Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/createState.html Standard pattern for overriding createState in a Flutter StatefulWidget. ```dart @override State createState() => _SomeWidgetState(); ``` -------------------------------- ### Define the style property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/style.html The style property is defined as a nullable TextStyle object. ```dart final TextStyle? style; ``` -------------------------------- ### Define textScaler property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/textScaler.html Declaration of the nullable TextScaler property. ```dart final TextScaler? textScaler; ``` -------------------------------- ### semanticsLabel property implementation Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/semanticsLabel.html The property is defined as a final nullable String. ```dart final String? semanticsLabel; ``` -------------------------------- ### Define postDataText property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/postDataText.html Declaration of the postDataText property within the class. ```dart final String? postDataText; ``` -------------------------------- ### Define preDataTextStyle property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/preDataTextStyle.html Declaration of the preDataTextStyle property within the class. ```dart final TextStyle? preDataTextStyle; ``` -------------------------------- ### Define preDataText property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/preDataText.html Declaration of the preDataText property as a nullable String. ```dart final String? preDataText; ``` -------------------------------- ### Define textAlign property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/textAlign.html The property is defined as a nullable TextAlign type. ```dart final TextAlign? textAlign; ``` -------------------------------- ### Define colorClickableText property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/colorClickableText.html Declaration of the colorClickableText property using the Color type. ```dart final Color? colorClickableText; ``` -------------------------------- ### Define isExpandable property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/isExpandable.html Declaration of the final boolean property used to manage text expansion state. ```dart final bool isExpandable; ``` -------------------------------- ### Implementation of didUpdateWidget in ReadMoreTextState Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreTextState/didUpdateWidget.html This implementation ensures the state correctly handles changes to the isCollapsed property by synchronizing the ValueNotifier with the new widget configuration. ```dart @override void didUpdateWidget(ReadMoreText oldWidget) { super.didUpdateWidget(oldWidget); if (widget.isCollapsed == null && oldWidget.isCollapsed != null) { final oldValue = oldWidget.isCollapsed!.value; (_isCollapsed ??= ValueNotifier(oldValue)).value = oldValue; } } ``` -------------------------------- ### Define annotations property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/annotations.html The annotations property is defined as a nullable list of Annotation objects. ```dart final List? annotations; ``` -------------------------------- ### Define trimMode property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/trimMode.html Declaration of the trimMode property within the widget class. ```dart final TrimMode trimMode; ``` -------------------------------- ### Define the data property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/data.html The data property is defined as a final String within the component. ```dart final String data; ``` -------------------------------- ### Define delimiterStyle property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/delimiterStyle.html The delimiterStyle property is defined as a nullable TextStyle. ```dart final TextStyle? delimiterStyle; ``` -------------------------------- ### Define the locale property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/locale.html The locale property is defined as a nullable Locale type. ```dart final Locale? locale; ``` -------------------------------- ### Define trimLength property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/trimLength.html The trimLength property is declared as a final integer. ```dart final int trimLength; ``` -------------------------------- ### Define delimiter property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/delimiter.html The delimiter property is declared as a final String within the class implementation. ```dart final String delimiter; ``` -------------------------------- ### Define textDirection property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/textDirection.html The textDirection property is defined as a nullable TextDirection type. ```dart final TextDirection? textDirection; ``` -------------------------------- ### TrimMode Property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/trimMode.html The trimMode property determines how the text is truncated. It accepts a TrimMode enum value: TrimMode.Length for character-based truncation or TrimMode.Lines for line-based truncation. ```APIDOC ## Property: trimMode ### Description Determines the type of trim applied to the text. ### Type TrimMode (Enum) ### Options - **TrimMode.Length**: Truncates based on the number of characters. - **TrimMode.Lines**: Truncates based on the number of lines. ``` -------------------------------- ### Define trimCollapsedText property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/trimCollapsedText.html The property is defined as a final String within the class implementation. ```dart final String trimCollapsedText; ``` -------------------------------- ### semanticsLabel property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/semanticsLabel.html The semanticsLabel property is a String? type that defines the semantic label for the ReadMoreText widget, used for accessibility purposes. ```APIDOC ## semanticsLabel ### Description A property that provides a custom label for screen readers. If provided, this string will be announced by accessibility services instead of the widget's text content. ### Type String? ### Usage Assign a string value to this property to improve accessibility for the ReadMoreText component. ``` -------------------------------- ### Implementation of isCollapsed property Source: https://pub.dev/documentation/readmore/latest/readmore/ReadMoreText/isCollapsed.html Defines the isCollapsed property as a final ValueNotifier of type bool. ```dart final ValueNotifier? isCollapsed; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.