I am given task for creating counter widget which has two iconbutton for increment and decrement value..
increment by 1 and decrement by -1, no variation..its fix..
I have created design but dont know how to implement it with – and + functionality
can anyone suggest me how to solve it
class _HomeScreenState extends State<HomeScreen> {
int age=30;
int weight=39;
int score=100;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
const SizedBox(height: 100,),
CartCounter(value: age,),
const SizedBox(height: 100,),
CartCounter(value: weight,),
const SizedBox(height: 100,),
CartCounter(value: score,),
],),
);
}
}
here is my effort but dont know how to implement increment coding and decrement coding
class CartCounter extends StatefulWidget {
final int value;
const CartCounter({Key? key,required this.value}) : super(key: key);
@override
State<CartCounter> createState() => _CartCounterState();
}
class _CartCounterState extends State<CartCounter> {
@override
Widget build(BuildContext context) {
return Container(
height: 80,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
color: Colors.blue,
onPressed: (){
}, icon: Icon(Icons.remove)),
SizedBox(width: 60,child: Center(child: Text(widget.value.toString(),style: Theme.of(context).textTheme.headlineSmall,)),),
IconButton(
color: Colors.blue,
onPressed: (){}, icon: Icon(Icons.add)),
],),
);
}
}
3
Answers
If I correctly understood your question, you want to implement the onPressed-Method in the IconButtons.
Here is my updated code:
I hope I helped you 😉
here is the solution…try..it will work
u just missing to add a Function in ur custom widget and returning value to calling class
add this Function to your custom widget and this widget should be stateful widget
final Function(int) onChange;
I have used a local variable named result and do increment and decrement it while tapping on iconbutton..