I have a start and stop button and a current status which is either ready or not ready. The behaviour I want is that:
The status is not ready when the start button is pressed and only becomes ready 4 seconds after the stop button is pressed. If in those 4 seconds the start button is pressed I want the status to become "not ready" right after the stop function set it to ready (so essentially the start function should wait for the stop function to be completed).
Here is the sample app with which I've tried to solve this problem:
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: HomePage(), ); } } class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { @override Widget build(BuildContext context) { return Scaffold( body: Column( crossAxisAlignment: CrossAxisAlignment.stretch, mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( isCurrentlyReady ? "ready" : "not ready", textAlign: TextAlign.center, ), if (canPressStart) CupertinoButton( child: Text('Start'), onPressed: start, ) else CupertinoButton( child: Text('Stop'), onPressed: stop, ) ], ), ); } bool isCurrentlyReady = true; bool canPressStart = true; Future<bool> isReadyFuture = Future.value(true); start() async { setState(() { canPressStart = false; }); await isReadyFuture; setState(() { isCurrentlyReady = false; }); //this future will never come isReadyFuture = Future.delayed(Duration(days: 90000), () => true); } stop() async { setState(() { canPressStart = true; }); await Future.delayed(Duration(seconds: 4)); setState(() { isCurrentlyReady = true; }); isReadyFuture = Future.value(true); } } How can I achieve the desired behaviour?