![](/img/trans.png)
[英]Blank screen randomly rendered when opening html file in app on iOS after closing app
[英]Remember screen state after closing and opening app
我的注册页面有 4 个页面,注册完成后有一个主页。 我需要跟踪屏幕,以便当用户离开和回来时:他会被带到他离开应用程序时所在的页面。 我见过几个使用SharedPreferences
的解决方案,但我使用的是 firebase/firestore 和Provider package
,所以我想我可以用它来保存 state。 我的注册流程是弹出/推送。 我的屏幕是Stateful Widgets
。 所以我可以使用initState
来保存 state。 只是不确定实现这一点的最佳方法......
我创建了一个代表注册页面状态的块,存储在其中的 state 可以在应用程序重新启动后继续存在,所有这一切,你拥有这些多汁的函数式编程东西,比如 Unions,永远不会忘记你拥有的任何情况,现在看看这段代码并尝试将其与在您的问题中保存用户进度(状态)的提供者方法进行比较,哪种方法会更好地创建更改通知器并用遍布各处的布尔值和字符串填充它们,或者使用数据结构和联合来表示 state 和让它不可变?
顺便说一句,我使用的解决方案是通过freezed和hydrad_bloc包实现的。
笔记:
我知道您很可能不会使用这种方法,正如我从您的评论中看到的那样,但无论如何这个答案是值得的,因为它可以帮助那些有时难以选择 state 管理解决方案的其他人
2.如果你觉得工会之类的东西很可怕,请不要,只是看看一些这样的教程,你对go很好
3.在你完全了解什么是冻结的 package 以及什么是 bloc 模式、什么是 union 以及所有这些如何工作之前,不要判断这个方法
这是解决方案的一个示例(解决方案的其他部分可以类似地完成):
注册状态.dart
//it contains the states as classes for each page,and each class holds the necessary fields to represent what's happening on that page
part of 'registration_bloc.dart';
@freezed
abstract class RegistrationState with _$RegistrationState {
const factory RegistrationState.firstPage({
//represents first page state
@required String email,
@required String password,
@required String phoneNumber,
}
//...
) = _FirstPage;
const factory RegistrationState.secondPage(//some params here
) = _SecondPage;
//same for other pages...
factory RegistrationState.initialState() => RegistrationState.firstPage(//consider initial state to be first page with empty strings
email: '',
password: '',
phoneNumber: '',
);
}
registraion_event.dart:
//represents all actions that might happen on each page and how you can update states accordingly
part of 'registration_bloc.dart';
@freezed
abstract class RegistrationEvent with _$RegistrationEvent {
const factory RegistrationEvent.emailChangedOnFirstPage() = _EmailChangedOnFirstPage;
const factory RegistrationEvent.passwordChangedOnFirstPage() = _PasswordChangedOnFirstPage;
const factory RegistrationEvent.phoneChangedOnFirstPage() = _PhoneChangedOnFirstPage;
const factory RegistrationEvent.submitPressedOnFirstPage() = _SubmitPressedOnFirstPage;
const factory RegistrationEvent.somethingHappenedOnSecondPage() = _somethingHappenedOnSecondPage;
//...
}
注册块.dart:
//place where you save state if app restarts and you turn evvents into states when they are fired from the UI
import 'dart:async';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
part 'registration_bloc.freezed.dart';
part 'registration_event.dart';
part 'registration_state.dart';
class RegistrationBloc
extends HydratedBloc<RegistrationEvent, RegistrationState> {
@override
RegistrationState get initialState =>
super.initialState ?? RegistrationState.initialState();
@override
Stream<RegistrationState> mapEventToState(
RegistrationEvent event,
) async* {
yield* event.map(emailChangedOnFirstPage: (event) async* {
//TODO yield some state
}, passwordChangedOnFirstPage: (event) async* {
//TODO yield some state
}, phoneChangedOnFirstPage: (event) async* {
//TODO yield some state
}, submitPressedOnFirstPage: (event) async* {
//TODO yield some state
}, somethingHappenedOnSecondPage: (event) async* {
//TODO yield some state
});
}
@override
RegistrationState fromJson(Map<String, dynamic> json) {
// TODO: get your state before app was killed last time
return null;
}
@override
Map<String, dynamic> toJson(RegistrationState state) {
// TODO: save your state as json
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.