- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的注册有 4 个页面,注册完成后有一个主页。我需要跟踪屏幕,以便当用户离开和回来时:他被带到他离开应用程序时所在的页面。我见过几个使用 SharedPreferences
的解决方案,但我使用的是 firebase/firestore 和 Provider 包
,所以我想我可能会用它来保持状态。我的注册流程是弹出/推送。我的屏幕是 Stateful Widgets
。所以我可以使用 initState
来保持状态。只是不确定实现这个的最佳方法......
最佳答案
我制作了一个代表注册页面状态的 bloc,存储在其中的状态可以在应用程序重新启动后继续存在,所有这一切,而且你拥有这些多汁的函数式编程东西,比如 Unions,永远不会忘记你拥有的任何情况,现在看在此代码中并尝试将其与保存用户进度(状态)的提供者方法在您的问题中进行比较,哪种方法会更好地创建更改通知程序并用散布在周围的 bool 值和字符串填充它们或使用数据结构和联合来表示状态并使其不可变?
顺便说一下,我使用的解决方案是通过 freezed 实现的和 hydrated_bloc包。
注释:
我知道您很可能不会像我从您的评论中看到的那样使用这种方法,但无论如何这个答案是值得的,因为它可以帮助有时难以选择状态管理解决方案的其他人
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/
我观察到很少有逆向工程师,他们将十进制转换为十六进制的速度如此之快。这简直太神奇了。我一直没有机会问他们。就我个人而言,我真的很讨厌这个转换,而且我总是使用计算器进行转换。 我想知道这种转换是否有某种
我有一个程序使用三个 JTextField 字段作为主要数据输入字段。我想要它,以便当用户终止程序然后再次打开它时,他们的最后一个条目仍将在字段中。 我怎样才能做到这一点?我需要某种数据库还是有更简单
我有以下脚本将 jquery 生成的对象的颜色更改为蓝色: $(".objects_list").live('click', function(event) { $(this).css("co
这个问题在这里已经有了答案: Is it a good idea to memoize all of my react components? (2 个答案) 关闭去年。 我知道使用 React.m
我在 R 中编写了一个递归函数并使用 memoise 来加速它。我试图通过在 Rcpp 中编写它然后记住 Rcpp 函数来进一步加快它的速度,但 R 函数更快。为什么会这样,有什么方法可以加快我的使用
我的应用程序包含几个 View ,我想在应用程序启动且用户尚未完成向导时显示一个小设置。我知道我可以使用 NSUserDefaults 实现此目的,但我不确定如何使其根据 NSUserDefaults
我正在从一台机器(相同版本的Delphi)开发应用程序。该应用程序的原始版本使用了TMS包中的TMoneyEdit组件。在移动项目时,我想删除对该产品的依赖。因此,在源代码中,我删除了TMoneyEd
我有一个触发器,希望将相同的随机值插入两个表中。我该怎么做呢? 在TableAB上插入后创建触发器insertTrigger 开始 插入TableA(id,num)VALUES(RANDOM(),1)
我有以下代码,通过 .swf 解决方案将服务器 IP 复制到客户端的剪贴板。正如您所看到的,它用成功的“已复制”消息替换了“复制 IP”按钮。我该如何让“复制 IP”按钮在显示成功消息 5 秒后返回,
我正在使用 GPS 定位功能,问题是权限弹出窗口一遍又一遍地出现(每次新的网址刷新/按 F5 键)。 我如何记住用户在浏览器中选择的状态(已批准或已拒绝)。 if (navigator.geoloca
有一个按钮(实际上有很多),它有一个事件处理程序: el.onclick = function(){ if(this.className.indexOf("minimized") != -1)
我正在制作一个纯 html+JavaScript 幻灯片。幻灯片位于网站的侧边栏中,该网站为每个具有幻灯片侧边栏的页面加载了 php。唯一没有侧边栏的页面是主页。 幻灯片放映工作正常。然而,可以理解的
我想制作一个 Chrome 扩展程序,它将存储来自用户的潜在大型代码片段(以及代码片段的名称)并使用它们。 我希望用户能够上传包含这些片段的文件(或者更好的是,将这些片段复制并粘贴到扩展程序选项页面的
我有一个方法是 pure function并需要一段时间才能运行。我想记住这个方法,以便后续调用更快。我可以在 Groovy 的文档中看到,您可以通过以下方式内存闭包: foo = {…}.memoi
[jQuery][1] 始终记住您的鼠标触发器,无论是单击还是悬停。因此,如果您鼠标输入和鼠标退出四次,它将执行该事件四次。 如何让它“忘记”触发器,以便当我用鼠标输入和退出触发器时它只执行一次? 编
对于我的开发人员工作,我几乎整天都在 *nix shell 环境中工作,但似乎仍然无法记住我每天不使用的程序的名称和参数细节。我想知道其他“临时健忘症患者”是如何处理这个问题的。你有一个大的备忘单吗?
我有一个表格,在客户填写各种表格后,我希望能够浏览网站,并返回表格并保持填写状态。 我考虑过在浏览器的客户端中保留 cookie,您可能会推荐其他方法吗? 关于代码,这里是 o 想到的 javascr
好的,我已经实现了 Facebook 登录按钮: loginButton.readPermissions = ["public_profile", "email", "user_friends"] l
我在移动应用程序中使用 AngularJS。用户能够将产品添加到订单中。但是我使用了不同的选项卡,以便用户可以在类别之间切换,并且对于每个类别,都会显示属于该类别的产品。这些产品可以添加到他/她的订单
我有一个使用自定义适配器、布局和模型类的 ListView (包含文本和复选框)。我想将选定的复选框保存在 sqlite 数据库中,以便当我导航到另一个 Activity 然后返回时,选定的复选框保持
我是一名优秀的程序员,十分优秀!