You can use the Future.delayed method to execute a function after a certain duration in Flutter. Here's an example of how to use it to change the style of a FlutterLogo widget after a delay:
import 'package:flutter/material.dart';
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
bool _isLogoStyleChanged = false;
@override
void initState() {
super.initState();
// Execute the _changeLogoStyle function after a delay of 2 seconds
Future.delayed(Duration(seconds: 2), () {
setState(() {
_isLogoStyleChanged = true;
});
});
}
@override
Widget build(BuildContext context) {
return Center(
child: _isLogoStyleChanged
? FlutterLogo(style: FlutterLogoStyle.horizontal)
: FlutterLogo(style: FlutterLogoStyle.markOnly),
);
}
}
In the above example, we set _isLogoStyleChanged to true after a delay of 2 seconds using Future.delayed. When _isLogoStyleChanged is true, we change the style of the FlutterLogo widget to FlutterLogoStyle.horizontal. Otherwise, we use the default style of FlutterLogoStyle.markOnly.
To know more, join our Flutter Training today.