| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import 'package:flutter/gestures.dart';
- import 'package:flutter/material.dart';
- // ignore: must_be_immutable
- class LinkRichText extends StatefulWidget {
- double width;
- String text;
- LinkRichText({Key? key,required this.width,required this.text}) : super(key: key);
- @override
- State<LinkRichText> createState() => _LinkRichTextState();
- }
- class _LinkRichTextState extends State<LinkRichText> {
- bool mIsExpaned = false;
- int mSelectIndex = -1;
- bool isExpaned(String text, width) {
- TextPainter _textPainter = TextPainter(
- maxLines: 2,
- text: TextSpan(
- text: text, style: TextStyle(fontSize: 14.0, color: Colors.black)),
- textDirection: TextDirection.ltr)..layout(
- maxWidth: width-180,minWidth: 80
- );
- if (_textPainter.didExceedMaxLines){
- return true;
- }
- return false;
- }
- void _isShowText(){
- if (mIsExpaned){
- setState(() {
- mIsExpaned = false;
- });
- }
- else {
- setState(() {
- mIsExpaned = true;
- });
- }
- }
- // ignore: non_constant_identifier_names
- Widget _RichText(String _text,width){
- if (isExpaned(_text, width)){
- if (mIsExpaned){
- return Text.rich(
- TextSpan(
- text: _text,
- style: TextStyle(color: Colors.white,fontSize:14.0),
- children: [
- TextSpan(
- text: '收起',
- style: TextStyle(color: Colors.white),
- recognizer: TapGestureRecognizer()..onTap = ()async{
- _isShowText();
- }
- )
- ]
- )
- );
- }
- else {
- return Stack(
- children: [
- Text(_text,style: TextStyle(color: Colors.white,fontSize: 14),
- maxLines: 2,textAlign: TextAlign.left,overflow: TextOverflow.ellipsis,),
- Positioned(bottom: 0,right: 0,child: GestureDetector(
- onTap: (){
- _isShowText();
- },
- child: Container(
- width: 50,
- child: Text('展开'),
- ),
- ))
- ],
- );
- }
- }
- else {
- return Text(
- _text,
- maxLines: 2,
- textAlign: TextAlign.left,
- overflow: TextOverflow.ellipsis,
- );
- }
- }
- @override
- Widget build(BuildContext context) {
- return _RichText(widget.text, widget.width);
- }
- }
|