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

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo Bouwman committed Oct 19, 2017
1 parent 1b34e69 commit d45fcdb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.0.2] - Refactor

* Refactored class naming

## [0.0.1] - Initial release

* Initial release
88 changes: 45 additions & 43 deletions lib/cupertino_segment_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@ import 'package:flutter/widgets.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/painting.dart';

class CupertinoSegmentControlItem {
class SegmentControlItem {
SegmentControlItem(this.title, this.content);

final String title;
final Widget content;
}

CupertinoSegmentControlItem(this.title, this.content);
abstract class SegmentControlCallbacks {
void _changeTab(String title);
}

class CupertinoSegmentControl extends StatefulWidget {
final List<CupertinoSegmentControlItem> tabs;
final int activeTabIndex;
class SegmentControl extends StatefulWidget {
SegmentControl(this.tabs, {this.activeTabIndex = 0})
: assert(tabs.length > 1 && tabs.length <= 3),
assert(activeTabIndex <= tabs.length - 1);

CupertinoSegmentControl(this.tabs, {this.activeTabIndex = 0})
: assert(tabs.length > 1 && tabs.length <= 3), assert(activeTabIndex <= tabs.length - 1);
final List<SegmentControlItem> tabs;
final int activeTabIndex;

@override
State createState() {
return new _CupertinoSegmentControlState();
}
_SegmentControlState createState() => new _SegmentControlState();
}

class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
class _SegmentControlState extends State<SegmentControl>
with SegmentControlCallbacks {
int _activeTabIndex;

@override
Expand All @@ -36,14 +40,25 @@ class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
});
}

void _changeTab(String title) {
setState(() {
for (int i = 0; i < widget.tabs.length; i++) {
SegmentControlItem t = widget.tabs[i];
if (t.title == title) {
_activeTabIndex = i;
}
}
});
}

@override
Widget build(BuildContext context) {
Widget activeTab = widget.tabs[_activeTabIndex].content;

List<_CupertinoSegmentControlItem> list = <_CupertinoSegmentControlItem>[];
List<_SegmentControlItem> list = <_SegmentControlItem>[];

for (int i = 0; i < widget.tabs.length; i++) {
CupertinoSegmentControlItem tap = widget.tabs[i];
SegmentControlItem tap = widget.tabs[i];
bool isActive = tap == widget.tabs[_activeTabIndex];
_ButtonPlace place = _ButtonPlace.start;

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

list.add(new _CupertinoSegmentControlItem(this, tap, place, isActive));
list.add(new _SegmentControlItem(this, tap, place, isActive));
}

return new Column(
Expand All @@ -66,56 +81,43 @@ class _CupertinoSegmentControlState extends State<CupertinoSegmentControl> {
),
padding: new EdgeInsets.all(12.0),
),
activeTab
activeTab,
],
);
}

void changeTab(String title) {
setState(() {
for (int i = 0; i < widget.tabs.length; i++) {
CupertinoSegmentControlItem t = widget.tabs[i];
if (t.title == title) {
_activeTabIndex = i;
}
}
});
}
}

class _CupertinoSegmentControlItem extends StatefulWidget {
class _SegmentControlItem extends StatefulWidget {
_SegmentControlItem(this.callbacks, this.buttonTab, this.place, this.isActive,
{this.color = CupertinoColors.activeBlue,
this.inverseColor = CupertinoColors.white});

final double _defaultBorderRadius = 3.0;

final CupertinoSegmentControlItem cupertinoButtonTab;
final _CupertinoSegmentControlState parent;
final SegmentControlItem buttonTab;
final SegmentControlCallbacks callbacks;
final _ButtonPlace place;
final bool isActive;
final Color color;
final Color inverseColor;

_CupertinoSegmentControlItem(
this.parent, this.cupertinoButtonTab, this.place, this.isActive,
{this.color = CupertinoColors.activeBlue,
this.inverseColor = CupertinoColors.white});

@override
State createState() {
return new _CupertinoSegmentControlItemState(color, inverseColor);
return new _SegmentControlItemState(color, inverseColor);
}
}

class _CupertinoSegmentControlItemState
extends State<_CupertinoSegmentControlItem> {
class _SegmentControlItemState extends State<_SegmentControlItem> {
_SegmentControlItemState(this.color, this.inverseColor);

Color color;
Color inverseColor;
bool tapDown = false;

_CupertinoSegmentControlItemState(this.color, this.inverseColor);

BoxDecoration _boxDecoration(_ButtonPlace place) {
BorderRadius radius;

switch(place) {
switch (place) {
case _ButtonPlace.start:
radius = new BorderRadius.only(
topLeft: new Radius.circular(widget._defaultBorderRadius),
Expand Down Expand Up @@ -173,16 +175,16 @@ class _CupertinoSegmentControlItemState
_tabDown();
},
onTapUp: (_) {
_tabUp();
_tabUp();
},
onTap: () {
widget.parent.changeTab(widget.cupertinoButtonTab.title);
widget.callbacks._changeTab(widget.buttonTab.title);
},
child: new Container(
decoration: _boxDecoration(widget.place),
padding: new EdgeInsets.fromLTRB(20.0, 4.0, 20.0, 4.0),
child: new Text(
widget.cupertinoButtonTab.title,
widget.buttonTab.title,
style: new TextStyle(color: widget.isActive ? inverseColor : color),
),
),
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: cupertino_segment_control
description: A Cupertino-stype segment control
version: 0.0.1
author:
homepage:
version: 0.0.2
author: Theo Bouwman <[email protected]>
homepage: https://github.com/theobouwman/flutter_cupertino_segment_control

dependencies:
flutter:
Expand Down

0 comments on commit d45fcdb

Please sign in to comment.