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

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

先来看常用方法

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

javascript 复制代码
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),就可以使得文本不随着系统改变

javascript 复制代码
//在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

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

相关推荐
梦想的颜色10 小时前
AI 时代小白 VibeCoding 做 APP:UniApp(含 Uni‑X)、Flutter 与 React Native+Expo全维度技术选型对比指南
flutter·react native·app·uniapp·vibecoding·unippx·app产品
坚果的博客13 小时前
Flutter-OH 3.44.9-dev 悄然上线|首个 OpenHarmony Canary 预览版上线
flutter
大龄秃头程序员13 小时前
Flutter 动画随笔:业务里真正高频的几类控件
flutter
天空之城--15 小时前
Android Flutter行业最新动态与实用参考(2026年8月第2周)
android·flutter
恋猫de小郭15 小时前
Flutter 3.47 发布,快来看看有什么更新吧
android·前端·flutter
大龄秃头程序员1 天前
一次 Flutter 动画 Demo 的整理与复盘
flutter
AlexMaybeBot1 天前
躺在沙发上开发 Openclaw 的移动端APP
前端·flutter
天空之城--1 天前
Android Flutter行业动态与学习参考(2026年8月)
android·flutter
FungLeo1 天前
Flutter Web token 存储陷阱:crypto.subtle 在非安全上下文失效排查实录
前端·安全·flutter
FungLeo1 天前
Flutter Web 中文字体困境与渲染器选择:CanvasKit vs HTML renderer 实战
前端·flutter·html