再次启动应用程序时如何使用共享首选项打开特定屏幕

How to use shared preferences to open a specific screen when app lauch again

我是 flutter 的新手,不是经验丰富的开发人员。我正在构建一个带有介绍滑块、登录屏幕和注册屏幕的应用程序。

我的问题是如何在关闭应用程序之前知道用户在哪个屏幕上,然后在用户再次启动应用程序时使用共享首选项打开该屏幕?并在屏幕为注册屏幕时显示数据。

我有一段测试代码与你的问题相似。

一般来说,当用户从您的程序切换到另一个程序时,您的程序实际上并没有退出。它只是在后台,准备再次被提升到顶部。

但是,如果用户明确终止程序,则会调用 initState 方法并检索保存的水果状态,在这种情况下,将被检索。

您会注意到在 Build 方法中将显示最后保存的水果。

使用此代码作为模板,我认为它应该编译并且运行按原样,您可以根据您的情况修改它。

import 'package:flutter/material.dart';
import 'dart:io';

import 'package:shared_preferences/shared_preferences.dart';

class TestPage extends StatefulWidget {
  TestPage({Key key}) : super(key: key);
  @override
  TestPageState createState() => TestPageState();
}

class TestPageState extends State<TestPage> {
  TestPageState() {}

  int _lastFruitSelected = 0;

  @override
  void initState() {
    super.initState();

    getLastFruit().then((_fruitResult) {
      if (_fruitResult != null) {
        setState(() {
          _lastFruitSelected = _fruitResult;
        });
      }
    });
  }

  static Future<int> getLastFruit() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    if (prefs != null) {
      int fruit = await prefs.getInt("LAST_FRUIT");
      if (fruit == null) {
        await prefs.setInt("LAST_FRUIT", FruitChoice.APPLE);
        return (FruitChoice.APPLE);
      } else {
        return (fruit);
      }
    }
    return (FruitChoice.APPLE);
  }

  static Future<int> setLastFruit(int fruit) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    if (prefs != null) {
      await prefs.setInt("LAST_FRUIT", fruit).then((onValue) {
        return (fruit);
      });
    }
  }

  Future<void> _fruitSelect(FruitChoice choice_of_fruit) async {
    ///////////////////////////
    // How to pass in the dish to this function?
    ///////////////////////////

    String dish = "????";

    print("Fruit: " +
        choice_of_fruit.fruit_name +
        " for Dish: " +
        choice_of_fruit.dish);

    if (choice_of_fruit.fruit_name == "Apple") {
      await setLastFruit(FruitChoice.APPLE).then((result_if_any) {
        getLastFruit().then((fruit) {
          setState(() {
            print("result=" + fruit.toString());
            _lastFruitSelected = fruit;
          });
        });
      });
    } else if (choice_of_fruit.fruit_name == "Orange") {
      await setLastFruit(FruitChoice.ORANGE).then((result_if_any) {
        getLastFruit().then((fruit) {
          setState(() {
            print("result=" + fruit.toString());
            _lastFruitSelected = fruit;
          });
        });
        ;
      });
    } else if (choice_of_fruit.fruit_name == "Kiwi") {
      await setLastFruit(FruitChoice.KIWI).then((result_if_any) {
        getLastFruit().then((fruit) {
          setState(() {
            print("result=" + fruit.toString());
            _lastFruitSelected = fruit;
          });
        });
      });
    }
  }

  // 40,520 detainees

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text("Assign fruit to dish:"),
        ),
        body: Column(children: <Widget>[
          _lastFruitSelected == FruitChoice.APPLE
              ? Text("Current fruit choice Apple")
              : _lastFruitSelected == FruitChoice.ORANGE
                  ? Text("Current fruit choice Orange")
                  : Text("Current fruit choice Kiwi"),
          ListTile(
              title: new Text("Dish #1"),
              subtitle: PopupMenuButton<FruitChoice>(
                icon: new Icon(Icons.assignment),
                onSelected: _fruitSelect,
                itemBuilder: (BuildContext context) {
                  return fruit_choices.map((FruitChoice choice) {
                    return PopupMenuItem<FruitChoice>(
                      value: choice.setDish("d1"),
                      child: ListTile(title: Text(choice.fruit_name)),
                    );
                  }).toList();
                },
              ))
        ]));
  }
}

class FruitChoice {
  static final int APPLE = 0;
  static final int ORANGE = 1;
  static final int KIWI = 2;

  FruitChoice({this.fruit_name});

  String fruit_name;

  String dish = "";

  FruitChoice setDish(String adish) {
    dish = adish;
    return (this);
  }
}

List<FruitChoice> fruit_choices = <FruitChoice>[
  FruitChoice(fruit_name: 'Apple'),
  FruitChoice(fruit_name: 'Orange'),
  FruitChoice(fruit_name: 'Kiwi'),
];
  1. 将所有信息存储在共享首选项中。例如。 (登录)a.user_id、b.password 注册一个。 user_id, b.电子邮件,c.password, 和 current_screen(login/signup/incomplete)
  2. 更新 current_screen 表单提交值。
  3. 启动应用程序之前,从共享首选项中检索数据。
  4. 如果 current_screen 值不完整重定向到 signup.If 值是 signup 然后重定向到登录页面 如果值是 login 然后重定向到登录页面 user_id 和密码显示在页面并等待表单提交。