在关闭和打开应用后记住屏幕状态

时间:2020-04-20 19:16:25

标签: flutter

我的注册中有4页,注册完成后有一个主页。我需要跟踪屏幕,以便当用户离开并返回时:他被带到离开应用程序时所在的页面。我已经看到了一些使用SharedPreferences的解决方案,但是我正在使用firebase / firestore和Provider package,所以我想我可以用它来保持状态。我的注册流程为pop / push。我的屏幕是Stateful Widgets。因此,我可以使用initState来保持状态。只是不确定实现此目标的最佳方法...

1 个答案:

答案 0 :(得分:0)

我创建了一个表示注册页面状态的块,并且存储在其中的状态可以在应用程序重新启动后幸存下来,所有这些都使您拥有了多汁的功能编程之类的东西,例如Union,永远不会忘记您遇到的任何情况,现在来看将此代码尝试与保存您问题中用户进度(状态)的提供程序方法进行比较,哪种方法会更好地创建更改通知程序,并用散布在各处的布尔值和字符串或使用数据结构和联合来表示它们状态并使它不可侵犯?

freezedhydrated_bloc软件包使我使用的解决方案成为可能。

注释:

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

    2。如果您发现像工会这样的东西令人恐惧,请不要这样做,只需看一下this之类的教程,您就可以开始学习

    3。在完全了解什么是冻结包,什么是块模式以及什么是联合以及所有这些如何工作之前,请不要判断这个方法。

这是解决方案的样本(解决方案的其他部分也可以类似地完成):

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