CupertinoSlidingSegmentedControl doesn’t provide a way to update the value programmatically. The only workaround is to update a key, e.g., ValueKey, but doing so would disable the sliding transition, making it less ideal.
CupertinoSlidingSegmentedControl(
padding: EdgeInsets.zero,
thumbColor: ColorAssets.green,
groupValue: selected,
onValueChanged: (value) {
},
children: widget.children.map(
(key, value) => MapEntry(
key,
Container(
height: constraints.maxHeight,
alignment: Alignment.center,
padding: const EdgeInsets.only(bottom: 2),
child: Text(
value,
),
)
),
),
)
2
Answers
Here is the Snippet to achieve toggling
CupertinoSegmentedControl
programmatically.where
sliderkey
is given toCupertinoSegmentedControl
widget.You can update
CupertinoSlidingSegmentedControl
without messing with keys by usingsetState
to update the variable you’re passing to thegroupValue
parameter.Here’s an example with code:
and here’s the code. Scroll to the bottom for the relevant portion where you update the segmented control programatically.