Random Image path refreshes everytime when I click on a checkbox

I generated the following code to random load a background url for the image widget. This is working when refreshing the app but it also calls this fuction everytime I click on a checkbox. I think this has to do with Flutter refreshing the page when a change happens like checking a checkbox. How can I stop this from happening? Is this because of stateful and stateless widgets?

String randomBackground() {

List<String> backgrounds = [
'https://images.unsplash.com/photo-1521747116042-5a810fda9664',
'https://images.unsplash.com/photo-1519681393784-d120267933ba',
'https://images.unsplash.com/photo-1519681393784-d120267933ba',
];
final random = math.Random();
final index = random.nextInt(backgrounds.length);
return backgrounds[index];
}

1
2 replies