Skip to content

Commit 5c12b34

Browse files
committed
Flet Predictive Back Support Google TV
1 parent 6a04a97 commit 5c12b34

File tree

1 file changed

+61
-42
lines changed

1 file changed

+61
-42
lines changed

packages/flet/lib/src/controls/page.dart

+61-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:convert';
2+
import 'dart:io';
23

34
import 'package:collection/collection.dart';
45
import 'package:equatable/equatable.dart';
@@ -834,6 +835,7 @@ class ViewControl extends StatefulWidget {
834835

835836
class _ViewControlState extends State<ViewControl> with FletStoreMixin {
836837
final scaffoldKey = GlobalKey<ScaffoldState>();
838+
DateTime ? currentBackPressTime;
837839

838840
@override
839841
Widget build(BuildContext context) {
@@ -1063,49 +1065,66 @@ class _ViewControlState extends State<ViewControl> with FletStoreMixin {
10631065
? parseTheme(widget.parent, "darkTheme", Brightness.dark)
10641066
: parseTheme(widget.parent, "theme", Brightness.dark);
10651067

1066-
Widget scaffold = Scaffold(
1067-
key: bar == null || bar is AppBarControl ? scaffoldKey : null,
1068-
backgroundColor: control.attrColor("bgcolor", context) ??
1069-
CupertinoTheme.of(context).scaffoldBackgroundColor,
1070-
appBar: bar is AppBarControl ? bar : null,
1071-
drawer: drawerView != null
1072-
? NavigationDrawerControl(
1073-
control: drawerView.control,
1074-
children: drawerView.children,
1075-
parentDisabled: control.isDisabled,
1076-
parentAdaptive: adaptive,
1077-
backend: widget.backend)
1078-
: null,
1079-
onDrawerChanged: (opened) {
1080-
if (drawerView != null && !opened) {
1081-
widget.parent.state["drawerOpened"] = false;
1082-
dismissDrawer(drawerView.control.id);
1083-
}
1084-
},
1085-
endDrawer: endDrawerView != null
1086-
? NavigationDrawerControl(
1087-
control: endDrawerView.control,
1088-
children: endDrawerView.children,
1089-
parentDisabled: control.isDisabled,
1090-
parentAdaptive: adaptive,
1091-
backend: widget.backend)
1092-
: null,
1093-
onEndDrawerChanged: (opened) {
1094-
if (endDrawerView != null && !opened) {
1095-
widget.parent.state["endDrawerOpened"] = false;
1096-
dismissDrawer(endDrawerView.control.id);
1097-
}
1068+
// For PopScope - https://docs.flutter.dev/release/breaking-changes/android-predictive-back
1069+
// We need to wrap Scaffold as the child -- this helps GOOGLE TV exit flet app properly
1070+
Future<bool> _allowBackPress() async {
1071+
DateTime now = DateTime.now();
1072+
if (currentBackPressTime == null || now.difference(currentBackPressTime !) > Duration(seconds : 2)) {
1073+
currentBackPressTime = now;
1074+
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Press again to exit")));
1075+
return false;
1076+
}
1077+
exit(0); // Back button hit twice exit program
1078+
}
1079+
1080+
Widget scaffold = PopScope(
1081+
canPop: false,
1082+
onPopInvokedWithResult: (didPop, result) async {
1083+
await _allowBackPress();
10981084
},
1099-
body: body,
1100-
bottomNavigationBar: bnb != null
1101-
? createControl(control, bnb.id, control.isDisabled,
1102-
parentAdaptive: adaptive)
1103-
: null,
1104-
floatingActionButton: fab != null
1105-
? createControl(control, fab.id, control.isDisabled,
1106-
parentAdaptive: adaptive)
1107-
: null,
1108-
floatingActionButtonLocation: fabLocation,
1085+
child: Scaffold(
1086+
key: bar == null || bar is AppBarControl ? scaffoldKey : null,
1087+
backgroundColor: control.attrColor("bgcolor", context) ?? CupertinoTheme.of(context).scaffoldBackgroundColor,
1088+
appBar: bar is AppBarControl ? bar : null,
1089+
drawer: drawerView != null
1090+
? NavigationDrawerControl(
1091+
control: drawerView.control,
1092+
children: drawerView.children,
1093+
parentDisabled: control.isDisabled,
1094+
parentAdaptive: adaptive,
1095+
backend: widget.backend)
1096+
: null,
1097+
onDrawerChanged: (opened) {
1098+
if (drawerView != null && !opened) {
1099+
widget.parent.state["drawerOpened"] = false;
1100+
dismissDrawer(drawerView.control.id);
1101+
}
1102+
},
1103+
endDrawer: endDrawerView != null
1104+
? NavigationDrawerControl(
1105+
control: endDrawerView.control,
1106+
children: endDrawerView.children,
1107+
parentDisabled: control.isDisabled,
1108+
parentAdaptive: adaptive,
1109+
backend: widget.backend)
1110+
: null,
1111+
onEndDrawerChanged: (opened) {
1112+
if (endDrawerView != null && !opened) {
1113+
widget.parent.state["endDrawerOpened"] = false;
1114+
dismissDrawer(endDrawerView.control.id);
1115+
}
1116+
},
1117+
body: body,
1118+
bottomNavigationBar: bnb != null
1119+
? createControl(control, bnb.id, control.isDisabled,
1120+
parentAdaptive: adaptive)
1121+
: null,
1122+
floatingActionButton: fab != null
1123+
? createControl(control, fab.id, control.isDisabled,
1124+
parentAdaptive: adaptive)
1125+
: null,
1126+
floatingActionButtonLocation: fabLocation,
1127+
)
11091128
);
11101129

11111130
var systemOverlayStyle =

0 commit comments

Comments
 (0)