본문 바로가기

Flutter

[j Flutter] MediaQuery.of(context).size.width 처음 사용할때 width가 0일때 처리

728x90
반응형

 

 

MediaQuery.of(context).size.width를 사용하다보면 페이지 처음 이동시 0이 나올 때 처리를 해보자.

 

Future<double> whenNotZero(Stream<double> source) async {
    await for (double value in source) {
        print("Width:" + value.toString());
        if (value > 0) {
            print("Width > 0: " + value.toString());
            return value;
        }
    }
    // stream exited without a true value, maybe return an exception.
}

MediaQuery.of(context).size.width : 0보다 큰지 확인 하기 위해 50 밀리 초마다 확인한다.

FutureBuilder(
    future: whenNotZero(
        Stream<double>.periodic(Duration(milliseconds: 50),
            (x) => MediaQuery.of(context).size.width),
    ),
    builder: (BuildContext context, snapshot) {
        if (snapshot.hasData) {
            if (snapshot.data > 0) {
                return Container( 
                    child: //... Your widgets here!
                );
            }
        } else {
            return Container();
        }
    }
),

 

 

 

 

출처 :

github.com/flutter/flutter/issues/25827#issuecomment-478238747

 

MediaQuery.of(context).size.height returns 0 on startup in release mode · Issue #25827 · flutter/flutter

I have a widget which is a member of a stack that's in the body of Scaffold: MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( body: S...

github.com

728x90
반응형