I have a floating action button in my scaffold and I am starting a timer when that button is pressed and also it opens the BottomModalSheet by using showModalBottomSheet() function but the timer is not updating in the BottomSheet
Code Example:
// Bottom sheet timer
Duration duration = Duration();
Timer? timer;
// Start timer method
void startTimer() {
print("timer start");
timer = Timer.periodic(Duration(seconds: 1), (_) => addTime());
}
void addTime() {
final addSeconds = 1;
setState(() {
final seconds = duration.inSeconds + addSeconds;
duration = Duration(seconds: seconds);
});
}
@override
Widget build(BuildContext context) {
if (isProfileLoaded) {
return Scaffold(
key: _scaffoldKey,
backgroundColor: CassettColors.gray,
extendBodyBehindAppBar: true,
floatingActionButton: FloatingActionButton.large(
onPressed: () {
startTimer();
showModalBottomSheet(
context: context,
builder: (context) {
return StatefulBuilder(builder: (BuildContext context,
StateSetter setState /*You can rename this!*/) {
return buildSheet(duration);
});
});
},
child: Text(
"Start",
style: GoogleFonts.openSans(
textStyle: TextStyle(
color: CassettColors.text100,
fontSize: 16,
fontWeight: FontWeight.w600),
),
),
backgroundColor: CassettColors.primaryRed,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
}