Skip to content

Instantly share code, notes, and snippets.

@Jay-flow
Created May 13, 2022 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jay-flow/97df386bfdb9ce5d3d1858ec2c8c0555 to your computer and use it in GitHub Desktop.
Save Jay-flow/97df386bfdb9ce5d3d1858ec2c8c0555 to your computer and use it in GitHub Desktop.
To add 'more' text functionality for the text widget in Flutter
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