Home > Software engineering >  How to make ListView.builder build only 1 widget per second?
How to make ListView.builder build only 1 widget per second?

Time:08-24

Is it possible to make ListView.builder build only 1 widget per second?

Tried to use Future.delayed() and sleep() but its just don't do anything.

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ListView.builder(
        itemCount: 20,
        itemBuilder: (context, index) {
          Future.delayed(
            const Duration(seconds: 1),
          );
          return Text(index.toString());
        },
      ),
    );
  }

Thanks in advance

CodePudding user response:

Create a list and add items to the list using a timer

List<String> allWidget = [];
@override
 void initState(){
  startTimer();
}
@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ListView.builder(
        itemCount: allWidget.length,
        itemBuilder: (context, index) {
          return Text(allWidget[index].toString());
        },
      ),
    );
  }
startTimer(){
   Timer.periodic(Duration(seconds: 1), (timer){
      if(allWidget.length >= 20) {
        timer.cancel();
      } else{ 
        allWidget.addd("${allWidget.length}");
        setState((){});
      }
   }
}
  • Related