Reputation: 38985
Now I am using this code to show text in flutter, this is my code:
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8.0),
child: Text(
item.subName,
softWrap: true,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
],
),
sometimes the text item.subName
is too long, how to make it auto expand or render in new line when the text overflow? I have tried to add this but still not work:
softWrap: true,
This is the error:
Upvotes: 4
Views: 5975
Reputation: 1
We can use the Expanded text to show the first time 2 line and when click on the text it shows all the text.
import 'package:flutter/cupertino.dart';
class ExpandableText extends StatefulWidget {
final String text;
final TextStyle textStyle;
const ExpandableText({Key? key,
required this.text,
required this.textStyle,
}) : super(key: key);
@override
ExpandableTextState createState() => ExpandableTextState();
}
class ExpandableTextState extends State<ExpandableText> {
bool isExpanded = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
setState(() {
isExpanded = !isExpanded;
});
},
child: Text(
widget.text,
style: widget.textStyle,
maxLines: isExpanded ? null : 2,
overflow: isExpanded ? null : TextOverflow.ellipsis,
),
);
}
}
Use this ExpandedText:
ExpandableText(
text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic",
textStyle: const TextStyle(color: Colors.black),
),
Upvotes: 0
Reputation: 137
Widget build(BuildContext context){
final size = MediaQuery.of(context).size;
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: size.width * 0.4, // Choose a number that works for you
child: Text(
item.subName,
softWrap: true,
overflow: TextOverflow.clip,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
],
),
Upvotes: 1
Reputation: 272
Row(
children: <Widget>[
Expanded(
child: new Text("your text"))
],
)
Upvotes: 3
Reputation: 3611
Try this
new Container(
child: Row(
children: <Widget>[
Flexible(
child: new Text("sample text"))
],
));
Upvotes: 14