![](/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.