skip to Main Content

enter image description here

As you can see, there is am empty space on the Text right side, how can i remove it.

Use CustomPainter can solve this problem but I need to calculate text width first.

Is there another way to remove this empty space?

example code:

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(
      home: Scaffold(
      body: Center(
        child: Row(
          children: [
            Flexible(
              child: Container(
                color: Colors.green,
                 child: Text(
                  "TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT",
                  style: TextStyle(fontSize: 20)
                )
              )
            )
          ],
        )
      )
      )
    );
  }
}

dartPad: https://dartpad.dev

2

Answers


  1. Just add textAlign:TextAlign.center

    Text("TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT",
                       textAlign:TextAlign.center,
                      style: TextStyle(fontSize: 20)
                    )
    
    Login or Signup to reply.
  2. You can’t remove this space because text is automatically adjust according to mobile screen.

    You cant align text center using text align property.

    updated code

    Scaffold(
          body: Center(
              child: Row(
                children: [
                  Flexible(
                      child: Container(
                          color: Colors.green,
                          child: const Text(
                              "TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT",
                              style: TextStyle(fontSize: 20),
                            textAlign:TextAlign.center,
                          )
                      )
                  )
                ],
              )
          )
      )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search