I’m making an Instagram clone app and I’m trying to use StatefulBuilder
but the setState
function of the builder does not change the color of the Container
wrapped in GestureDetector
( onTap
function ).
StatefulBuilder(
builder: (context, setState) {
var color = Color.fromRGBO(38, 38, 38, 1);
return Flexible(
flex: 1,
child: GestureDetector(
onTap: () {
setState(() {
color = Color.fromRGBO(0, 149, 246, 1);
});
},
child: Container(
alignment: Alignment.center,
height: 30,
// width: double.infinity,
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(
Radius.circular(10),
),
color: color),
child: const Text('Follow'),
),
),
);
},
)
I want the Follow
button to change it’s color but I don’t want to use the setState
of the Stateful Widget
as I don’t want to rebuild the whole screen.
Am I using StatefulBuilder
incorrectly or is there any other solution to my problem?
2
Answers
above line should be outside of
StatefulBuilder
You define your
color
variable inside yourStatefulBuilder
, and every time you callsetState
it resets this value and set the default value to it, you need to define it out ofStatefulBuilder
, like this: