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

标签 dart flutter flutter-layout

我想根据各种设备大小或 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/

相关文章:

Flutter - 墨水池 : Why does onHover() require onTap()?

dart - 如何在Dart编辑器中设置 Release模式

flutter - 如何从 Flutter 中的列表中提取 JSON?

dart - Flutter 在图像上绘制并缩放并移动两者

flutter - 如何从 Flutter Searchdelegate 中删除飞溅

android - 如何在Rikulo中使用Cordova/Phonegap文件系统API?

flutter - 如何在 Dart (Flutter) 的回调函数中捕获异常?

java - Android应用设备体积

flutter - Cupertino widgets 会在 android 上运行吗

Flutter,如何设置最大高度以列出 View 项目