first you need a timer to make a periodical update to your layout:
Timer? timer;
void initState() {
super.initState();
timer =
Timer.periodic(Duration(seconds: 1), (Timer t) => updatePeriodically());
}
@override
void dispose() {
timer?.cancel();
super.dispose();
}
then you need to assign a new value to your list builder every time your periodical function is called:
void updatePeriodically() {
setState(() {
itemsCount += 1;
});
}
the full example code:
class Home extends StatefulWidget {
@override
HomeState createState() => HomeState();
}
class HomeState extends State<Home> {
@override
Timer? timer;
void initState() {
super.initState();
timer =
Timer.periodic(Duration(seconds: 1), (Timer t) => updatePeriodically());
}
int itemsCount = 3;
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: itemsCount,
itemBuilder: (context, index) {
return ListTile(
leading: Text("item" + index.toString()),
);
},
),
);
}
@override
void dispose() {
timer?.cancel();
super.dispose();
}
void updatePeriodically() {
setState(() {
itemsCount += 1;
});
}
}