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

阿贝德

我是 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'),
];

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS 7应用程序中的应用程序内首选项屏幕

来自分类Dev

如何使用OS X API查找应用程序首选项文件夹?

来自分类Dev

如何为10“以下的标签页定制应用程序首选项屏幕(两窗格布局)

来自分类Dev

使用segue时,在Cocoa应用程序中打开“首选项窗格”的无限窗口

来自分类Dev

如何打开特定的应用程序,如macOS`open -a`?

来自分类Dev

在启动时打开特定的网站

来自分类Dev

在启动应用程序首选项中添加应用程序的命令是什么?

来自分类Dev

在启动时设置setxkbmap首选项

来自分类Dev

Flutter-如何在共享首选项加载时暂停应用程序?

来自分类Dev

应用程序启动时如何打开抽屉?

来自分类Dev

如何在我的应用程序中使用首选项显示个人资料图片

来自分类Dev

我的设置活动使我的应用程序崩溃(使用共享首选项)

来自分类Dev

在颤抖中共享首选项停止应用程序

来自分类Dev

如何打开特定的应用程序,如macOS`open -a`?

来自分类Dev

如何在两个沙盒应用程序之间共享首选项文件?

来自分类Dev

启动应用程序首选项中的顺序

来自分类Dev

我如何仅通过xml从首选项屏幕中按程序包名称启动应用程序

来自分类Dev

使用共享首选项时,Android应用程序崩溃

来自分类Dev

如何删除Mac应用程序的首选项?

来自分类Dev

在启动时设置setxkbmap首选项

来自分类Dev

使用共享的首选项一次显示启动屏幕

来自分类Dev

如何从我的应用程序打开特定的Facebook页面?

来自分类Dev

启动应用程序首选项-SSH密钥代理

来自分类Dev

在不使用 android:allowBackup="false" 的情况下卸载应用程序后删除共享首选项

来自分类Dev

如何使我们的 android 应用程序更安全,目前我正在使用算法在共享首选项中存储数据,用户安全可以吗?

来自分类Dev

即使用户重新打开应用程序,我也可以使用共享首选项来检索我的数据吗?

来自分类Dev

我可以使用定制的共享首选项 XML 在启动时设置应用程序吗?

来自分类Dev

在启动应用程序首选项中添加应用程序的命令是什么?

来自分类Dev

我的应用程序尝试从非活动类访问共享首选项时被终止

Related 相关文章

  1. 1

    iOS 7应用程序中的应用程序内首选项屏幕

  2. 2

    如何使用OS X API查找应用程序首选项文件夹?

  3. 3

    如何为10“以下的标签页定制应用程序首选项屏幕(两窗格布局)

  4. 4

    使用segue时,在Cocoa应用程序中打开“首选项窗格”的无限窗口

  5. 5

    如何打开特定的应用程序,如macOS`open -a`?

  6. 6

    在启动时打开特定的网站

  7. 7

    在启动应用程序首选项中添加应用程序的命令是什么?

  8. 8

    在启动时设置setxkbmap首选项

  9. 9

    Flutter-如何在共享首选项加载时暂停应用程序?

  10. 10

    应用程序启动时如何打开抽屉?

  11. 11

    如何在我的应用程序中使用首选项显示个人资料图片

  12. 12

    我的设置活动使我的应用程序崩溃(使用共享首选项)

  13. 13

    在颤抖中共享首选项停止应用程序

  14. 14

    如何打开特定的应用程序,如macOS`open -a`?

  15. 15

    如何在两个沙盒应用程序之间共享首选项文件?

  16. 16

    启动应用程序首选项中的顺序

  17. 17

    我如何仅通过xml从首选项屏幕中按程序包名称启动应用程序

  18. 18

    使用共享首选项时,Android应用程序崩溃

  19. 19

    如何删除Mac应用程序的首选项?

  20. 20

    在启动时设置setxkbmap首选项

  21. 21

    使用共享的首选项一次显示启动屏幕

  22. 22

    如何从我的应用程序打开特定的Facebook页面?

  23. 23

    启动应用程序首选项-SSH密钥代理

  24. 24

    在不使用 android:allowBackup="false" 的情况下卸载应用程序后删除共享首选项

  25. 25

    如何使我们的 android 应用程序更安全,目前我正在使用算法在共享首选项中存储数据,用户安全可以吗?

  26. 26

    即使用户重新打开应用程序,我也可以使用共享首选项来检索我的数据吗?

  27. 27

    我可以使用定制的共享首选项 XML 在启动时设置应用程序吗?

  28. 28

    在启动应用程序首选项中添加应用程序的命令是什么?

  29. 29

    我的应用程序尝试从非活动类访问共享首选项时被终止

热门标签

归档