I have a login screen, after the user logs in he goes to the Working Screen, if the user is loged in and he leaves the app and comes back i made it so the home screen with be the Working Screen using shared prefs, but now i have another screen witch is the Stop Working Screen, and what im doing is that the user goes to this Working Screen and starts working, when he presses start working on the Working Screen it navigates him to the Stop Working Screen and there is a button to stop working witch will navigate him to the Working Screen. What i need right now is that if the user pressed start working on the Working Screen and left the app i want the home screen to be the Stop Working Screen instead of the Working Screen. So if the user is loged in and he is working(pressed start working button) than i want the Stop Working Screen to be the home screen. Any help would be highly appreciated. Also when the user presses Stop Working and exits the app it should send him to the Working Screen not the Stop Working Screen
// Start Working button
ElevatedButton(
child: loading
? const Loading()
: const Text(
'Start Working',
style: TextStyle(fontSize: 20),
),
style: ElevatedButton.styleFrom(
primary: Colors.teal),
onPressed: () async {
SharedPreferences prefs =
await SharedPreferences.getInstance();
// End Work Button
ElevatedButton(
onPressed: () {
putEndWork();
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const WorkingScreen(),
),
);
},
child: const Text(
'End Work',
// main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var authorization = prefs.getString('authorization');
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: authorization == null ? const LoginScreen() : const WorkingScreen(),
),
);
}
CodePudding user response:
The general idea of BJW's answer should work.
I would expect the part that redeclares var screenToShow to cause issues though:
var screenToShow = WorkingScreen();
if (authorization == null) {
var screenToShow = LoginScreen();
} else if (isWorking!) {
var screenToShow = StopWorkingScreen();
}
Try that same strategy with a helper like:
Widget getScreenFromIsWorking([String? isWorking]) {
if (isWorking != null) {
return StopWorkingScreen();
}
return WorkingScreen();
}
Called in main like:
var screenToShow = authorization == null ? LoginScreen() : getScreenFromIsWorking(isWorking);
CodePudding user response:
Add another variable to Shared Prefs, called it for example isWorking. isWorking is set to true when "Start working button" is pressed and it is set to false when "End working button" is pressed.
Your main method should also retrieve the isWorking variable inside Shared Prefs.
Then change main method to:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var authorization = prefs.getString('authorization');
bool? isWorking = prefs.getBool('isWorking');
var screenToShow = WorkingScreen();
if (authorization == null) {
var screenToShow = LoginScreen();
} else if (isWorking!) {
var screenToShow = StopWorkingScreen();
}
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: screenToShow,
),
);
}
