Skip to content
This repository was archived by the owner on Sep 17, 2018. It is now read-only.

Commit 72ac348

Browse files
author
Theo Bouwman
committed
0.0.2
1 parent 1b34e69 commit 72ac348

File tree

4 files changed

+57
-51
lines changed

4 files changed

+57
-51
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.0.2] - Refactor
2+
3+
* Refactored class naming
4+
15
## [0.0.1] - Initial release
26

37
* Initial release

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ A flutter package which adds the [Apple's iOS Segment Control UI element](https:
77
- Add the package to your pubspec
88
- Add following code:
99
```dart
10-
new CupertinoSegmentControl([
11-
new CupertinoSegmentControlItem("test1", new Text("test1")),
12-
new CupertinoSegmentControlItem("test2", new Text("test2")),
13-
new CupertinoSegmentControlItem("test3", new Text("test3")),
10+
new SegmentControl([
11+
new SegmentControlItem("test1", new Text("test1")),
12+
new SegmentControlItem("test2", new Text("test2")),
13+
new SegmentControlItem("test3", new Text("test3")),
1414
]),
1515
```
1616
17-
PS: minimum of 1 `CupertinoSegmentControlItem` and maximum of 3 `CupertinoSegmentControlItem` due to this bug: https://github.com/flutter/flutter/issues/12583
17+
PS: minimum of 1 `SegmentControlItem` and maximum of 3 `SegmentControlItem` due to this bug: https://github.com/flutter/flutter/issues/12583
1818
1919
## Showcase
2020

lib/cupertino_segment_control.dart

+45-43
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,31 @@ import 'package:flutter/widgets.dart';
44
import 'package:flutter/cupertino.dart';
55
import 'package:flutter/painting.dart';
66

7-
class CupertinoSegmentControlItem {
7+
class SegmentControlItem {
8+
SegmentControlItem(this.title, this.content);
9+
810
final String title;
911
final Widget content;
12+
}
1013

11-
CupertinoSegmentControlItem(this.title, this.content);
14+
abstract class SegmentControlCallbacks {
15+
void _changeTab(String title);
1216
}
1317

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);
1722

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;
2025

2126
@override
22-
State createState() {
23-
return new _CupertinoSegmentControlState();
24-
}
27+
_SegmentControlState createState() => new _SegmentControlState();
2528
}
2629

27-
class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
30+
class _SegmentControlState extends State<SegmentControl>
31+
with SegmentControlCallbacks {
2832
int _activeTabIndex;
2933

3034
@override
@@ -36,14 +40,25 @@ class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
3640
});
3741
}
3842

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+
3954
@override
4055
Widget build(BuildContext context) {
4156
Widget activeTab = widget.tabs[_activeTabIndex].content;
4257

43-
List<_CupertinoSegmentControlItem> list = <_CupertinoSegmentControlItem>[];
58+
List<_SegmentControlItem> list = <_SegmentControlItem>[];
4459

4560
for (int i = 0; i < widget.tabs.length; i++) {
46-
CupertinoSegmentControlItem tap = widget.tabs[i];
61+
SegmentControlItem tap = widget.tabs[i];
4762
bool isActive = tap == widget.tabs[_activeTabIndex];
4863
_ButtonPlace place = _ButtonPlace.start;
4964

@@ -53,7 +68,7 @@ class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
5368
place = _ButtonPlace.middle;
5469
}
5570

56-
list.add(new _CupertinoSegmentControlItem(this, tap, place, isActive));
71+
list.add(new _SegmentControlItem(this, tap, place, isActive));
5772
}
5873

5974
return new Column(
@@ -66,56 +81,43 @@ class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
6681
),
6782
padding: new EdgeInsets.all(12.0),
6883
),
69-
activeTab
84+
activeTab,
7085
],
7186
);
7287
}
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-
}
8488
}
8589

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+
8795
final double _defaultBorderRadius = 3.0;
8896

89-
final CupertinoSegmentControlItem cupertinoButtonTab;
90-
final _CupertinoSegmentControlState parent;
97+
final SegmentControlItem buttonTab;
98+
final SegmentControlCallbacks callbacks;
9199
final _ButtonPlace place;
92100
final bool isActive;
93101
final Color color;
94102
final Color inverseColor;
95103

96-
_CupertinoSegmentControlItem(
97-
this.parent, this.cupertinoButtonTab, this.place, this.isActive,
98-
{this.color = CupertinoColors.activeBlue,
99-
this.inverseColor = CupertinoColors.white});
100-
101104
@override
102105
State createState() {
103-
return new _CupertinoSegmentControlItemState(color, inverseColor);
106+
return new _SegmentControlItemState(color, inverseColor);
104107
}
105108
}
106109

107-
class _CupertinoSegmentControlItemState
108-
extends State<_CupertinoSegmentControlItem> {
110+
class _SegmentControlItemState extends State<_SegmentControlItem> {
111+
_SegmentControlItemState(this.color, this.inverseColor);
112+
109113
Color color;
110114
Color inverseColor;
111115
bool tapDown = false;
112116

113-
_CupertinoSegmentControlItemState(this.color, this.inverseColor);
114-
115117
BoxDecoration _boxDecoration(_ButtonPlace place) {
116118
BorderRadius radius;
117119

118-
switch(place) {
120+
switch (place) {
119121
case _ButtonPlace.start:
120122
radius = new BorderRadius.only(
121123
topLeft: new Radius.circular(widget._defaultBorderRadius),
@@ -173,16 +175,16 @@ class _CupertinoSegmentControlItemState
173175
_tabDown();
174176
},
175177
onTapUp: (_) {
176-
_tabUp();
178+
_tabUp();
177179
},
178180
onTap: () {
179-
widget.parent.changeTab(widget.cupertinoButtonTab.title);
181+
widget.callbacks._changeTab(widget.buttonTab.title);
180182
},
181183
child: new Container(
182184
decoration: _boxDecoration(widget.place),
183185
padding: new EdgeInsets.fromLTRB(20.0, 4.0, 20.0, 4.0),
184186
child: new Text(
185-
widget.cupertinoButtonTab.title,
187+
widget.buttonTab.title,
186188
style: new TextStyle(color: widget.isActive ? inverseColor : color),
187189
),
188190
),

pubspec.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: cupertino_segment_control
22
description: A Cupertino-stype segment control
3-
version: 0.0.1
4-
author:
5-
homepage:
3+
version: 0.0.2
4+
author: Theo Bouwman <theobouwman98@gmail.com>
5+
homepage: https://github.com/theobouwman/flutter_cupertino_segment_control
66

77
dependencies:
88
flutter:

0 commit comments

Comments
 (0)