cake_wallet/lib/src/screens/root/root.dart

84 lines
2 KiB
Dart
Raw Normal View History

2020-01-04 19:31:52 +00:00
import 'package:flutter/material.dart';
import 'package:cake_wallet/routes.dart';
2020-01-08 12:26:34 +00:00
import 'package:cake_wallet/src/screens/auth/auth_page.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/store/authentication_store.dart';
import 'package:cake_wallet/entities/qr_scanner.dart';
2020-01-04 19:31:52 +00:00
class Root extends StatefulWidget {
2020-09-29 17:56:11 +00:00
Root(
{Key key,
this.authenticationStore,
this.appStore,
this.child,
this.navigatorKey})
2020-09-14 12:08:33 +00:00
: super(key: key);
2020-06-20 07:10:00 +00:00
final AuthenticationStore authenticationStore;
final AppStore appStore;
2020-09-29 17:56:11 +00:00
final GlobalKey<NavigatorState> navigatorKey;
2020-09-14 12:08:33 +00:00
final Widget child;
2020-01-04 19:31:52 +00:00
@override
RootState createState() => RootState();
}
class RootState extends State<Root> with WidgetsBindingObserver {
bool _isInactive;
bool _postFrameCallback;
@override
void initState() {
_isInactive = false;
_postFrameCallback = false;
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.paused:
if (isQrScannerShown) {
return;
}
2020-09-21 11:50:26 +00:00
if (!_isInactive &&
widget.authenticationStore.state == AuthenticationState.allowed) {
setState(() => _isInactive = true);
}
2020-01-04 19:31:52 +00:00
break;
default:
break;
}
}
@override
Widget build(BuildContext context) {
2020-09-21 11:50:26 +00:00
if (_isInactive && !_postFrameCallback) {
_postFrameCallback = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
2020-09-29 17:56:11 +00:00
widget.navigatorKey.currentState.pushNamed(Routes.unlock,
2020-09-21 11:50:26 +00:00
arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) {
if (!isAuthenticatedSuccessfully) {
return;
}
_reset();
auth.close();
});
});
}
2020-06-20 07:10:00 +00:00
2020-09-28 19:02:30 +00:00
return WillPopScope(onWillPop: () async => false, child: widget.child);
2020-09-21 11:50:26 +00:00
}
2020-01-04 19:31:52 +00:00
2020-09-21 11:50:26 +00:00
void _reset() {
setState(() {
_postFrameCallback = false;
_isInactive = false;
});
2020-01-04 19:31:52 +00:00
}
}