|
1 | 1 | import 'dart:convert';
|
| 2 | +import 'dart:io'; |
2 | 3 |
|
3 | 4 | import 'package:collection/collection.dart';
|
4 | 5 | import 'package:equatable/equatable.dart';
|
@@ -834,6 +835,7 @@ class ViewControl extends StatefulWidget {
|
834 | 835 |
|
835 | 836 | class _ViewControlState extends State<ViewControl> with FletStoreMixin {
|
836 | 837 | final scaffoldKey = GlobalKey<ScaffoldState>();
|
| 838 | + DateTime ? currentBackPressTime; |
837 | 839 |
|
838 | 840 | @override
|
839 | 841 | Widget build(BuildContext context) {
|
@@ -1063,49 +1065,66 @@ class _ViewControlState extends State<ViewControl> with FletStoreMixin {
|
1063 | 1065 | ? parseTheme(widget.parent, "darkTheme", Brightness.dark)
|
1064 | 1066 | : parseTheme(widget.parent, "theme", Brightness.dark);
|
1065 | 1067 |
|
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(); |
1098 | 1084 | },
|
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 | + ) |
1109 | 1128 | );
|
1110 | 1129 |
|
1111 | 1130 | var systemOverlayStyle =
|
|
0 commit comments