4

I want to scale the text in my flutter application according to various devices size or devicePixelRatio value. I know flutter should scale text according to devicePixelRatio value, so I change my code in that way, but it's not working in my code.

Here is my code:

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),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          )),
    );
  }
}

But this is not working. There is no change in the text font size in various devices. Please help me.

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Rosemary
  • 1,049
  • 2
  • 11
  • 13

1 Answers1

1

Have you tried computing the fontSize based on a proportion of MediaQuery.of(context).size.width ?

Set the sizes using a base device, and use it's width to calculate the other devices proportional sizes.

  ...
  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,
    );
    ...
Feu
  • 5,372
  • 1
  • 31
  • 57
  • It doesn't work properly. When you switch from portrait to landscape on the device, this "constant" changes with it. So, it will oversizing just by simply rotating the screen, and that's not the behavior expected. – José Pulido Mar 11 '21 at 03:12