【flutter】flutter如何让app内字体大小不随着系统改变而改变

如果我们不特意设置,flutter开发的app他的字体大小是会跟着系统设置的字体大小而改变,这样就会导致页面出现布局错乱问题,那么如何解决这个问题呢?我也搜索了相关资料,有两个常用也是网络上搜集到比较多的方法,还有一个是我自己使用的比较简单粗暴但是我认为方便快捷的方法。

先来看常用方法

方案一、自定义组件继承Text组件,在使用的时候直接使用FixedText来定义

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);
}

方案二、修改全局配置

在main函数中设置MediaQuery.of(context).copyWith(textScaleFactor: 1.0),就可以使得文本不随着系统改变

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

方案三:直接修改Text组件

因为在发现这个问题的时候,代码已经比较庞大并且多处使用Text,挨个替换肯定是不合理的,因此我直接修改了Text的源代码,设置textScaleFactor默认值为1.0

因为我肯定是整体都不能让他随着系统改变而改变,所以直接干脆修改源代码,默认不能改变,简单、粗暴且有效。

猜你喜欢

转载自blog.csdn.net/wuguidian1114/article/details/131986077