์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
- FloatingActionButton
- stateful
- InputDecoration
- drawer
- flutter
- Routing
- slider
- PushNamed
- switch
- figma
- GestureDetector
- textField
- button
- vscode
- TextEditingController
- POP
- icon
- ElevatedButton
- Snackbar
- AlertDialog
- ontap
- list
- dart
- scaffoldmessenger
- sizedbox
- AppBar
- navigator
- Scaffold
- Webflow
- image
- Today
- Total
์ฌ๋ฐ๋๊ฑฐ๐
Flutter_11. Drawer, ListTitle, Navigator, body, class, Stateful ๋ณธ๋ฌธ
Flutter_11. Drawer, ListTitle, Navigator, body, class, Stateful
uraon 2023. 5. 5. 23:22์ค๋์ ์ง๋๋ฒ ํฌ์คํ ์ ์ด์ด์ Drawer์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ ํ์ด์ง ์ด๋์ ์์ผ๋ณด๋๋ก ํ๊ฒ ๋ค.
2023.05.04 - [๊ฐ๋ฐํ๊ธฐ] - Flutter_10. Drawer, AppBar IconButton
Flutter_10. Drawer, AppBar IconButton
์ค๋์ AppBar์ IconButton์ ๋ฃ๊ณ , ํด๋ฆญ์ Drawer๋ฅผ ์ด์ด๋ณด๋ ๊ฒ์ ๋ฉ๋ชจํ๋ คํ๋ค. AppBar์ ์ฌ์ฉ๋๋ IconButton์ ์์น์ ๋ฐ๋ผ leading, actions๋ก ๋๋๋ค. ์์ธํ ๋ด์ฉ์ ์๋ ๋งํฌ ํ์ธ! :) https://api.flutter.dev/
uraon.tistory.com
Routing, Drawer, ListView
1) ํด๋ "page"์ ํ์ผ ์ถ๊ฐํ๊ณ , main.dart ํ์ผ์ Routing ์ ํ
2) home.dart ํ์ผ > Drawer - Header ๋ง๋ค๊ธฐ ๋ณต์ต ๋ฉ๋ชจ!
UserAccountsDrawerHeader
- ๋ฉ์ธ ์ด๋ฏธ์ง currentAccountPicture: CircleAvatar
- ์๋ธ ์ด๋ฏธ์ง otherAccountsPictures: [CircleAvatar, CircleAvatar, ...]
- ๊ณ์ ๋ค์ด๋ฐ accountName: Text
- ๊ณ์ ์ด๋ฉ์ผ accountEmail: Text
- ๊พธ๋ฏธ๊ธฐ decoration: BoxDecoration()
3) home.dart ํ์ผ > Drawer - ListTitle ๋ง๋ค๊ธฐ
ListTitle
- ์์ด์ฝ leading: Icon
- ๋ฆฌ์คํธ title: Text
- ์์ด์ฝ trailing: Icon
- ํด๋ฆญ ์ด๋ฒคํธ onTap: (){}
4) Navigator.pop์ผ๋ก ํญํ๋ฉด Drawer ๋ซํ ์ฒ๋ฆฌ
5) ์ ์ฅํ๊ณ Drawer ํด๋ฆญ ํ์ธ! ์ด์์ด ์๋ค๋ฉด, ListTitle ๋ณต์ฌ~*-*//
#Navigator.pop์ผ๋ก ๋ซ๊ณ , pushNamed๋ก ์ด๋ ๊ฒฝ๋ก ์ค์
6) ์ ์ฅํ๊ณ Drawer ํด๋ฆญ๊ณผ ํ์ด์ง ์ด๋ ํ์ธ!
body, class, StatefulWidget
Stateless๋ก home.dart ํ์ผ์ ๋ง๋ค๊ณ , Drawer๋ฅผ ๋ฃ์๋๋ ์ฝ๋๊ฐ ์กฐ๊ธ ๊ธธ์ด์ก๋ค.
๊ทธ๋ฆฌ๊ณ , ํ๋ฉ์ธ์ ์ฒซ๋ฒ์งธ๋ก ๋ณต์ตํ๊ณ ์ ํ๋ ๊ฒ์ด ์ด๋ฏธ์ง ์ค์์ดํ ํ๋ฉด์์ผ๋ก ์์ง์ ๋ณํ๊ฐ ์๋ ์์ ฏ์ด ํ์ํ๋ค.
๊ทธ๋์ body๋ฅผ ๋ณ๋์ Stateful ํ์ผ๋ก ๋ถ๋ฆฌํด๋ณด์๋ค. ๋ฐฉ๋ฒ์ ๊ฐ๋จํ๋ค :)
- Stateless : ์ํ๊ฐ ์๋ ์ ์ ์์ ฏ
- Statefull : ์์ง์ ๋ณํ๊ฐ ์๋ ์์ ฏ
1) home.dart ํ์ผ body์ const๋ก class ์ด๋ฆ "ImageSwipe"๋ก ๋ฃ๊ธฐ
2) img_swipe.dart ํ์ผ ์ ๊ท ์์ฑ
3) sateful๋ก class ๋ง๋ค๊ณ , return์ body์ Center๋ถํฐ ๋ด์ฉ ์ถ๊ฐ
- stf + Enter : StatefulWidget์ผ๋ก ๊ธฐ๋ณธ ์์ ์๋์ถ๋ ฅ
- ํด๋์ค ์ด๋ฆ "ImageSwipe"๋ก ๋ณ๊ฒฝ
- ์๋ ์์ฑ๋ import ์ง์ฐ๊ณ , material ์ถ๊ฐ
- ๋ฆฌํด๊ฐ Center();๋ก ๋ณ๊ฒฝํ๊ณ , home.dart์ Center๋ด์ฉ ์งค๋ผ์ ๋ถ์ฌ๋ฃ๊ธฐ
์ ์ฅํ๊ณ ํ์ธํ๋ฉด, ํ์ผ๋ง ๋ถ๋ฆฌ๋์๊ณ ํ๋ฉด์ ๊ทธ๋๋ก:)
Stateful Life Cycle
๐ Stateless Widgets
Constructor Function -> build()
๐ Stateful Widgets์ Life Cycle
Constructor Function -> initState() -> build() -> setState() -> didUpdateWidget() -> build()
initState() : ์ํ ์ด๊ธฐํ(=๋ณ์ ์ ์)
build() : ๋ง๋ค๊ณ
setState() : ์ํ ๊ฐฑ์ -> build()
์๋ ์ฝ๋๋ฅผ ์คํํด๋ณด๋ฉด, initState()๋ ์ต์ด 1ํ๋ง ์คํ๋๋ค.
๋ฒํผ ํด๋ฆญ์ ๋ฐ๋ผ ์ํ๊ฐ ๋ณ๊ฒฝ๋๋ฉฐ, ์ํ๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง๋ค setState๋ฅผ ํตํด build()๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
// 1. ํ๋์ (์ ์ญ)๋ณ์ late๋ก ์ ์ธ
late String _buttonState;
late Color _buttonColor;
// 2. initState : ์ด๊ธฐ๊ฐ ์ค์ (์ฑ ์ฒ์ ์คํํ ๋๋ง run)
@override
void initState() {
super.initState();
print('initState()');
_buttonState = "OFF";
_buttonColor = Colors.amber;
}
@override
void deactivate() {
print('deactivate()');
super.deactivate();
}
// 3. dispose : ์ฑ์ด ์ข
๋ฃ์ ์
@override
void dispose() {
print('dispose()');
super.dispose();
}
@override
Widget build(BuildContext context) {
print('build()');
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Life Cycle'),
backgroundColor: const Color.fromARGB(255, 98, 104, 255),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(_buttonColor)
),
onPressed: () {
// -------------
_onClick();
},
child: const Text('๋ฒํผ์ ๋๋ฅด์ธ์'),
),
const SizedBox(
height: 50,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('๋ฒํผ ์ํ :'),
const SizedBox(
width: 10,
),
Text(_buttonState)
],
)
],
),
),
);
}
// --- Functions ---
_onClick(){
// setState : buildํํ
๋ณ์ ๋ณ๊ฒฝ ์๋ ค์ฃผ๊ธฐ!
setState(() {
if(_buttonState == "OFF"){
_buttonState = 'ON';
_buttonColor = Colors.red;
}else{
_buttonState = 'OFF';
_buttonColor = Colors.amber;
}
});
print(_buttonState);
}
}//End
'๊ฐ๋ฐํ๊ธฐ > Flutter Start' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Flutter_13. Stateful, Switch, setState, Button (0) | 2023.05.06 |
---|---|
Flutter_12. Package, Gesture, List, Image, Swipe, Button (0) | 2023.05.06 |
Flutter_10. Drawer, ListView, UserAccountsDrawerHeader, AppBar IconButton (0) | 2023.05.04 |
Flutter_09.Navigator, Push, PushNamed, Pop (0) | 2023.05.03 |
Flutter_08. AlertDialog, Navigator, Routing, PushNamed (0) | 2023.05.03 |