Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Flutter
- android
- Dialog
- drift
- activity
- viewmodel
- appbar
- intent
- scroll
- 테스트
- data
- Compose
- Kotlin
- Navigation
- 안드로이드
- tabbar
- ScrollView
- CustomScrollView
- livedata
- textfield
- TEST
- Coroutines
- 앱바
- LifeCycle
- textview
- DART
- 앱
- Button
- binding
- 계측
Archives
- Today
- Total
Study Record
[Flutter] 뒤로 가기(back press) 버튼 컨트롤 (WillPopScope) 본문
728x90
🎁 WillPopScope Widget
안드로이드에서 뒤로 가기 버튼을 누르거나 system back button 이 실행되면 현재 화면이 종료되거나(Route Stack Pop) 앱이 종료된다. 이러한 액션이 실행되기 전에 콜백 이벤트를 받아 액션을 실행할지 말지 정할 수 있게 해주는 위젯이 WillPopScope 이다.
onWillPop 인자로 콜백 이벤트 인자를 받아 리턴값이 true 이면 액션을 그대로 실행하고 false 이면 액션을 실행하지 않는다.
WillPopScope(
onWillPop: () async {
return true;
},
child: Scaffold(
body: Center(child: Text("back press test")),
),
);
😶 간단한 예시)
뒤로 가기 버튼을 눌렀을 때 다이얼로그를 띄워 종료할지 안 할지 결정
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: MyApp(),
debugShowCheckedModeBanner: false,
));
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
return await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: Text('종료하시겠습니까?'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: Text('아니요'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text('네'),
),
],
),
) ?? false;
},
child: Scaffold(
body: Center(child: Text("back press test")),
),
);
}
}
WillPopScope class - widgets library - Dart API
Registers a callback to veto attempts by the user to dismiss the enclosing ModalRoute. Whenever the back button is pressed, you will get a callback at onWillPop, which returns a Future. If the Future returns true, the screen is popped. link To create a loc
api.flutter.dev
728x90
'Flutter' 카테고리의 다른 글
[Flutter] Debug 혹은 Release 인지 판단하는 방법 (0) | 2023.05.20 |
---|---|
[Flutter] 앱 이름 바꾸기 (app name) (0) | 2023.04.22 |
[Flutter] Dialog (다이얼로그), 모서리가 둥근 다이얼로그 (0) | 2023.04.20 |
[AndroidStudio] 자동 완성 설정하기 (Live Templates) (0) | 2023.04.14 |
[Flutter] Run start ms-settings:developers (0) | 2023.04.07 |