Reputation: 157
So I am trying to make counter in my cart ui that will help me update the quantity of item.
CounterCustom(
initialValue: _defaultValue,
minValue: 0,
maxValue: 10,
step: 0.5,
decimalPlaces: 1,
onChanged: (value) {
setState(() {
_defaultValue = value;
_counter = value;
});
},
),
This is the counter package I am using. If I place this widget in ListView directly under my scaffold it works fine. But if I place this widget in a custom Widget it doesn't change its value in display.
Upvotes: 0
Views: 303
Reputation: 1808
There are so many limitations in this counter package. Please consider creating your own counter. It's easy and straight forward. It's better if you separate the whole counter in a different widget so that it won't build every widgets while calling setState
. Alternatively you can use inheritted widget
or Provider state management.
int timerSeconds;
DateTime startedAt;
StartTimer(){
_timer= new Timer.periodic(oneSec, (Timer t) => {
setState(() {
startedAt=DateTime.now();
seconds--;
})
});
}
//to format the timer text
String _printDuration(Duration duration) {
String twoDigits(int n) {
if (n >= 10) return "$n";
return "0$n";
}
String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
return "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
}
String getTimerText(){
int timePassed= DateTime.now().difference(startedAt).inSeconds;
timerTextString=_printDuration(Duration(seconds: timerText-timePassed));
}
Then stop the timer when disposing of.
@override
void dispose() {
_timer.cancel();
super.dispose();
}
Then call the text string in Text widget like this
Text(getTimerText());
Upvotes: 1