无夜之星辰
无夜之星辰

Reputation: 6158

FontWeight not work with Chinese in Flutter

Below is my test code:

Text(
  'I love 中国',
  style: TextStyle(
    fontSize: 30,
    fontWeight: FontWeight.w300,
    color: Colors.red,
  ),
),
Text(
  'I love 中国',
  style: TextStyle(
    fontSize: 30,
    fontWeight: FontWeight.w500,
    color: Colors.red,
  ),
),

Result:

enter image description here

You can see with different fontWeight letter works but Chinese act the same.

How can I solve it?

My flutter is 3.3.2.

Note: run on iOS.

Upvotes: 0

Views: 672

Answers (2)

无夜之星辰
无夜之星辰

Reputation: 6158

Solution 1: Set theme in your MaterialApp:

theme: ThemeData(
  fontFamily: "PingFang SC",
),

Solution 2: Set fontFamilyFallback where you need:

Text(
  'I love 中国',
  style: TextStyle(
    fontSize: 30,
    fontWeight: FontWeight.w500,
    color: Colors.red,
    fontFamilyFallback: ["PingFang SC", "Heiti SC"],
  ),
),

Solution 3: Use DefaultTextStyle with fontFamilyFallback:

DefaultTextStyle(
  style: TextStyle(
    fontFamilyFallback: ["PingFang SC", "Heiti SC"],
    color: Colors.red,
    fontSize: 30,
  ),
  child: Column(
    children: [
      Text(
        'I love 中国',
        style: TextStyle(
          fontWeight: FontWeight.w400,
        ),
      ),
      Text(
        'I love 中国',
        style: TextStyle(
          fontWeight: FontWeight.w300,
        ),
      ),
      Text(
        'I love 中国',
        style: TextStyle(
          fontWeight: FontWeight.w500,
        ),
      ),
    ],
  ),
),

enter image description here

Upvotes: 0

mohammad esmaili
mohammad esmaili

Reputation: 1727

Flutter will search for a font on the host system that is closest to the specified font weight. But if the system only provides one font that handles the requested characters, then Flutter and Skia will synthesize a "fake bold" version of that font to handle other font weights.

Due to issue on github

Upvotes: 2

Related Questions