[Flutter] Conditonal 하게 Rendering하는 방법
2023. 3. 5. 22:43ㆍDevelopers 공간 [Shorts]/Frontend
728x90
반응형
<분류>
A. 수단
- OS/Platform/Tool : Linux, Kubernetes(k8s), Docker, AWS
- Package Manager : node.js, yarn, brew,
- Compiler/Transpillar : React, Nvcc, gcc/g++, Babel, Flutter
- Module Bundler : React, Webpack, Parcel
B. 언어
- C/C++, python, Javacsript, Typescript, Go-Lang, CUDA, Dart, HTML/CSS
C. 라이브러리 및 프레임워크 및 SDK
- OpenCV, OpenCL, FastAPI, PyTorch, Tensorflow, Nsight
1. What? (현상)
new Center(
height: condition == true ?
Mediaquery.of(context).size.height*0.7 :
Mediaquery.of(context).size.height*0.4
)
조건에 따라 Rendering을 다르게 하고 싶을 때 하는 방법 중
삼항연산자 ternary operator가 잘 동작을 안한다?
2. Why? (원인)
- 저의 경우는 코드 상 제 실수였습니다... 본인의 방법이 확실하지 않다면 하기 다른 방법을 시도해봅니다.
3. How? (해결책)
- 방법1. Flutter 에서 Condotional 하게 Child Widget을 Rendering 하는 다른 방법을 사용한다.
- 3항 연산자(Tenary operator)
condition ? Text("True") : null - if문
class StatmentExample extends StatelessWidget {
Widget build(BuildContext context) {
return Text( (() {
if(true){
return "tis true";}
return "anything but true";
})());
}
}
혹은 ({}불가)
children: [
oneItem,
if(isVisible) VisibleWidget()
]
혹은
Column(
children: [
if (_id == 0) ...[
Container()
] else if(_id == 1)...[
Text("Hello")
] else ...[
SizedBox(height: 20)
],
],
), - for문 Rendering
children: [
...manyItems,
oneItem,
if(canIKickIt)
...kickTheCan
for (item in items)
Text(item) - method를 사용하기
child: getWidget()
Widget getWidget() {
if (x > 5) ...
//more logic here and return a Widget
혹은 (case2함수 하기 참조)
child: case2(myInput,
{
1: Text("Its one"),
2: Text("Its two"),
}, Text("Default")); - map문 Rendering (paragraph 함수 하기 참조)
...data.map((f) => paragraph(f.title, f.description)).toList(),
- 3항 연산자(Tenary operator)
// Case 4
TValue case2<TOptionType, TValue>(
TOptionType selectedOption,
Map<TOptionType, TValue> branches, [
TValue defaultValue = null,
]) {
if (!branches.containsKey(selectedOption)) {
return defaultValue;
}
return branches[selectedOption];
}
//==================================================
// Case 5
Column paragraph(String title, String description) {
return new Column(
children: <Widget>[
Container(
child: Text(title),
),
Container(
child: Text(description),
),
],
);
}
List<AboutUsData> data = [
new AboutUsData(
title: 'Our Vision',
description:
'OUR VISION',
),
new AboutUsData(
title: 'Our Mission',
description:
'OUR MISSION',
),
new AboutUsData(
title: 'Our Values',
description:
'As we grow as a company',
),
];
- 방법2. Stateful을 사용해 내부 변수를 셋팅한다.
- 방법3. SizeChangedLayoutNotifier와 NotificationListener를활용한다.
NotificationListener(
onNotification: (SizeChangedLayoutNotification notification){
Future.delayed(Duration(milliseconds: 300),(){setState(() {
print('size changed');
_height++;
});});
return true;
},
child: SizeChangedLayoutNotifier( child: AnimatedContainer(width: 100, height: _height))
)
https://roseline.oopy.io/07505f44-8a0f-45cf-986c-38900b06e35d
728x90
반응형
'Developers 공간 [Shorts] > Frontend' 카테고리의 다른 글
[Flutter] List, ListView, ListView.Builder 차이 (0) | 2023.03.11 |
---|---|
[Flutter] A GlobalKey was used multiple times inside one widget's child list (0) | 2023.03.06 |
[Flutter] Stacked 패키지에서 initState가 동작을 안한다? (State에 대해서....) (0) | 2023.03.05 |
[Flutter] 내부 widget이 부여받은 사이즈로 구성하는 방법 (0) | 2023.03.05 |
[Flutter] Flutter에 아이폰을 연결해주기 (0) | 2023.02.28 |