Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redraw widget with Delay in Flutter

I want to remove a content from a Widget based on a conditional logic.

Below is my Widget tree,

return MaterialApp(
    home: Scaffold(
    body: Container(
    margin: EdgeInsets.all(120.0),
    child: Align(
      alignment: Alignment.center,
      child: Column(
        children: <Widget>[
          Text("Loading...");
        ],
      ),
    ),
  ),
));

Lets say I have a variable which fetches value from SharePreferences and if the value is "Hide" then Text widget encapsulated inside must be hidden. Otherwise it should display the text fetched from the SP.

The catch here is that the Text will initially display "Loading..." for a specific duration and then after that it must behave dynamically based on the condition.

I have tried with Timer and setState() but couldn't really make it work. Kindly assist if anyone has a solution for this.

like image 755
It Assistors Avatar asked Oct 28 '25 13:10

It Assistors


1 Answers

Jordan Davies is right. You need a FutureBuilder or StreamBuilder.

return MaterialApp(
        home: Scaffold(
      body: Container(
        margin: EdgeInsets.all(120.0),
        child: Center(
            child: FutureBuilder(
                future: Future.delayed(Duration(seconds: 3)),
                builder: (c, s) => s.connectionState == ConnectionState.done
                    ? Text("Loaded")
                    : Text("Loading..."))),
      ),
    ));

Just replace Future.delayed with your function.

like image 83
olexa.le Avatar answered Oct 30 '25 02:10

olexa.le



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!