Flutter_끄적끄적

[Flutter]플러터 Back Key(빽키) 컨트롤하기 |안드로이드, IOS

Ksr 2021. 11. 27. 12:22
반응형
  • 사용된 패키지
    import 'package:flutter/services.dart';
  • 사용 코드(WillPopScope 위젯)
    @override
    Widget build(BuildContext context) {
      _firstMainScreenProvider = Provider.of<FirstMainScreenProvider>(context);
      //WillPopScope는 사용자가 빽키를 눌렀을때 작동되는 위젯
      return WillPopScope(
        onWillPop: () {
          return _onBackKey();
        },
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBar(
  • 사용 코드(WillPopScope 에서 사용된 함수)
    //휴대폰 피지컬 버튼 뒤로 가기 눌렀을때,
    Future<bool> _onBackKey() async {
      return await showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              backgroundColor: Color(0xff161619),
              title: Text(
                '끝내시겠습니까?',
                style: TextStyle(color: Colors.white),
              ),
              actions: [
                TextButton(
                    onPressed: () {
                    //onWillpop에 true가 전달되어 앱이 종료 된다.
                      Navigator.pop(context, true);
                    },
                    child: Text('끝내기')),
                TextButton(
                    onPressed: () {
                    //onWillpop에 false 전달되어 앱이 종료되지 않는다.
                      Navigator.pop(context, false);
                    },
                    child: Text('아니요')),
              ],
            );
          });
    }
반응형