import
'package:flutter/material.dart'
;
import
'dart:async'
;
void
main() => runApp(
const
MyApp());
class
MyApp extends StatelessWidget {
const
MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return
MaterialApp(
title:
'GFG Timer Example'
,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title:
const
Text(
'GeeksforGeeks - Timer Example'
),
),
body:
const
TimerDemo(),
),
);
}
}
class
TimerDemo extends StatefulWidget {
const
TimerDemo({Key? key}) : super(key: key);
@override
State<TimerDemo> createState() => _TimerDemoState();
}
class
_TimerDemoState extends State<TimerDemo> {
Color _bgColor = Colors.green.shade50;
@override
Widget build(BuildContext context) {
return
Container(
color: _bgColor,
child: Center(
child: ElevatedButton(
onPressed: () {
changeBg();
},
child:
const
Text(
"Change the background after 3 Seconds."
),
),
),
);
}
void
changeBg() {
Timer.periodic(
const
Duration(seconds: 1), (timer) {
if
(timer.tick == 3) {
timer.cancel();
setState(() {
_bgColor = Colors.red.shade300;
});
}
});
}
}