diff --git a/lib/auto_size_text.dart b/lib/auto_size_text.dart index 81bb4c4..537a98b 100644 --- a/lib/auto_size_text.dart +++ b/lib/auto_size_text.dart @@ -5,6 +5,7 @@ library auto_size_text; import 'dart:async'; import 'package:flutter/widgets.dart'; +import 'package:flutter/material.dart' show SelectableText; part 'src/auto_size_text.dart'; part 'src/auto_size_group.dart'; diff --git a/lib/src/auto_size_text.dart b/lib/src/auto_size_text.dart index ec43838..44bf257 100644 --- a/lib/src/auto_size_text.dart +++ b/lib/src/auto_size_text.dart @@ -32,6 +32,7 @@ class AutoSizeText extends StatefulWidget { this.textScaleFactor, this.maxLines, this.semanticsLabel, + this.selectable, }) : textSpan = null, super(key: key); @@ -57,6 +58,7 @@ class AutoSizeText extends StatefulWidget { this.textScaleFactor, this.maxLines, this.semanticsLabel, + this.selectable, }) : data = null, super(key: key); @@ -215,6 +217,14 @@ class AutoSizeText extends StatefulWidget { /// ``` final String? semanticsLabel; + /// Whether the text should be selectable + /// + /// If present, this widget is generating text using SelectableText or SelectableText.rich instead of the default Text or Text.rich. + /// + /// If present, locale, softWrap, overflow and semanticsLabel are ignored as the are not supported by SelectableText. + + final bool? selectable; + @override _AutoSizeTextState createState() => _AutoSizeTextState(); } @@ -411,36 +421,62 @@ class _AutoSizeTextState extends State { } Widget _buildText(double fontSize, TextStyle style, int? maxLines) { - if (widget.data != null) { - return Text( - widget.data!, - key: widget.textKey, - style: style.copyWith(fontSize: fontSize), - strutStyle: widget.strutStyle, - textAlign: widget.textAlign, - textDirection: widget.textDirection, - locale: widget.locale, - softWrap: widget.softWrap, - overflow: widget.overflow, - textScaleFactor: 1, - maxLines: maxLines, - semanticsLabel: widget.semanticsLabel, - ); + if (widget.selectable == true) { + if (widget.data != null) { + return SelectableText( + widget.data!, + key: widget.textKey, + style: style.copyWith(fontSize: fontSize), + strutStyle: widget.strutStyle, + textAlign: widget.textAlign, + textDirection: widget.textDirection, + textScaleFactor: 1, + maxLines: maxLines, + ); + } else { + return SelectableText.rich( + widget.textSpan!, + key: widget.textKey, + style: style, + strutStyle: widget.strutStyle, + textAlign: widget.textAlign, + textDirection: widget.textDirection, + textScaleFactor: fontSize / style.fontSize!, + maxLines: maxLines, + ); + } } else { - return Text.rich( - widget.textSpan!, - key: widget.textKey, - style: style, - strutStyle: widget.strutStyle, - textAlign: widget.textAlign, - textDirection: widget.textDirection, - locale: widget.locale, - softWrap: widget.softWrap, - overflow: widget.overflow, - textScaleFactor: fontSize / style.fontSize!, - maxLines: maxLines, - semanticsLabel: widget.semanticsLabel, - ); + if (widget.data != null) { + return Text( + widget.data!, + key: widget.textKey, + style: style.copyWith(fontSize: fontSize), + strutStyle: widget.strutStyle, + textAlign: widget.textAlign, + textDirection: widget.textDirection, + locale: widget.locale, + softWrap: widget.softWrap, + overflow: widget.overflow, + textScaleFactor: 1, + maxLines: maxLines, + semanticsLabel: widget.semanticsLabel, + ); + } else { + return Text.rich( + widget.textSpan!, + key: widget.textKey, + style: style, + strutStyle: widget.strutStyle, + textAlign: widget.textAlign, + textDirection: widget.textDirection, + locale: widget.locale, + softWrap: widget.softWrap, + overflow: widget.overflow, + textScaleFactor: fontSize / style.fontSize!, + maxLines: maxLines, + semanticsLabel: widget.semanticsLabel, + ); + } } }