【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

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

相关推荐
Android-Flutter3 小时前
flutter GetX 详解
android·flutter
坚果的博客7 小时前
Flutter OHOS 环境搭建实战:oh-3.44.9-dev 从 0 到 1 完整记录
flutter·华为·harmonyos
Crazy_MT9 小时前
Android Studio 运行 Flutter iOS 真机白屏,但 Xcode 和命令行正常的排查记录
flutter·android studio
Android-Flutter10 小时前
flutter async/await 详解
android·flutter
SoaringHeart11 小时前
Flutter 进阶 | 组件封装:用 CustomPainter 实现光环动画组件AnimatedHalo
前端·flutter
Light Gao11 小时前
企业级移动端 APP 架构设计:从原生双端到 Hybrid、React Native 与 Flutter
flutter·react native·react.js
末代iOS程序员华仔1 天前
iOS 5.6 条例解决方法
flutter·ios·swift
末代iOS程序员华仔1 天前
Guideline 5.6 - Developer Code of Conduct
flutter·ios·swift
恋猫de小郭1 天前
聊个比较有意思的 Flutter Web 问题
android·前端·flutter
2501_915918411 天前
Flutter项目配置iOS混淆的详细步骤与工具推荐
android·flutter·ios·小程序·uni-app·cocoa·iphone