i am going to make a multi-line text overflow ellipsis, but i try some example, all those are not the expected result. Is anyone know how to do it?
For the example 1, it shows perfect when the text only have 1 line.
It the text is more than 1 line, the "more" is vertically align with the hello text, i want the "more" right after "…"
I try to use rich text, but it does work.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Example 1:',
),
Row(
children: [
Expanded(
child: Text(
'hello hello hello hello hello hello hello hello hello hello ',
overflow: TextOverflow.ellipsis,
style: TextStyle(
backgroundColor: Colors.amber,
),
),
),
Text("more"),
],
),
],
)),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Example 2:',
),
Row(
children: [
Expanded(
child: Text(
'hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello ',
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: TextStyle(
backgroundColor: Colors.amber,
),
),
),
Text("more"),
],
),
],
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Example 3:',
),
Row(
children: [
Expanded(
child: Material(
child: RichText(
maxLines: 2,
text: TextSpan(
text: '',
style: TextStyle(
color: Colors.black,
),
children: <TextSpan>[
TextSpan(
text:
'hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello ',
style: TextStyle(
overflow: TextOverflow.ellipsis,
),
),
TextSpan(
text: "more",
style: TextStyle(),
),
],
),
),
),
),
],
),
],
),
),
],
),
);
}
}
2
Answers
You can easily achieve this by using this lib extended_text, which can custom overflow, click here.
Just using RichText can’t achieve this at least now.
as i mention in the comment above by limit the number of the character.
result :
expand