일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 앱바
- drift
- Navigation
- ScrollView
- CustomScrollView
- Button
- LifeCycle
- binding
- textfield
- appbar
- 테스트
- textview
- Dialog
- scroll
- DART
- 앱
- Kotlin
- TEST
- livedata
- tabbar
- android
- Compose
- viewmodel
- activity
- data
- intent
- Coroutines
- 계측
- 안드로이드
- Flutter
- Today
- Total
Study Record
[Flutter] MediaQuery , MediaQueryData 본문
✍ MediaQuery
MediaQuery 는 정의에 따르면, "미디어 쿼리가 지정된 데이터를 확인하는 하위 트리를 설정합니다." 라고 되어있다. 조금 더 쉽게 이야기하면 현재 미디어(ex. 앱 화면)에 대한 정보를 주면 데이터를 확인해주는 하위 트리를 설정해주고 우리는 그걸로 미디어에 대한 정보(MediaQueryData)를 얻을 수 있다.
현재 화면에 대한 미디어 정보는 다음과 같이 얻는다.
...
Widget build(BuildContext context) {
MediaQueryData mediaData = MediaQuery.of(context);
}
MediaQuery.of 는 BuildContext 를 기반으로 관련된 MediaQuery 를 찾을 수 없으면 예외를 발생시킨다. 이럴 경우엔 MediaQuery.maybeOf 를 사용하면 예외를 발생시키지 않고 null 을 반환하게 된다.
✍ MediaQueryData
MediaQueryData 에서 현재 미디어에 대한 정보를 얻을 수 있다.
😶 padding, viewPadding, viewInsets
EdgeInset 값을 가지는 세가지 padding, viewPadding, viewInsets 값이 있는데 padding 은 iPhone X 에서의 notch 와 같이 완전히 보이지 않을 수 있는 영역을 의미하고 viewInsets 는 키보드에 가려 보이지 않는 영역을 의미한다.
완전히 보이지 않을 수 있는 영역이란 안드로이드에서 상태바나 아이폰 하단의 보이지 않을 수 있는 영역 등을 뜻한다.
padding = max(0, viewPadding- viewInsets) 이다. padding 과 viewPadding 은 viewInsets 값의 변화가 있으면 차이를 보인다.
예를 들어, 키보드가 보이지 않을 때 padding 과 viewPadding 의 bottom 값은 모두 40 으로 동일하지만
키보드가 위로 올라오면, viewPadding 은 40 으로 값을 유지하지만 padding 은 40 - 320 < 0 이므로 0 이 된다.
+ MediaQueryData.viewPadding.top = 안드로이드의 상태바 높이
😶 화면 크기
// 화면 너비와 높이
MediaQuery.of(context).size.width
MediaQuery.of(context).size.height
// 안드로이드의 경우 화면 높이
MediaQuery.of(context).size.height - MediaQuery.of(context).padding.top
😶 방향
// 가로 : Orientation.landscape
// 세로 : Orientation.portrait
MediaQuery.of(context).orientation
MediaQuery class - widgets library - Dart API
Establishes a subtree in which media queries resolve to the given data. For example, to learn the size of the current media (e.g., the window containing your app), you can read the MediaQueryData.size property from the MediaQueryData returned by MediaQuery
api.flutter.dev
MediaQueryData class - widgets library - Dart API
Information about a piece of media (e.g., a window). For example, the MediaQueryData.size property contains the width and height of the current window. To obtain the current MediaQueryData for a given BuildContext, use the MediaQuery.of function. For examp
api.flutter.dev
'Flutter' 카테고리의 다른 글
[Flutter] key? (0) | 2023.03.02 |
---|---|
[Flutter] 바텀 시트에서 입력받기(TextField) - 키보드 위로 패딩 조절 (0) | 2023.02.28 |
[Flutter] version solving failed error - 중복된 라이브러리 (0) | 2023.02.22 |
[Flutter] 스케줄러 앱 (0) | 2023.02.20 |
[Flutter] 권한 추가하기(permission_handler) (0) | 2023.02.19 |