如何在颤振中获取设备字体大小

Oma*_* Fd 4 flutter

当高度有界时,我遇到了列表视图的问题,所以当我更改手机字体大小时,会发生溢出,我不想为容器提供额外的高度。

Container(
       height: fixed height goes here,
       child: ListView(
         scrollDirection: Axis.horizontal,
         children: <Widget>[
           some widgets goes here...
         ],
       ),
     )
Run Code Online (Sandbox Code Playgroud)

小智 8

您可以尝试依赖 textScaleFactor,默认情况下,1.0 如果您在设备的“设置”页面上更改字体大小,则此值将更改为 1.15 1.3 等等

所以你可以用这个值乘以容器高度

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: SafeArea(child: Home()),
      ),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    double h = MediaQuery.of(context).textScaleFactor;
    return Center(
      child: Text('$h'), // with default settings it shows 1.0
    );
  }
}
Run Code Online (Sandbox Code Playgroud)