1

Use this very simple code to reproduce:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: Center(
          child: Container(
            color: Colors.orange[200],
            child: const Text(
              '探索文本工王田甲乙丙丁一二三四口',
              style: TextStyle(
                fontSize: 24,
                height: 1,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

This is the screenshot when running the app:

enter image description here

As we can see, the text has two strange things:

  1. It has non-neglectable bottom paddings.
  2. It has negative top padding.

This is a problem, because with such, we cannot vertically make a text center-aligned. for example, a Center will make the text "box" centered, but due to uneven padding, it will indeed be put higher than expected.

By the way, I have tested it on a iPhone and an android, so it seems not to be a problem of one specific font, but rather something intrinsic.

ch271828n
  • 15,854
  • 5
  • 53
  • 88

2 Answers2

0

try to reference to strutStyle to adjust your padding, FYI

const Text(
  '探索文本工王田甲乙丙丁一二三四口',
  style: TextStyle(
    fontSize: 10,
  ),
  strutStyle: StrutStyle(
    fontSize: 14,
    height: 1.7,
    leading: 1.7,
  ),
),
Jim
  • 6,928
  • 1
  • 7
  • 18
0

try the code below

            Text(
              '测试',
              style: TextStyle(fontSize: 40),
              textHeightBehavior: TextHeightBehavior(
                applyHeightToFirstAscent: false,
                applyHeightToLastDescent: false,
              ),
            ),