TextOverflow.ellipsis feature doesn't work in a Text widget when it is enclosed within a Row widget

16 views Asked by At

In my scenario, I aim to display overflowing text with an ellipsis using TextOverflow.ellipsis within a Text widget, in which the Row as parent widget. However, the ellipsis functionality is effective only when I wrap the Text widget within an additional SizedBox or Container. I desire the overflowing text to be displayed with an ellipsis directly within the Row without the need for additional wrapping in a container.

Added the code for the reference,

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      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(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 200,
              height: 60,
              color: Colors.yellow,
              child: Row(children: const [
                SizedBox(
                  width: 100,
                  child: Text(
                    'It is a lengthy text you have to reduce the overflow',
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                SizedBox(
                    width: 100,
                    child: Text(
                      'Yes, will reduce the length by overflow property',
                      overflow: TextOverflow.ellipsis,
                    )),
              ]),
            )
          ],
        ),
      ),
    );
  }
}

In the provided code, I enclosed the two texts with a SizedBox within a parent Row widget, resulting in the overflowed text displaying with an ellipsis. However, I aim to achieve the same behavior without the necessity of being wrapped by a SizedBox or Container.

0

There are 0 answers