[flutter] How flutter prevents the font size in the app from changing with system changes

If we do not set it deliberately, the font size of the app developed by Flutter will change according to the font size set by the system, which will cause the layout of the page to be disordered. So how to solve this problem? I also searched for relevant information. There are two commonly used methods that are widely collected on the Internet. There is also a method that I use myself that is relatively simple and crude but I think is convenient and fast.

Let’s look at the common methods first

Solution 1: The custom component inherits the Text component and directly uses FixedText to define it when using it.

import 'package:flutter/material.dart';
import 'package:flutter_app2/View/FixedSizeText.dart';

class FixedText extends Text {
  const FixedText(String data, {
    Key key,
    TextStyle style,
    StrutStyle strutStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    Locale locale,
    bool softWrap,
    TextOverflow overflow,
    double textScaleFactor = 1.0,
    int maxLines,
    String semanticsLabel,
  }) : super(data,
      key:key,
      style:style,
      strutStyle:strutStyle,
      textAlign:textAlign,
      textDirection:textDirection,
      locale:locale,
      softWrap:softWrap,
      overflow:overflow,
      textScaleFactor:textScaleFactor,
      maxLines:maxLines,
      semanticsLabel:semanticsLabel);
}

Option 2. Modify global configuration

Set MediaQuery.of(context).copyWith(textScaleFactor: 1.0) in the main function to prevent the text from changing with the system

//在main函数中,设置builder
MaterialApp(
    home:Home(),
    builder: (context, widget) {
	    return MediaQuery(
		   ///设置文字大小不随系统设置改变
		   data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
		   child: widget,
		);
    },
),

Option 3: Directly modify the Text component

Because when I discovered this problem, the code was already relatively large and used Text in many places. It was definitely unreasonable to replace them one by one, so I directly modified the source code of Text and set the default value of textScaleFactor to 1.0.

Because I am sure that the whole system cannot be changed as the system changes, so I simply modify the source code. It cannot be changed by default. It is simple, crude and effective.

 

Guess you like

Origin blog.csdn.net/wuguidian1114/article/details/131986077