Build Context
// 상위 트리 클래스 MyApp 에서 theme 지정
...
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
textTheme: const TextTheme(
titleLarge: TextStyle(
color: Colors.red,
),
),
),
...
// 하위 트리 클래스 _MyLargeTitle 에서 theme 사용
Widget build(BuildContext context) {
return Text(
'Click Count',
style: TextStyle(
fontSize: 30,
color: Theme.of(context)
.textTheme
.titleLarge!
.color,
),
);
}
Widget Lifecycle
Widget has Lifecycle and Lifecycle functions that we can use.
It has initState(), build, dispose()
setState
We can implement well by using Widget Lifecycle 👍
It's example code.
class _MyLargeTitle extends StatefulWidget {
const _MyLargeTitle();
State<_MyLargeTitle> createState() => _MyLargeTitleState();
}
class _MyLargeTitleState extends State<_MyLargeTitle> {
// int count = 0;
// build 를 하기 전에 먼저 호출
// 대표적으로 API 불러올때 사용
void initState() {
print('initState!');
super.initState();
}
// 해당 위젯이 화면에서 사라질때 호출된다.
void dispose() {
print('dispose');
super.dispose();
}
Widget build(BuildContext context) {
print('build!');
return Text(
'Click Count',
style: TextStyle(
fontSize: 30,
color: Theme.of(context)
.textTheme
.titleLarge!
.color, // titleLarge 는 항상 있다는 뜻 (!), titleLarge 는 있을 수도 없을 수도 있다는 뜻 (?)
),
);
}
}