我有一个包含敏感医疗信息的Flutter应用程序,企业希望在将应用程序放入后台时隐藏此信息,包括“最近的应用程序”屏幕。
我添加了WidgetsBindingObserver并且正在正确地监听事件。恢复状态正确触发并将用户返回登录页面;但是,暂停事件在收到状态时不会执行任何操作。作为参考,我已经尝试将新屏幕推入堆栈以及弹出所有屏幕直到登录但是都不起作用。
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.paused:
Navigator.of(context).push(new PageRouteBuilder(
pageBuilder: (_, __, ___) => new Splash(
inBackground: true,
),
));
break;
case AppLifecycleState.resumed:
Navigator.of(context).pushNamed('/login');
break;
default:
break;
}
}
我希望当收到暂停的事件时能够更改屏幕以保护此敏感信息。欢迎任何想法!
编辑:最新的代码。
import 'package:boxview_mobile_flutter/screens/splash/index.dart';
import 'package:boxview_mobile_flutter/services/shared_prefs.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
class PushNotifications extends StatefulWidget {
@override
_PushNotificationsState createState() => _PushNotificationsState();
}
class _PushNotificationsState extends State<PushNotifications> with WidgetsBindingObserver {
bool loggedOut = false;
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
Widget build(BuildContext context) {
return Container(child: Splash(loggedOut: this.loggedOut));
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.paused:
print("Paused");
break;
case AppLifecycleState.inactive:
print("inactive");
setState(() {
loggedOut = true;
});
break;
case AppLifecycleState.suspending:
print("suspending");
break;
case AppLifecycleState.resumed:
setState(() {
loggedOut = false;
});
print("resumed");
break;
}
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
_firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
SharedPreferencesHelper.setFirebaseToken(token);
});
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
}
Splash只是一个启动页面,而boolean loggedOut param只是说不要转发到登录页面。
别忘了将with WidgetsBindingObserver
添加到你的州级
class YourClass extends StatefulWidget { ....
class _YourClassState extends State<BottomNavigator> with WidgetsBindingObserver { ...
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
AppLifecycleState _notification;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.paused:
print("Paused");
setState(() {
_notification = state;
});
break;
case AppLifecycleState.inactive:
setState(() {
_notification = state;
});
print("inactive");
break;
case AppLifecycleState.suspending:
setState(() {
_notification = state;
});
print("suspending");
break;
case AppLifecycleState.resumed:
setState(() {
_notification = state;
});
print("resumed");
break;
}
}
并使用_notification
更改您的应用状态
例如
@override
Widget build(BuildContext context) {
return _notification == AppLifecycleState.inactive
? Scaffold(
body: Text("inactive"),
)
: YourRealWidget()
你可以查看qazxsw poi的更多信息