有没有办法在 provider class 中的特定变量发生变化时触发页面导航?
Is there a way to trigger the page navigation when a specific variable in provider class changes?
有没有办法在 provider class 中的特定变量发生变化时触发页面导航?
例如。
我想在 appLock
变量变为真时导航到登录屏幕,否则什么也不做
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
runApp(MultiProvider(providers: [
ChangeNotifierProvider(create: (_) => AppLockHelper()),
], child: const MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
context.read<AppLockHelper>().startThread();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
scaffoldBackgroundColor: kWhiteColor,
),
supportedLocales: L10n.all,
initialRoute: '/',
onGenerateRoute: RouteGenerator.generateRoute,
);
}
}
这是我的供应商class
class AppLockHelper extends ChangeNotifier {
bool _appLocked = false;
bool get appLocked => _appLocked;
final _get = GenericGetApi();
Timer? timer;
void startThread() {
timer = Timer.periodic(
const Duration(seconds: 15), (Timer t) => getAppStatus());
}
void stopThread() {
timer?.cancel();
}
Future<void> getAppStatus() async {
var appStatusResult =
await _get.get(endpoint: EndPointsPool.getAppLockStatus);
appStatusResult.fold((l) {
_appLocked = false;
}, (r) {
AppLock appLockResult = AppLock.fromJson(r);
_appLocked = appLockResult.applocked;
});
notifyListeners();
}
}
好的,解决方案是在 initState 中添加一个侦听器。
@override
void initState() {
super.initState();
final myNotifier = context.read<AppLockHelper>();
void listener() {
myNotifier.appLocked
? Navigator.pushNamedAndRemoveUntil(context, "/log-in", (_) => false)
: null;
}
myNotifier.addListener(listener);
}
有没有办法在 provider class 中的特定变量发生变化时触发页面导航?
例如。
我想在 appLock
变量变为真时导航到登录屏幕,否则什么也不做
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
runApp(MultiProvider(providers: [
ChangeNotifierProvider(create: (_) => AppLockHelper()),
], child: const MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
context.read<AppLockHelper>().startThread();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
scaffoldBackgroundColor: kWhiteColor,
),
supportedLocales: L10n.all,
initialRoute: '/',
onGenerateRoute: RouteGenerator.generateRoute,
);
}
}
这是我的供应商class
class AppLockHelper extends ChangeNotifier {
bool _appLocked = false;
bool get appLocked => _appLocked;
final _get = GenericGetApi();
Timer? timer;
void startThread() {
timer = Timer.periodic(
const Duration(seconds: 15), (Timer t) => getAppStatus());
}
void stopThread() {
timer?.cancel();
}
Future<void> getAppStatus() async {
var appStatusResult =
await _get.get(endpoint: EndPointsPool.getAppLockStatus);
appStatusResult.fold((l) {
_appLocked = false;
}, (r) {
AppLock appLockResult = AppLock.fromJson(r);
_appLocked = appLockResult.applocked;
});
notifyListeners();
}
}
好的,解决方案是在 initState 中添加一个侦听器。
@override
void initState() {
super.initState();
final myNotifier = context.read<AppLockHelper>();
void listener() {
myNotifier.appLocked
? Navigator.pushNamedAndRemoveUntil(context, "/log-in", (_) => false)
: null;
}
myNotifier.addListener(listener);
}