How to run code after some delay in Flutter

0 votes

I'd like to execute a function after a certain delay after my Widget is built. What's the idiomatic way of doing this in Flutter?

What I'm trying to achieve: I'd like to start with a default FlutterLogo Widget and then change its style property after some duration.

Mar 9, 2023 in Android by sarit
• 1,830 points
1,125 views

1 answer to this question.

0 votes

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.

answered Mar 10, 2023 by vinayak

Related Questions In Android

0 votes
1 answer

How can I add a border to a widget in Flutter?

To add a border to a widget ...READ MORE

answered Mar 1, 2023 in Android by vishalini
12,790 views
0 votes
1 answer

How to change package name in flutter?

Yes, you can change the package name ...READ MORE

answered Mar 1, 2023 in Android by vishalini
3,581 views
0 votes
1 answer

How to create number input field in Flutter?

Yes, Flutter has a built-in widget called ...READ MORE

answered Mar 1, 2023 in Android by vishalini
9,064 views
0 votes
1 answer

How to create border radius of elevatedbutton in flutter

To create a rounded button with border-radius ...READ MORE

answered Mar 1, 2023 in Android by vishalini
33,804 views
0 votes
1 answer

How to run code after some delay in Flutter?

You can use the Future.delayed function in ...READ MORE

answered Mar 18, 2023 in Android by vishalini
2,871 views
0 votes
1 answer
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
1,327 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
4,266 views
0 votes
1 answer

How to add a ListView to a Column in Flutter?

To add a ListView to a Column ...READ MORE

answered Mar 9, 2023 in Android by pooja
17,591 views
0 votes
2 answers

How to change the application launcher icon on Flutter? Ask Question

how to change the app icon in ...READ MORE

answered Nov 29, 2023 in Android by anonymous
10,634 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP