skip to Main Content

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.

this is the example capture

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


  1. 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.

    Login or Signup to reply.
  2. as i mention in the comment above by limit the number of the character.

    result :

    enter image description here

    expand

    enter image description here

    import 'package:flutter/material.dart';
    import 'package:flutter/gestures.dart';
    
    const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData.dark().copyWith(
            scaffoldBackgroundColor: darkBlue,
          ),
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            backgroundColor: Colors.white,
            body: Center(
              child: MyWidget(),
            ),
          ),
        );
      }
    }
    
    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return DescriptionText(
            text:
                "Very long text Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore");
      }
    }
    
    class DescriptionText extends StatefulWidget {
      const DescriptionText({super.key, required this.text});
      final String text;
    
      @override
      State<DescriptionText> createState() => _DescriptionTextState();
    }
    
    class _DescriptionTextState extends State<DescriptionText> {
      bool isExpanded = false;
      String text = "";
      @override
      void initState() {
        super.initState();
        text = widget.text;
      }
    
      final textStyle = const TextStyle(color: Colors.black);
      final linkStyle = const TextStyle(color: Colors.blue);
    
      @override
      Widget build(BuildContext context) {
        final numTxt = 86;
        return Padding(
          padding: const EdgeInsets.only(left: 8.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('Description', style: textStyle),
              const SizedBox(height: 5),
              RichText(
                text: TextSpan(
                  style: textStyle,
                  children: [
                    TextSpan(
                      text: isExpanded
                          ? text
                          : text.length > numTxt
                              ? '${text.substring(0, numTxt)}...'
                              : text,
                    ),
                    if (text.length > numTxt)
                      TextSpan(
                        text: isExpanded ? ' Read Less' : ' Read More',
                        style: linkStyle,
                        recognizer: TapGestureRecognizer()
                          ..onTap = () {
                            setState(() {
                              isExpanded = !isExpanded;
                            });
                          },
                      ),
                  ],
                ),
              ),
            ],
          ),
        );
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search