How to run code after some delay in Flutter

0 votes

After my widget is generated, I want to have a function run after a specific amount of time. What is the proper Flutter idiom for this?

What I want to do is start with a default FlutterLogo Widget and then modify its style attribute after a certain amount of time.

Mar 10, 2023 in Android by sarit
• 1,830 points
1,809 views

1 answer to this question.

0 votes

You can use the Future.delayed function in Dart to run code after a specified delay. Here's an example of how to modify the style attribute of a FlutterLogo widget after a delay of 2 seconds:

import 'package:flutter/material.dart';

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  bool _isDelayed = false;

  @override
  void initState() {
    super.initState();

    // Start a delayed timer
    Future.delayed(Duration(seconds: 2), () {
      setState(() {
        _isDelayed = true;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: _isDelayed
          ? FlutterLogo(
              style: FlutterLogoStyle.stacked,
              size: 200.0,
            )
          : FlutterLogo(size: 200.0),
    );
  }
}

In this example, we're using the initState method to start a delayed timer using Future.delayed. After a delay of 2 seconds, we update the _isDelayed variable to true using setState. This triggers a rebuild of the widget, which displays a FlutterLogo widget with the style attribute set to FlutterLogoStyle.stacked. If _isDelayed is false, we display a FlutterLogo widget with the default style.

To know more about Flutter, join our Flutter Certification Course today.

answered Mar 18, 2023 by vishalini

Related Questions In Android

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,482 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,833 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
979 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
9,281 views
0 votes
1 answer

How to run code after some delay in Flutter?

You can use the Future.delayed method to ...READ MORE

answered Mar 10, 2023 in Android by vinayak
524 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,224 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
794 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,846 views
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
7,413 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