Created
May 13, 2022 07:21
-
-
Save Jay-flow/97df386bfdb9ce5d3d1858ec2c8c0555 to your computer and use it in GitHub Desktop.
To add 'more' text functionality for the text widget in Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:artcalendar/widgets/empty.dart'; | |
import 'package:flutter/material.dart'; | |
import 'badge.dart'; | |
class OverflowText extends StatefulWidget { | |
const OverflowText({ | |
Key? key, | |
required this.text, | |
required this.maxLength, | |
}) : super(key: key); | |
final String text; | |
final int maxLength; | |
@override | |
State<OverflowText> createState() => _OverflowTextState(); | |
} | |
class _OverflowTextState extends State<OverflowText> { | |
bool _isTextOverflow = false; | |
bool _isMore = false; | |
@override | |
void initState() { | |
super.initState(); | |
_isTextOverflow = widget.text.length > widget.maxLength; | |
_isMore = !_isTextOverflow; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Text( | |
widget.text, | |
maxLines: _isTextOverflow && !_isMore ? 10 : null, | |
overflow: _isTextOverflow && !_isMore | |
? TextOverflow.ellipsis | |
: TextOverflow.visible, | |
), | |
_isTextOverflow && !_isMore | |
? Container( | |
margin: const EdgeInsets.only(top: 20), | |
width: 80, | |
child: InkWell( | |
onTap: () { | |
setState(() { | |
_isMore = true; | |
}); | |
}, | |
child: Badge( | |
height: 30, | |
text: 'MORE', | |
backgroundColor: Colors.white, | |
textStyle: const TextStyle( | |
color: Colors.black, | |
fontSize: 12, | |
), | |
border: Border.all( | |
color: Colors.black, | |
), | |
), | |
), | |
) | |
: const Container(), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment