I created custom ThemeDatas (light & dark), when I launch my app with them I get a _mulFromInteger() issue. 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. This class calls MediaQuery.of(context) that will modify my screen configurations in function to it's value. The problem is that I call my ThemeData (with the TextTheme) before I call my SizeConfigurations.init(context). If I try putting my init before creating my MaterialApp and setting the theme it tells me I used a context without a 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)
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),
),
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.