Comments (11)
I think , you initialization is not correct.
please show me the code.
from flutter_screenutil.
import 'package:flutter/material.dart';
import 'package:bmatch_flutter/route/routes.dart';
import 'package:flutter/services.dart';
import 'package:bmatch_flutter/provider/auth.dart';
import 'package:bmatch_flutter/utils/adaptor.dart';
void main() async {
runApp(new MainApp());
}
class MainApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context); // is wrong
return new MaterialApp(
title: 'My Login App',
theme: new ThemeData(
brightness: Brightness.light,
primaryColorBrightness: Brightness.light,
accentColorBrightness: Brightness.light,
primaryColor: Colors.white,
primaryTextTheme: TextTheme(title: TextStyle(color: Color(0xFF000000))),
),
routes: routes,
);
}
}
I init before MaterailApp is wrong.
but init in a widget is OK.
from flutter_screenutil.
Be sure to set the page in the MaterialApp's home(ie the entry file, just set it once) to ensure that the fit size is set before each use.
init in first widget(route) is ok , and you only need init once.
from flutter_screenutil.
我也遇到同样的问题
`import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main(){
runApp(new HomePage());
}
class HomePage extends StatelessWidget{
Widget _buildHome(){
return new Container(
width: 750,
color: Color.fromRGBO(200, 200, 200, 1),
child: new Image.asset('images/banner01.png')
);
}
@OverRide
Widget build(BuildContext context) {
//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
return new MaterialApp(
title: '首页',
home: new Scaffold(
appBar: AppBar(
title: new Text('首页'),
),
body: _buildHome(),
),
);
}
}`
from flutter_screenutil.
@lixsh8 能放具体的错误日志以及flutter 和 库的版本吗
from flutter_screenutil.
flutter_screenutil: ^0.5.2
flutter:1.5.4
I/flutter (19461): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (19461): The following assertion was thrown building HomePage(dirty):
I/flutter (19461): MediaQuery.of() called with a context that does not contain a MediaQuery.
I/flutter (19461): No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
I/flutter (19461): This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce
I/flutter (19461): a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
I/flutter (19461): The context used was:
I/flutter (19461): HomePage(dirty)
I/flutter (19461):
I/flutter (19461): When the exception was thrown, this was the stack:
I/flutter (19461): #0 MediaQuery.of (package:flutter/src/widgets/media_query.dart:528:5)
I/flutter (19461): #1 ScreenUtil.init (package:flutter_screenutil/flutter_screenutil.dart:37:44)
I/flutter (19461): #2 HomePage.build (package:ui_xly_home/main.dart:22:65)
I/flutter (19461): #3 StatelessElement.build (package:flutter/src/widgets/framework.dart:3789:28)
I/flutter (19461): #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3739:15)
I/flutter (19461): #5 Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5)
I/flutter (19461): #6 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3722:5)
I/flutter (19461): #7 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3717:5)
I/flutter (19461): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
I/flutter (19461): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
I/flutter (19461): #10 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:933:16)
I/flutter (19461): #11 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:904:5)
I/flutter (19461): #12 RenderObjectToWidgetAdapter.attachToRenderTree. (package:flutter/src/widgets/binding.dart:850:17)
I/flutter (19461): #13 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2258:19)
I/flutter (19461): #14 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:849:13)
I/flutter (19461): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:736:7)
I/flutter (19461): #16 runApp (package:flutter/src/widgets/binding.dart:780:7)
I/flutter (19461): #17 main (package:ui_xly_home/main.dart:5:3)
I/flutter (19461): #18 _runMainZoned.. (dart:ui/hooks.dart:199:25)
I/flutter (19461): #23 _runMainZoned. (dart:ui/hooks.dart:190:5)
I/flutter (19461): #24 _startIsolate. (dart:isolate-patch/isolate_patch.dart:300:19)
I/flutter (19461): #25 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
I/flutter (19461): (elided 4 frames from package dart:async)
I/flutter (19461): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/zygote (19461): Ignoring second debugger -- accepting and dropping
D/EGL_emulation(19461): eglMakeCurrent: 0x9dd28a00: ver 3 0 (tinfo 0xa8f4e9d0)
D/EGL_emulation(19461): eglMakeCurrent: 0x9f904240: ver 3 0 (tinfo 0x9f903280)
E/eglCodecCommon(19461): glUtilsParamSize: unknow param 0x00008741
I/chatty (19461): uid=10086(u0_a86) 1.gpu identical 2 lines
E/eglCodecCommon(19461): glUtilsParamSize: unknow param 0x00008741
from flutter_screenutil.
去掉ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);这句就没问题
from flutter_screenutil.
有时间帮我看下吗
from flutter_screenutil.
有时间帮我看下吗
你这个context不对啊 .
你要在_buildHome 里面初始化
from flutter_screenutil.
_buildHome里面没有context,
我看到你的demo都是放在Widget build(BuildContext context) {}里面的
新手勿喷
from flutter_screenutil.
_buildHome里面没有context,
我看到你的demo都是放在Widget build(BuildContext context) {}里面的
新手勿喷
你看层级不一样 ,
你是在materiapp里初始化的
我是在它的home中的组件里初始化的.
你可以拆分成这样:
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main() {
runApp(new HomePage());
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: '首页',
home: HomeWidget(),
);
}
}
class HomeWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
return new Scaffold(
appBar: AppBar(
title: new Text('首页'),
),
body: _buildHome(),
);
}
Widget _buildHome() {
return new Container(
width: 750,
color: Color.fromRGBO(200, 200, 200, 1),
child: new Image.asset('images/banner01.png'));
}
}
from flutter_screenutil.
Related Issues (20)
- How to display well on different devices with different ratio? HOT 2
- Landscape mode, font size increases causing overflow HOT 5
- BorderRadiusExtension and RadiusExtension named wrong HOT 2
- some text widgets in build mode is missing HOT 15
- i update to flutter 3.19.5 the package dosen't work any more HOT 7
- ScreenUtils is best Utils for Flutter developer. HOT 1
- 5.9.0 oppo colorOS 14.0 Android14列表item有时会空白 HOT 3
- flutter_screenutil 5.9.0, Android phones can run with the screen turned off and wiget will be lost HOT 2
- 关于我使用ScreenUtilInit这种方式初始化,builder会调用两次,导致跳转页面会多生成一个 HOT 7
- u
- 横竖屏切换布局错乱 HOT 13
- Problem rendering in tablet size HOT 1
- 新版本 v5.9.2 以及以上版本,输入框无法正常弹出? HOT 2
- UI crashes in release mode HOT 2
- [Feature/Enhancement] Request for max and min screen size
- [Feature/Enhancement] Does not take the MediaQueryData of the parent
- Containers have extra space on bottom in ios
- flutter_screenutil 6.0.0-alpha.1 HOT 1
- Provide a concise title for your issue, e.g., "Error: The getter 'backgroundColor' isn't defined for the class 'ThemeData'".
- Integration with device_preview to correctly update MediaQueryData
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_screenutil.