Flutter_끄적끄적

[Flutter]플러터 (앱 종료 함수) |안드로이드, IOS

Ksr 2021. 11. 27. 12:26
반응형
  • 사용된 패키지
    import 'package:flutter/services.dart';
  • 사용 코드
    appBar: AppBar(
      backgroundColor: Color(0xff161619),
      toolbarHeight: 28,
      //앱 종료 버튼 따로 만들기
      leading: IconButton(
        icon: Icon(CupertinoIcons.power,size:20,),
        onPressed: () {
          _onPowerKey();
        },
      ),
  • 사용 코드(SystemChannel)
    //앱 내에서 종료 버튼 눌렀을때,
    Future _onPowerKey() async {
      return await showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              backgroundColor: Color(0xff161619),
              title: Text(
                '끝내시겠습니까?',
                style: TextStyle(color: Colors.white),
              ),
              actions: [
                TextButton(
                    onPressed: () {
                    //아래 함수를 이용해서 앱을 종료 할 수 있다.
                      SystemChannels.platform.invokeMethod('SystemNavigator.pop');
                    },
                    child: Text('끝내기')),
                TextButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    child: Text('아니요')),
              ],
            );
          });
    }
반응형