Code Monkey home page Code Monkey logo

Comments (11)

lizhuoyuan avatar lizhuoyuan commented on July 20, 2024

I think , you initialization is not correct.

please show me the code.

from flutter_screenutil.

zfha avatar zfha commented on July 20, 2024
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.

lizhuoyuan avatar lizhuoyuan commented on July 20, 2024

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.

lixsh8 avatar lixsh8 commented on July 20, 2024

我也遇到同样的问题
`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.

lizhuoyuan avatar lizhuoyuan commented on July 20, 2024

@lixsh8 能放具体的错误日志以及flutter 和 库的版本吗

from flutter_screenutil.

lixsh8 avatar lixsh8 commented on July 20, 2024

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.

lixsh8 avatar lixsh8 commented on July 20, 2024

去掉ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);这句就没问题

from flutter_screenutil.

lixsh8 avatar lixsh8 commented on July 20, 2024

有时间帮我看下吗

from flutter_screenutil.

lizhuoyuan avatar lizhuoyuan commented on July 20, 2024

有时间帮我看下吗

你这个context不对啊 .
你要在_buildHome 里面初始化

from flutter_screenutil.

lixsh8 avatar lixsh8 commented on July 20, 2024

_buildHome里面没有context,
我看到你的demo都是放在Widget build(BuildContext context) {}里面的
新手勿喷

from flutter_screenutil.

lizhuoyuan avatar lizhuoyuan commented on July 20, 2024

_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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.