@@ -52,6 +52,10 @@ const Radius _kSeparatorRadius = Radius.circular(_kSeparatorWidth / 2);
52
52
// amount of time.
53
53
const double _kMinThumbScale = 0.95 ;
54
54
55
+ // The maximum scale factor of the thumb, when being pressed on for a sufficient
56
+ // amount of time.
57
+ const double _kMaxThumbScale = 1.05 ;
58
+
55
59
// The minimum horizontal distance between the edges of the separator and the
56
60
// closest child.
57
61
const double _kSegmentMinPadding = 10 ;
@@ -104,6 +108,7 @@ class _Segment<T> extends StatefulWidget {
104
108
required this .isDragging,
105
109
required this .enabled,
106
110
required this .segmentLocation,
111
+ required this .isMomentary,
107
112
}) : super (key: key);
108
113
109
114
final Widget child;
@@ -112,13 +117,15 @@ class _Segment<T> extends StatefulWidget {
112
117
final bool highlighted;
113
118
final bool enabled;
114
119
final _SegmentLocation segmentLocation;
120
+ final bool isMomentary;
115
121
116
122
// Whether the thumb of the parent widget (CupertinoSlidingSegmentedControl)
117
123
// is currently being dragged.
118
124
final bool isDragging;
119
125
120
- bool get shouldFadeoutContent => pressed && ! highlighted && enabled;
126
+ bool get shouldFadeoutContent => pressed && ! highlighted && enabled && ! isMomentary ;
121
127
bool get shouldScaleContent => pressed && highlighted && isDragging && enabled;
128
+ bool get shouldHighlightContent => highlighted && ! isMomentary;
122
129
123
130
@override
124
131
_SegmentState <T > createState () => _SegmentState <T >();
@@ -148,12 +155,26 @@ class _SegmentState<T> extends State<_Segment<T>> with TickerProviderStateMixin<
148
155
assert (oldWidget.key == widget.key);
149
156
150
157
if (oldWidget.shouldScaleContent != widget.shouldScaleContent) {
151
- highlightPressScaleAnimation = highlightPressScaleController.drive (
152
- Tween <double >(
153
- begin: highlightPressScaleAnimation.value,
154
- end: widget.shouldScaleContent ? _kMinThumbScale : 1.0 ,
155
- ),
156
- );
158
+ final Animatable <double > scaleAnimation =
159
+ widget.isMomentary && widget.shouldScaleContent
160
+ ? TweenSequence <double >(< TweenSequenceItem <double >> [
161
+ TweenSequenceItem <double >(
162
+ tween: Tween <double >(
163
+ begin: highlightPressScaleAnimation.value,
164
+ end: _kMaxThumbScale,
165
+ ),
166
+ weight: 50 ,
167
+ ),
168
+ TweenSequenceItem <double >(
169
+ tween: Tween <double >(begin: _kMaxThumbScale, end: 1.0 ),
170
+ weight: 50 ,
171
+ ),
172
+ ])
173
+ : Tween <double >(
174
+ begin: highlightPressScaleAnimation.value,
175
+ end: widget.shouldScaleContent ? _kMinThumbScale : 1.0 ,
176
+ );
177
+ highlightPressScaleAnimation = highlightPressScaleController.drive (scaleAnimation);
157
178
highlightPressScaleController.animateWith (_kThumbSpringAnimationSimulation);
158
179
}
159
180
}
@@ -185,7 +206,8 @@ class _SegmentState<T> extends State<_Segment<T>> with TickerProviderStateMixin<
185
206
child: AnimatedDefaultTextStyle (
186
207
style: DefaultTextStyle .of (context).style.merge (
187
208
TextStyle (
188
- fontWeight: widget.highlighted ? _kHighlightedFontWeight : _kFontWeight,
209
+ fontWeight:
210
+ widget.shouldHighlightContent ? _kHighlightedFontWeight : _kFontWeight,
189
211
fontSize: _kFontSize,
190
212
color: widget.enabled ? null : _kDisabledContentColor,
191
213
),
@@ -352,6 +374,7 @@ class CupertinoSlidingSegmentedControl<T extends Object> extends StatefulWidget
352
374
this .padding = _kHorizontalItemPadding,
353
375
this .backgroundColor = CupertinoColors .tertiarySystemFill,
354
376
this .proportionalWidth = false ,
377
+ this .isMomentary = false ,
355
378
}) : assert (children.length >= 2 ),
356
379
assert (
357
380
groupValue == null || children.keys.contains (groupValue),
@@ -465,6 +488,14 @@ class CupertinoSlidingSegmentedControl<T extends Object> extends StatefulWidget
465
488
/// Defaults to `EdgeInsets.symmetric(vertical: 2, horizontal: 3)` .
466
489
final EdgeInsetsGeometry padding;
467
490
491
+ /// Determines whether segments in the segmented control show selected state.
492
+ ///
493
+ /// If true, segments in the control don’t show selected state and
494
+ /// don’t update the value of selectedSegmentIndex after tracking ends.
495
+ ///
496
+ /// Defaults to false.
497
+ final bool isMomentary;
498
+
468
499
@override
469
500
State <CupertinoSlidingSegmentedControl <T >> createState () => _SegmentedControlState <T >();
470
501
}
@@ -635,6 +666,7 @@ class _SegmentedControlState<T extends Object> extends State<CupertinoSlidingSeg
635
666
if (isThumbDragging) {
636
667
return ;
637
668
}
669
+
638
670
final T segment = segmentForXPosition (details.localPosition.dx);
639
671
onPressedChangedByGesture (null );
640
672
if (segment != widget.groupValue && ! widget.disabledChildren.contains (segment)) {
@@ -769,6 +801,7 @@ class _SegmentedControlState<T extends Object> extends State<CupertinoSlidingSeg
769
801
isDragging: isThumbDragging,
770
802
enabled: ! widget.disabledChildren.contains (entry.key),
771
803
segmentLocation: segmentLocation,
804
+ isMomentary: widget.isMomentary,
772
805
child: entry.value,
773
806
),
774
807
),
@@ -807,7 +840,7 @@ class _SegmentedControlState<T extends Object> extends State<CupertinoSlidingSeg
807
840
builder: (BuildContext context, Widget ? child) {
808
841
return _SegmentedControlRenderWidget <T >(
809
842
key: segmentedControlRenderWidgetKey,
810
- highlightedIndex: highlightedIndex,
843
+ highlightedIndex: widget.isMomentary ? null : highlightedIndex,
811
844
thumbColor: CupertinoDynamicColor .resolve (widget.thumbColor, context),
812
845
thumbScale: thumbScaleAnimation.value,
813
846
proportionalWidth: widget.proportionalWidth,
0 commit comments