I'm new in flutter and i'm not an experienced developer. I'm building an app with an intro slider, a login screen and a sign up screen.
My question is how to know which screen the user was on before closing the app, to then use shared preference to open that screen when user launch the app again? And also display the data when the screen is the sign up screen.
I had a piece of testing code laying around that was similar to your question.
In general when a user switches out of your program to another program your program does not actually exit. It just is in the background, ready to be raised to the top again.
But, if the user explicitly kills the program then the initState method will be called and the saved fruit state, in this instance, will be retrieved.
You will note in the Build method the last fruit saved will be displayed.
Using this code as a template, I think it should compile and run as it is, you could modify it for your situation.
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'),
];