gpt4 book ai didi

flutter - 关闭和打开应用程序后记住屏幕状态

转载 作者:行者123 更新时间:2023-12-04 17:29:04 24 4
gpt4 key购买 nike

我的注册有 4 个页面,注册完成后有一个主页。我需要跟踪屏幕,以便当用户离开和回来时:他被带到他离开应用程序时所在的页面。我见过几个使用 SharedPreferences 的解决方案,但我使用的是 firebase/firestore 和 Provider 包,所以我想我可能会用它来保持状态。我的注册流程是弹出/推送。我的屏幕是 Stateful Widgets。所以我可以使用 initState 来保持状态。只是不确定实现这个的最佳方法......

最佳答案

我制作了一个代表注册页面状态的 bloc,存储在其中的状态可以在应用程序重新启动后继续存在,所有这一切,而且你拥有这些多汁的函数式编程东西,比如 Unions,永远不会忘记你拥有的任何情况,现在看在此代码中并尝试将其与保存用户进度(状态)的提供者方法在您的问题中进行比较,哪种方法会更好地创建更改通知程序并用散布在周围的 bool 值和字符串填充它们或使用数据结构和联合来表示状态并使其不可变?

顺便说一下,我使用的解决方案是通过 freezed 实现的和 hydrated_bloc包。

注释:

  1. 我知道您很可能不会像我从您的评论中看到的那样使用这种方法,但无论如何这个答案是值得的,因为它可以帮助有时难以选择状态管理解决方案的其他人

    2.如果你觉得像union这样的东西很可怕,请不要,只看一些像this这样的教程。你很高兴去

    3.在你完全了解什么是卡住包,什么是 block 模式,什么是联合以及这些是如何工作之前,不要判断这个方法

这是一个解决方案的示例(解决方案的其他部分可以类似地完成):

registration_state.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;
//...
}

registarion_bloc.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;
}
}

关于flutter - 关闭和打开应用程序后记住屏幕状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61330278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com