gpt4 book ai didi

dart - TextSize 不会根据 deviceSize 或 devicePixelRatio 进行缩放

转载 作者:IT王子 更新时间:2023-10-29 06:46:16 24 4
gpt4 key购买 nike

我想根据各种设备大小或 devicePixelRatio 值缩放我的 flutter 应用程序中的文本。我知道 flutter 应该根据 devicePixelRatio 值缩放文本,所以我以这种方式更改了我的代码,但它在我的代码中不起作用。

这是我的代码:

class MyHomePage extends StatefulWidget {
@override
MyHomePageState createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
TextStyle style50 = new TextStyle(
inherit: true,
fontSize: 50.0,
color: Colors.white,
);
TextStyle style19 = new TextStyle(
inherit: true,
color: Colors.white,
fontSize: 19.0,
fontFamily: 'helvetica_neue',
);
TextStyle style15White = new TextStyle(
inherit: true,
color: Colors.white,
fontSize: 15.0,
fontFamily: 'helvetica_neue',
);
TextStyle style15Green = new TextStyle(
inherit: true,
color: Colors.green,
fontSize: 15.0,
fontFamily: 'helvetica_neue',
);
return new Scaffold(
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/images/home_page_background.png"),
fit: BoxFit.cover,
),
),
child: new Container(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(
margin: new EdgeInsets.only(top: 160.0),
child: new Text(
'JAHMAIKA',
textAlign: TextAlign.center,
style: style50,
),
),
new Container(
margin: new EdgeInsets.only(top: 230.0),
child: new FlatButton(
child: new Container(
padding: new EdgeInsets.only(
left: 45.0, right: 45.0, top: 15.0, bottom: 15.0),
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: new BorderRadius.all(
new Radius.elliptical(40.0, 50.0)),
border: new Border.all(
color: Colors.white,
),
),
child: new Text(
'Create an account',
style: style19,
),
),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new SignUpPage()),
);
}),
),
new Container(
margin: new EdgeInsets.only(top: 16.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(
child: new Text(
'Already have an account?',
textAlign: TextAlign.center,
style: style15White,
),
),
new GestureDetector(
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new SignInPage()),
);
},
child: new Container(
margin: new EdgeInsets.only(left: 16.0),
child: new Text('Login',
textAlign: TextAlign.center, style: style15Green),
),
),
],
),
),
],
),
)),
);
}
}

但这行不通。各种设备中的文本字体大小没有变化。请帮助我。

最佳答案

您是否尝试过根据 MediaQuery.of(context).size.width 的比例计算 fontSize

使用基本设备设置尺寸,并使用它的宽度来计算其他设备的比例尺寸。

  ...
Widget build(BuildContext context) {
// 414.0 is the width of an iPhone 7plus
double fs = 50.0 * MediaQuery.of(context).size.width / 414.0;
// print("MediaQuery.of(context).size.width=${MediaQuery.of(context).size.width}");
// print("fs=$fs");
TextStyle style50 = new TextStyle(
inherit: true,
fontSize: fs,
color: Colors.white,
);
...

关于dart - TextSize 不会根据 deviceSize 或 devicePixelRatio 进行缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51184878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com