반응형
- 코드 작성
import 'package:flutter/material.dart';
import 'dart:async';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late DateTime _lastPressedAt;
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
final now = DateTime.now();
if (now.difference(_lastPressedAt) > Duration(seconds: 2)) {
_lastPressedAt = now;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('한번 더 뒤로가기를 누를 시 종료됩니다'),
duration: Duration(seconds: 2),
),
);
return false;
}
return true;
},
child: Scaffold(
appBar: AppBar(
title: Text("Demo"),
),
body: Center(
child: Text(
'뒤로가기를 2초 안에 2번 누르면 종료됩니다',
style: TextStyle(fontSize: 20),
),
),
),
);
}
}
참고할만한 글
반응형