简体   繁体   English

Flutter 具有自适应文本大小的自定义文本主题

[英]Flutter Custom TextTheme With Adaptive TextSize

I created custom ThemeDatas (light & dark), when I launch my app with them I get a _mulFromInteger() issue.我创建了自定义 ThemeDatas(浅色和深色),当我使用它们启动我的应用程序时,我遇到了 _mulFromInteger() 问题。 This is because the FontSizes in TextTheme inside my ThemeData have values that change according to the screen size, to make these adaptive FontSizes I use a class that contains my size configurations.这是因为我的 ThemeData 中 TextTheme 中的 FontSizes 具有根据屏幕大小而变化的值,为了使这些自适应 FontSizes 我使用包含我的大小配置的 class 。 This class calls MediaQuery.of(context) that will modify my screen configurations in function to it's value.这个 class 调用 MediaQuery.of(context) 它将修改我在 function 中的屏幕配置到它的值。 The problem is that I call my ThemeData (with the TextTheme) before I call my SizeConfigurations.init(context).问题是我在调用我的 SizeConfigurations.init(context) 之前调用了我的 ThemeData(带有 TextTheme)。 If I try putting my init before creating my MaterialApp and setting the theme it tells me I used a context without a MediaQuery.如果我在创建 MaterialApp 并设置主题之前尝试将我的 init 放在它告诉我我使用了没有 MediaQuery 的上下文。 Any idea how to solve this?知道如何解决这个问题吗? Thanks:)谢谢:)

Code:代码:

  @override
  Widget build(BuildContext context) {
    // When I call SizeConfig().init(context); here it tells me the context doesn't have a MediaQuery
    return MaterialApp(
      theme: AppTheme.getLightTheme(),
      darkTheme: AppTheme.getDarkTheme(),
      themeMode: ThemeMode.system,
      title: 'App Title',
      onGenerateRoute: NavigationRouter.generateRoute,
      home: FutureBuilder(
        future: jwtOrEmpty,            
        builder: (context, snapshot) {
          SizeConfig().init(context);
          …
        }
      ),
    );
  }

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
You can use MediaQueryData.fromWindow(ui.window)您可以使用MediaQueryData.fromWindow(ui.window)
code snippet代码片段

void init() {
    _mediaQueryData = MediaQueryData.fromWindow(ui.window);
    _screenWidth = _mediaQueryData.size.width;
    _screenHeight = _mediaQueryData.size.height;

full code完整代码

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

class SizeConfig {
  static MediaQueryData _mediaQueryData;
  static double _screenWidth;
  static double _screenHeight;

  double get screenWidth => _screenWidth;
  double get screenHeight => _screenHeight;

 void init() {
    _mediaQueryData = MediaQueryData.fromWindow(ui.window);
    _screenWidth = _mediaQueryData.size.width;
    _screenHeight = _mediaQueryData.size.height;
    print(_screenWidth);
    print(_screenHeight);
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SizeConfig().init();

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      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) {
    print(MediaQuery.of(context).size.width);
    print(MediaQuery.of(context).size.height);
    print(SizeConfig().screenWidth);

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            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),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM