Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Last active December 28, 2021 13:09
Show Gist options
  • Save CaiJingLong/d28208f569b44f39dd572a7e8f455912 to your computer and use it in GitHub Desktop.
Save CaiJingLong/d28208f569b44f39dd572a7e8f455912 to your computer and use it in GitHub Desktop.
不随系统字体大小发生变化
import 'package:flutter/material.dart';
import 'scale_text_widget.dart';
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
builder: (ctx, w) {
return MaxScaleTextWidget(
max: 1.0,
child: w,
);
},
);
}
}
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
double textScale = 1;
@override
Widget build(BuildContext context) {
return NotificationListener<ChangeTextScaleNotification>(
onNotification: (n) {
this.textScale = n.max;
setState(() {});
return true;
},
child: MaterialApp(
home: HomePage(),
builder: (context, widget) {
final data = MediaQuery.of(context);
print("textScaleFactor: ${data.textScaleFactor}");
return MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaleFactor: textScale,
),
child: widget,
);
},
),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({
Key key,
}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: <Widget>[
TextField(
style: TextStyle(fontSize: 20),
decoration: InputDecoration(hintText: "Test"),
),
Slider(
min: 0.8,
max: 3,
onChanged: (double value) {
(ChangeTextScaleNotification()..max = value).dispatch(context);
setState(() {});
},
value: MediaQuery.of(context).textScaleFactor,
),
],
),
);
}
}
class ChangeTextScaleNotification extends Notification {
double max;
}
import 'dart:math' as math;
import 'package:flutter/material.dart';
class NoScaleTextWidget extends StatelessWidget {
final Widget child;
const NoScaleTextWidget({
Key key,
@required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaxScaleTextWidget(
max: 1.0,
child: child,
);
}
}
class MaxScaleTextWidget extends StatelessWidget {
final double max;
final Widget child;
const MaxScaleTextWidget({
Key key,
this.max = 1.2,
@required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
var data = MediaQuery.of(context);
var scale = math.min(max, data.textScaleFactor);
return MediaQuery(
data: data.copyWith(textScaleFactor: scale),
child: child,
);
}
}
class ScaleTextWidget extends StatelessWidget {
final double scale;
final Widget child;
const ScaleTextWidget({
Key key,
@required this.scale,
@required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
var data = MediaQuery.of(context);
var scale = this.scale ?? data.textScaleFactor;
return MediaQuery(
data: data.copyWith(textScaleFactor: scale),
child: child,
);
}
}
@CaiJingLong
Copy link
Author

CaiJingLong commented Jan 16, 2020

添加了一个单文件Example: main.dart,
可以动态演示当前的字体变化

使用 https://dartpad.cn/d28208f569b44f39dd572a7e8f455912 可以在线查看效果

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment