Dismissibleλ 리μ€νΈ μμ΄ν
μ μ’μ°λ‘ μ€μμ΄νν΄μ μμ , 보κ΄, μλ£ κ°μ μ‘μ
μ μ€νν μ μκ² ν΄μ£Όλ Flutter μμ ―μ
λλ€.
β’
child: νλ©΄μ 보μ΄λ μ€μ μ½ν
μΈ
β’
background / secondaryBackground: μ€μμ΄ν μ€ λ€μμ 보μ΄λ λ°°κ²½
β’
direction: μ€μμ΄ν λ°©ν₯ μ ν
β’
confirmDismiss: μ€μ λ‘ μ κ±°νκΈ° μ νμΈ λ€μ΄μΌλ‘κ·Έ λ± μ²λ¦¬
β’
onDismissed: μ€μμ΄ν μλ£ ν μμ μ²λ¦¬μ μ€λ΅λ° νμ λ±μ μ¬μ©
Dismissible μμ ―
μΈμ μ¬μ©νλκ°
β’
ν μΌ λͺ©λ‘ (Todo List)
β’
μ΄λ©μΌ μ± (μ½μ μ²λ¦¬ / μμ )
β’
μ±ν
리μ€νΈ (λν μμ )
β’
κ°€λ¬λ¦¬ 리μ€νΈ (μ¬μ§ μ κ±°)
기본 ꡬ쑰
Dismissible(
key: Key(item.id), // λ°λμ κ³ μ κ°
child: ListTile(
title: Text(item.title),
),
)
Dart
볡μ¬
ν΅μ¬ μμ± μ 리
child
child: ListTile(title: Text("ν μΌ"))
Dart
볡μ¬
background / secondaryBackground
background: Container(
color: Colors.red,
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(left: 20),
child: Icon(Icons.delete, color: Colors.white),
),
secondaryBackground: Container(
color: Colors.blue,
alignment: Alignment.centerRight,
padding: EdgeInsets.only(right: 20),
child: Icon(Icons.archive, color: Colors.white),
),
Dart
볡μ¬
β’
background
μΌμͺ½ β μ€λ₯Έμͺ½ μ€μμ΄ν
β’
secondaryBackground
μ€λ₯Έμͺ½ β μΌμͺ½ μ€μμ΄ν
direction
direction: DismissDirection.horizontal
Dart
볡μ¬
μ΅μ
:
β’
horizontal
μ’μ° κ°λ₯
β’
vertical
μμλ κ°λ₯
β’
endToStart
μ€λ₯Έμͺ½ β μΌμͺ½
β’
startToEnd
μΌμͺ½ β μ€λ₯Έμͺ½
confirmDismiss
confirmDismiss: (direction) async {
return await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("μμ νμκ² μ΅λκΉ?"),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: Text("μ·¨μ"),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: Text("μμ "),
),
],
),
);
},
Dart
볡μ¬
onDismissed
onDismissed: (direction) {
setState(() {
items.removeAt(index);
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("μμ λμμ΅λλ€")),
);
},
Dart
볡μ¬
μ 체 μμ
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return Dismissible(
key: Key(item.id),
background: Container(
color: Colors.red,
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(left: 20),
child: Icon(Icons.delete, color: Colors.white),
),
secondaryBackground: Container(
color: Colors.green,
alignment: Alignment.centerRight,
padding: EdgeInsets.only(right: 20),
child: Icon(Icons.check, color: Colors.white),
),
confirmDismiss: (direction) async {
return await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("μ λ§ μμ νμκ² μ΅λκΉ?"),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: Text("μ·¨μ"),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: Text("μμ "),
),
],
),
);
},
onDismissed: (direction) {
setState(() {
items.removeAt(index);
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("${item.title} μμ λ¨")),
);
},
child: ListTile(
title: Text(item.title),
),
);
},
);
Dart
볡μ¬
TIP
1. keyλ μ λ μ€λ³΅λλ©΄ μλ¨
key: Key(item.id)
Dart
볡μ¬
2. confirmDismiss μμ΄ μμ νλ©΄ UX μμ’μ
3. μνκ΄λ¦¬λ κ°μ΄ μ°λ©΄ λ κ°λ ₯
β’
Provider
β’
Riverpod
β’
Bloc
4. λ°©ν₯λ³ λ€λ₯Έ κΈ°λ₯ ꡬν κ°λ₯
onDismissed: (direction) {
if (direction == DismissDirection.startToEnd) {
// μλ£ μ²λ¦¬
} else {
// μμ μ²λ¦¬
}
}
Dart
볡μ¬





