20231030 TIL
Today 요약
[✅] 프로그래머스 코딩테스트 3문제
[✅] dart 강의 11~15 5강
[✅] flutter 강의 2강
[] 옵시디언 정리
배운 점
1. javascript
1-1 for문과 forEach 차이
- 동기(sync), 비동기(async)의 차이 : for는 동기방식이기에 for문 안에 오류가 나면 위치 이후의 작업이 동작하지 않고 멈춰버린다. 하지만 forEach문은 비동기 방식이기 때문에 멈추지 않고 동작한다
- forEach문은 "향상된 for문"이라고 칭하며, 가변적인 배열이나 리스트 크기를 구할 필요 없어 복잡한 반복문에 적합하며, 인덱스를 생성하여 접근하는 for문보다 수행속도가 빠르다
- 단점 : 반복문 내에서 배열이나 리스트 값을 변경하거나 추가할 수 없고, 배열을 역순으로 탐색할 수 없다
[JavaScript] for문과 forEach문 차이
for문 예시 const array = new Array(4); for (let i = 0; i < array.length; i ++){ // ... } forEach문 예시 const array = new Array(5); object.forEach(element => { // ... }) 차이점 동기와 비동기 for문은 동기 방식이기 때문에 오류가
bum-developer.tistory.com
2. flutter
2-1 InheritedWidget ( 상속하는 위젯)
- main.dart에서 ThemeData primaryColor : const Color(0000) 을 지정해주면 다른 화면에서 color:Theme.of(context).primaryColor를 사용해서 위젯트리 먼 화면에서도 사용이 가능해진다
- 위젯의 정보가 어디에 있든 어디서든 직접 접근이 가능하다
- 앱 전체에 데이터를 공유하지만 업데이트 하지는 못한다
- InheritedWidget을 쓰거나 이걸 extends 할 때는 child가 필요하다
import 'package:flutter/widgets.dart';
class Widget extends InheritedWidget {
const Widget({super.key, required super.child});
@override
bool updateShouldNotify(covariant InheritedWidget oldWidget) {
// TODO: implement updateShouldNotify
throw UnimplementedError();
}
}
static Config of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<Config>()!;
}
- 위젯트리를 포함하는 context를 받아 InheritedWidget 중에 정확히 Config라는 타입을 찾는 것
- of 메서드는 일반적으로 null을 허용하지 않는 인스턴스를 반환
InheritedWidget class - widgets library - Dart API
Base class for widgets that efficiently propagate information down the tree. To obtain the nearest instance of a particular type of inherited widget from a build context, use BuildContext.dependOnInheritedWidgetOfExactType. Inherited widgets, when referenc
api.flutter.dev
2-2 SwitchListTile class
SwitchListTile class - material library - Dart API
A ListTile with a Switch. In other words, a switch with a label. The entire list tile is interactive: tapping anywhere in the tile toggles the switch. Tapping and dragging the Switch also triggers the onChanged callback. To ensure that onChanged correctly
api.flutter.dev
- 토슬스위치 목록
- CheckboxListTIle, RadioListTile 등과 같은 패턴을 따른다
class _SwitchListTileExampleState extends State<SwitchListTileExample> {
bool _lights = false;
@override
Widget build(BuildContext context) {
return SwitchListTile(
title: const Text('Lights'),
value: _lights,
onChanged: (bool value) {
setState(() {
_lights = value;
});
},
secondary: const Icon(Icons.lightbulb_outline),
);
}
}