简体   繁体   中英

How to substitute the path of an Image.asset with a variable which change with time?

I need to achieve this result: I open the app, there is one button, if I press the button in the first 10 seconds he open image.asset 1, if I don't press the button and wait those 10 seconds and then press it, within other 15 seconds, the path of that image.asset changes and the button open image.asset 2 and so on with other images (i wait also those 15 seconds and I press it opening image.asset 3). Any idea?

You can use DateTime.now().millisecondsSinceEpoch to track the time

Something like that:

//your widget
var _currentTime;
var imageAssetPath = 'assets/image1';

@override
void initState() {
   _currentTime = DateTime.now().millisecondsSinceEpoch;
   super.initState();
}

And onPressed of your button:

() {
   final timeDiff = DateTime.now().millisecondsSinceEpoch - _currentTime;
   if (timeDiff > 10000 && timeDiff < 25000){
      setState(() {
         imageAssetPath = 'assets/image2';
      });
   }
   else if (timeDiff > 25000 && timeDiff < 40000) {
      setState(() {
          imageAssetPath = 'assets/image3';
      });
   }
// ...

It is not very clean and short solution, but I hope you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM