꿈꾸는 시스템 디자이너

Flutter Example - WillPopScope | Handel back button pressed 본문

Tutorial/Flutter with App

Flutter Example - WillPopScope | Handel back button pressed

독행소년 2019. 9. 27. 10:13
import 'package:flutter/material.dart';

class WillPopScopeDemo extends StatelessWidget {
  DateTime currentBackPressTime;

  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        bool result = onPressBackButton();
        return await Future.value(result);
      },
      child: Scaffold(
        key: scaffoldKey,
        appBar: AppBar(title: Text("WillPopScope Demo")),
        body: Center(
          child: Text("Tap back button to leave this page"),
        ),
      ),
    );
  }

  bool onPressBackButton() {
    DateTime now = DateTime.now();
    if (currentBackPressTime == null ||
        now.difference(currentBackPressTime) > Duration(seconds: 2)) {
      currentBackPressTime = now;
      scaffoldKey.currentState
        ..hideCurrentSnackBar()
        ..showSnackBar(SnackBar(
          content: Text("Tap back again to leave."),
        ));
      return false;
    }
    return true;
  }
}

 

 

▶ Go to Table of Contents | 강의 목차로 이동


※ This example is also available in the Flutter Code Examples app. | 본 예제는 Flutter Code Examples 앱에서도 제공됩니다.

 

Flutter Code Examples - Google Play 앱

Are you a beginner at Flutter? Check out the various features of Flutter through the demo. Source code for all demos is also provided.

play.google.com

Comments