@@ -4,27 +4,31 @@ import 'package:flutter/widgets.dart';
4
4
import 'package:flutter/cupertino.dart' ;
5
5
import 'package:flutter/painting.dart' ;
6
6
7
- class CupertinoSegmentControlItem {
7
+ class SegmentControlItem {
8
+ SegmentControlItem (this .title, this .content);
9
+
8
10
final String title;
9
11
final Widget content;
12
+ }
10
13
11
- CupertinoSegmentControlItem (this .title, this .content);
14
+ abstract class SegmentControlCallbacks {
15
+ void _changeTab (String title);
12
16
}
13
17
14
- class CupertinoSegmentControl extends StatefulWidget {
15
- final List <CupertinoSegmentControlItem > tabs;
16
- final int activeTabIndex;
18
+ class SegmentControl extends StatefulWidget {
19
+ SegmentControl (this .tabs, {this .activeTabIndex = 0 })
20
+ : assert (tabs.length > 1 && tabs.length <= 3 ),
21
+ assert (activeTabIndex <= tabs.length - 1 );
17
22
18
- CupertinoSegmentControl ( this .tabs, { this .activeTabIndex = 0 })
19
- : assert (tabs.length > 1 && tabs.length <= 3 ), assert ( activeTabIndex <= tabs.length - 1 ) ;
23
+ final List < SegmentControlItem > tabs;
24
+ final int activeTabIndex;
20
25
21
26
@override
22
- State createState () {
23
- return new _CupertinoSegmentControlState ();
24
- }
27
+ _SegmentControlState createState () => new _SegmentControlState ();
25
28
}
26
29
27
- class _CupertinoSegmentControlState extends State <CupertinoSegmentControl > {
30
+ class _SegmentControlState extends State <SegmentControl >
31
+ with SegmentControlCallbacks {
28
32
int _activeTabIndex;
29
33
30
34
@override
@@ -36,14 +40,25 @@ class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
36
40
});
37
41
}
38
42
43
+ void _changeTab (String title) {
44
+ setState (() {
45
+ for (int i = 0 ; i < widget.tabs.length; i++ ) {
46
+ SegmentControlItem t = widget.tabs[i];
47
+ if (t.title == title) {
48
+ _activeTabIndex = i;
49
+ }
50
+ }
51
+ });
52
+ }
53
+
39
54
@override
40
55
Widget build (BuildContext context) {
41
56
Widget activeTab = widget.tabs[_activeTabIndex].content;
42
57
43
- List <_CupertinoSegmentControlItem > list = < _CupertinoSegmentControlItem > [];
58
+ List <_SegmentControlItem > list = < _SegmentControlItem > [];
44
59
45
60
for (int i = 0 ; i < widget.tabs.length; i++ ) {
46
- CupertinoSegmentControlItem tap = widget.tabs[i];
61
+ SegmentControlItem tap = widget.tabs[i];
47
62
bool isActive = tap == widget.tabs[_activeTabIndex];
48
63
_ButtonPlace place = _ButtonPlace .start;
49
64
@@ -53,7 +68,7 @@ class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
53
68
place = _ButtonPlace .middle;
54
69
}
55
70
56
- list.add (new _CupertinoSegmentControlItem (this , tap, place, isActive));
71
+ list.add (new _SegmentControlItem (this , tap, place, isActive));
57
72
}
58
73
59
74
return new Column (
@@ -66,56 +81,43 @@ class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
66
81
),
67
82
padding: new EdgeInsets .all (12.0 ),
68
83
),
69
- activeTab
84
+ activeTab,
70
85
],
71
86
);
72
87
}
73
-
74
- void changeTab (String title) {
75
- setState (() {
76
- for (int i = 0 ; i < widget.tabs.length; i++ ) {
77
- CupertinoSegmentControlItem t = widget.tabs[i];
78
- if (t.title == title) {
79
- _activeTabIndex = i;
80
- }
81
- }
82
- });
83
- }
84
88
}
85
89
86
- class _CupertinoSegmentControlItem extends StatefulWidget {
90
+ class _SegmentControlItem extends StatefulWidget {
91
+ _SegmentControlItem (this .callbacks, this .buttonTab, this .place, this .isActive,
92
+ {this .color = CupertinoColors .activeBlue,
93
+ this .inverseColor = CupertinoColors .white});
94
+
87
95
final double _defaultBorderRadius = 3.0 ;
88
96
89
- final CupertinoSegmentControlItem cupertinoButtonTab ;
90
- final _CupertinoSegmentControlState parent ;
97
+ final SegmentControlItem buttonTab ;
98
+ final SegmentControlCallbacks callbacks ;
91
99
final _ButtonPlace place;
92
100
final bool isActive;
93
101
final Color color;
94
102
final Color inverseColor;
95
103
96
- _CupertinoSegmentControlItem (
97
- this .parent, this .cupertinoButtonTab, this .place, this .isActive,
98
- {this .color = CupertinoColors .activeBlue,
99
- this .inverseColor = CupertinoColors .white});
100
-
101
104
@override
102
105
State createState () {
103
- return new _CupertinoSegmentControlItemState (color, inverseColor);
106
+ return new _SegmentControlItemState (color, inverseColor);
104
107
}
105
108
}
106
109
107
- class _CupertinoSegmentControlItemState
108
- extends State <_CupertinoSegmentControlItem > {
110
+ class _SegmentControlItemState extends State <_SegmentControlItem > {
111
+ _SegmentControlItemState (this .color, this .inverseColor);
112
+
109
113
Color color;
110
114
Color inverseColor;
111
115
bool tapDown = false ;
112
116
113
- _CupertinoSegmentControlItemState (this .color, this .inverseColor);
114
-
115
117
BoxDecoration _boxDecoration (_ButtonPlace place) {
116
118
BorderRadius radius;
117
119
118
- switch (place) {
120
+ switch (place) {
119
121
case _ButtonPlace .start:
120
122
radius = new BorderRadius .only (
121
123
topLeft: new Radius .circular (widget._defaultBorderRadius),
@@ -173,16 +175,16 @@ class _CupertinoSegmentControlItemState
173
175
_tabDown ();
174
176
},
175
177
onTapUp: (_) {
176
- _tabUp ();
178
+ _tabUp ();
177
179
},
178
180
onTap: () {
179
- widget.parent. changeTab (widget.cupertinoButtonTab .title);
181
+ widget.callbacks. _changeTab (widget.buttonTab .title);
180
182
},
181
183
child: new Container (
182
184
decoration: _boxDecoration (widget.place),
183
185
padding: new EdgeInsets .fromLTRB (20.0 , 4.0 , 20.0 , 4.0 ),
184
186
child: new Text (
185
- widget.cupertinoButtonTab .title,
187
+ widget.buttonTab .title,
186
188
style: new TextStyle (color: widget.isActive ? inverseColor : color),
187
189
),
188
190
),
0 commit comments