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
494 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
6,735 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
2,289 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
3,205 views
0 votes
1 answer

How to do Rounded Corners Image in Flutter?

To make the image in your Flutter ...READ MORE

answered Mar 1, 2023 in Android by vishalini
901 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
1,707 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,440 points
3,189 views
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,440 points
762 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,440 points
3,823 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
8,711 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
9,367 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