I was trying to make an app that starts to count when a button is pressed but the app keeps getting stuck, once I press on the button it immediately becomes unresponsive
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
void looping() {
for (int i = 0; i < 100; i++) {
setState(() {
counter++;
});
sleep(Duration(seconds: 1));
}
}
int counter = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.white,
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
looping();
},
),
body: Center(
child: Text(counter.toString(),
style: TextStyle(fontSize: 50.0, fontWeight: FontWeight.bold)),
),
),
);
}
}