基于 Flutter 设置中字体大小的自适应字体大小
Adaptive font size based on font size in settings on Flutter
我正在用 flutter 做一个应用。
所以如果我的phone有正常的字体设置,它看起来像这样。
但是如果我有大字体,它看起来像
有办法解决这个问题吗?
您可以复制粘贴 运行 下面的完整代码
您可以使用包 https://pub.dev/packages/auto_size_text
AutoSizeText
以 TextStyle.fontSize
开头。它测量生成的文本并重新缩放以适应其键
代码片段
Container(
decoration: BoxDecoration(
border: Border.all(),
),
width: 100.0,
height: 140.0,
child: AutoSizeText(
'Important',
style: TextStyle(fontSize: 30.0),
maxLines: 1,
),
),
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:auto_size_text/auto_size_text.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(),
),
width: 100.0,
height: 140.0,
child: Text(
'Important',
style: TextStyle(fontSize: 30.0),
),
),
Container(
decoration: BoxDecoration(
border: Border.all(),
),
width: 100.0,
height: 140.0,
child: AutoSizeText(
'Important',
style: TextStyle(fontSize: 30.0),
maxLines: 1,
),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
我正在用 flutter 做一个应用。
所以如果我的phone有正常的字体设置,它看起来像这样。
但是如果我有大字体,它看起来像
有办法解决这个问题吗?
您可以复制粘贴 运行 下面的完整代码
您可以使用包 https://pub.dev/packages/auto_size_text
AutoSizeText
以 TextStyle.fontSize
开头。它测量生成的文本并重新缩放以适应其键
代码片段
Container(
decoration: BoxDecoration(
border: Border.all(),
),
width: 100.0,
height: 140.0,
child: AutoSizeText(
'Important',
style: TextStyle(fontSize: 30.0),
maxLines: 1,
),
),
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:auto_size_text/auto_size_text.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(),
),
width: 100.0,
height: 140.0,
child: Text(
'Important',
style: TextStyle(fontSize: 30.0),
),
),
Container(
decoration: BoxDecoration(
border: Border.all(),
),
width: 100.0,
height: 140.0,
child: AutoSizeText(
'Important',
style: TextStyle(fontSize: 30.0),
maxLines: 1,
),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}