Skip to content

Commit

Permalink
Remove boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagojencmartins committed Jul 12, 2018
1 parent a12a79e commit 88da407
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 127 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Easily create your own floating action button list
Add UnicornDialer to your **pubspec.yaml**

```
unicorndial: "^1.0.9"
unicorndial: "^1.1.0"
```

## Options ##
Expand All @@ -29,8 +29,6 @@ unicorndial: "^1.0.9"

` Icon finalButtonIcon` - **Ending Icon (after animation is complete)**



` bool hasBackground` - **Background modal is set**

` Color parentButtonBackground` - **The main floating button background color**
Expand All @@ -43,15 +41,19 @@ unicorndial: "^1.0.9"

`Function onMainButtonPressed` - **To be called if set on the UnicornDialer parent widget**

**UnicornButton class**

`Chip label` - **Creates a label for the current floating button**

`FloatingActionButton currentButton` - **Floating list button **

**UnicornButton class**

## Example ##
`FloatingActionButton currentButton` - **Floating list button **

`String labelText`
`double labelFontSize`
`Color labelColor`
`Color labelBackgroundColor`
`Color labelShadowColor` - **Label container shadow**
`bool labelHasShadow`
`bool hasLabel`


## Authors
Expand Down
19 changes: 8 additions & 11 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,29 @@ class Example extends StatefulWidget {
}

class _Example extends State<Example> {

@override
Widget build(BuildContext context) {
var ChildButtons = List<UnicornButton>();
var childButtons = List<UnicornButton>();

ChildButtons.add(UnicornButton(
label: Chip(
backgroundColor: Colors.redAccent,
label: Text("Choo choo", style: TextStyle(color: Colors.white))),
childButtons.add(UnicornButton(
hasLabel: true,
labelText: "Choo choo",
currentButton: FloatingActionButton(
heroTag: "train",
backgroundColor: Colors.redAccent,
mini: true,
child: Icon(Icons.train),
onPressed: () {
},
onPressed: () {},
)));

ChildButtons.add(UnicornButton(
childButtons.add(UnicornButton(
currentButton: FloatingActionButton(
heroTag: "airplane",
backgroundColor: Colors.greenAccent,
mini: true,
child: Icon(Icons.airplanemode_active))));

ChildButtons.add(UnicornButton(
childButtons.add(UnicornButton(
currentButton: FloatingActionButton(
heroTag: "directions",
backgroundColor: Colors.blueAccent,
Expand All @@ -47,7 +44,7 @@ class _Example extends State<Example> {
parentButtonBackground: Colors.redAccent,
orientation: UnicornOrientation.VERTICAL,
parentButton: Icon(Icons.add),
childButtons: ChildButtons),
childButtons: childButtons),
appBar: AppBar(),
body: Center(child: RaisedButton(
onPressed: () {
Expand Down
Binary file removed horizontal.gif
Binary file not shown.
250 changes: 146 additions & 104 deletions lib/unicorndial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,52 @@ class UnicornOrientation {

class UnicornButton extends StatelessWidget {
final FloatingActionButton currentButton;
final Chip label;
final String labelText;
final double labelFontSize;
final Color labelColor;
final Color labelBackgroundColor;
final Color labelShadowColor;
final bool labelHasShadow;
final bool hasLabel;

UnicornButton({this.currentButton, this.label})
UnicornButton(
{this.currentButton,
this.labelText,
this.labelFontSize = 14.0,
this.labelColor,
this.labelBackgroundColor,
this.labelShadowColor,
this.labelHasShadow = true,
this.hasLabel = false})
: assert(currentButton != null);

Widget returnLabel() {
return Container(
decoration: BoxDecoration(
boxShadow: this.labelHasShadow
? [
new BoxShadow(
color: this.labelShadowColor == null
? Color.fromRGBO(204, 204, 204, 1.0)
: this.labelShadowColor,
blurRadius: 3.0,
),
]
: null,
color: this.labelBackgroundColor == null
? Colors.white
: this.labelBackgroundColor,
borderRadius: BorderRadius.circular(3.0)), //color: Colors.white,
padding: EdgeInsets.all(9.0),
child: Text(this.labelText,
style: TextStyle(
fontSize: this.labelFontSize,
fontWeight: FontWeight.bold,
color: this.labelColor == null
? Color.fromRGBO(119, 119, 119, 1.0)
: this.labelColor)));
}

@override
Widget build(BuildContext context) {
return this.currentButton;
Expand All @@ -34,16 +75,16 @@ class UnicornDialer extends StatefulWidget {

UnicornDialer(
{this.parentButton,
this.parentButtonBackground,
this.childButtons,
this.onMainButtonPressed,
this.orientation = 1,
this.hasBackground = true,
this.backgroundColor = Colors.redAccent,
this.parentHeroTag = "parent",
this.finalButtonIcon,
this.animationDuration = 180,
this.childPadding = 4.0})
this.parentButtonBackground,
this.childButtons,
this.onMainButtonPressed,
this.orientation = 1,
this.hasBackground = true,
this.backgroundColor = Colors.white30,
this.parentHeroTag = "parent",
this.finalButtonIcon,
this.animationDuration = 180,
this.childPadding = 4.0})
: assert(parentButton != null);

_UnicornDialer createState() => _UnicornDialer();
Expand Down Expand Up @@ -117,19 +158,19 @@ class _UnicornDialer extends State<UnicornDialer>
child: !hasChildButtons
? widget.parentButton
: AnimatedBuilder(
animation: this._animationController,
builder: (BuildContext context, Widget child) {
return Transform(
transform: new Matrix4.rotationZ(
this._animationController.value * 0.8),
alignment: FractionalOffset.center,
child: new Icon(this._animationController.isDismissed
? widget.parentButton.icon
: widget.finalButtonIcon == null
? Icons.close
: widget.finalButtonIcon.icon),
);
}));
animation: this._animationController,
builder: (BuildContext context, Widget child) {
return Transform(
transform: new Matrix4.rotationZ(
this._animationController.value * 0.8),
alignment: FractionalOffset.center,
child: new Icon(this._animationController.isDismissed
? widget.parentButton.icon
: widget.finalButtonIcon == null
? Icons.close
: widget.finalButtonIcon.icon),
);
}));

if (hasChildButtons) {
var mainFloatingButton = AnimatedBuilder(
Expand All @@ -140,86 +181,87 @@ class _UnicornDialer extends State<UnicornDialer>
});

var childButtonsList = widget.childButtons == null ||
widget.childButtons.length == 0
widget.childButtons.length == 0
? List<Widget>()
: List.generate(widget.childButtons.length, (index) {
var intervalValue = index == 0
? 0.9
: ((widget.childButtons.length - index) /
widget.childButtons.length) -
0.2;
var intervalValue = index == 0
? 0.9
: ((widget.childButtons.length - index) /
widget.childButtons.length) -
0.2;

intervalValue =
intervalValue < 0.0 ? (1 / index) * 0.5 : intervalValue;
intervalValue =
intervalValue < 0.0 ? (1 / index) * 0.5 : intervalValue;

var childFAB = FloatingActionButton(
onPressed: () {
if (widget.childButtons[index].currentButton.onPressed !=
null) {
widget.childButtons[index].currentButton.onPressed();
}
var childFAB = FloatingActionButton(
onPressed: () {
if (widget.childButtons[index].currentButton.onPressed !=
null) {
widget.childButtons[index].currentButton.onPressed();
}

this._animationController.reverse();
},
child: widget.childButtons[index].currentButton.child,
heroTag: widget.childButtons[index].currentButton.heroTag,
backgroundColor:
widget.childButtons[index].currentButton.backgroundColor,
mini: true,
tooltip: widget.childButtons[index].currentButton.tooltip,
key: widget.childButtons[index].currentButton.key,
elevation: widget.childButtons[index].currentButton.elevation,
foregroundColor:
widget.childButtons[index].currentButton.foregroundColor,
highlightElevation: widget
.childButtons[index].currentButton.highlightElevation,
isExtended:
widget.childButtons[index].currentButton.isExtended,
shape: widget.childButtons[index].currentButton.shape);
this._animationController.reverse();
},
child: widget.childButtons[index].currentButton.child,
heroTag: widget.childButtons[index].currentButton.heroTag,
backgroundColor:
widget.childButtons[index].currentButton.backgroundColor,
mini: true,
tooltip: widget.childButtons[index].currentButton.tooltip,
key: widget.childButtons[index].currentButton.key,
elevation: widget.childButtons[index].currentButton.elevation,
foregroundColor:
widget.childButtons[index].currentButton.foregroundColor,
highlightElevation: widget
.childButtons[index].currentButton.highlightElevation,
isExtended:
widget.childButtons[index].currentButton.isExtended,
shape: widget.childButtons[index].currentButton.shape);

return Positioned(
right: widget.orientation == UnicornOrientation.VERTICAL
? 0.0
: ((widget.childButtons.length - index) * 55.0),
bottom: widget.orientation == UnicornOrientation.VERTICAL
? ((widget.childButtons.length - index) * 55.0)
: 0.0,
child: Container(
padding: EdgeInsets.only(
bottom:
widget.orientation == UnicornOrientation.VERTICAL
? 18.0
: 4.0,
return Positioned(
right: widget.orientation == UnicornOrientation.VERTICAL
? 4.0
: 15.0),
child: Row(children: [
ScaleTransition(
scale: CurvedAnimation(
parent: this._animationController,
curve: Interval(intervalValue, 1.0,
curve: Curves.linear),
),
alignment: FractionalOffset.center,
child: (widget.childButtons[index].label == null) ||
widget.orientation ==
UnicornOrientation.HORIZONTAL
? Container()
: Padding(
padding: EdgeInsets.only(
right: widget.childPadding),
child: widget.childButtons[index].label)),
ScaleTransition(
scale: CurvedAnimation(
parent: this._animationController,
curve: Interval(intervalValue, 1.0,
curve: Curves.linear),
),
alignment: FractionalOffset.center,
child: childFAB)
]),
));
});
: ((widget.childButtons.length - index) * 55.0),
bottom: widget.orientation == UnicornOrientation.VERTICAL
? ((widget.childButtons.length - index) * 55.0)
: 0.0,
child: Container(
padding: EdgeInsets.only(
bottom:
widget.orientation == UnicornOrientation.VERTICAL
? 18.0
: 4.0,
right: widget.orientation == UnicornOrientation.VERTICAL
? 4.0
: 15.0),
child: Row(children: [
ScaleTransition(
scale: CurvedAnimation(
parent: this._animationController,
curve: Interval(intervalValue, 1.0,
curve: Curves.linear),
),
alignment: FractionalOffset.center,
child: (!widget.childButtons[index].hasLabel) ||
widget.orientation ==
UnicornOrientation.HORIZONTAL
? Container()
: Container(
padding: EdgeInsets.only(
right: widget.childPadding),
child: widget.childButtons[index]
.returnLabel())),
ScaleTransition(
scale: CurvedAnimation(
parent: this._animationController,
curve: Interval(intervalValue, 1.0,
curve: Curves.linear),
),
alignment: FractionalOffset.center,
child: childFAB)
]),
));
});

var unicornDialWidget = Stack(
children: childButtonsList.toList()
Expand Down Expand Up @@ -257,15 +299,15 @@ class _UnicornDialer extends State<UnicornDialer>

return widget.hasBackground
? Stack(
alignment: Alignment.topCenter,
overflow: Overflow.visible,
children: [
Positioned(right: -16.0, bottom: -16.0, child: modal),
unicornDialWidget
])
alignment: Alignment.topCenter,
overflow: Overflow.visible,
children: [
Positioned(right: -16.0, bottom: -16.0, child: modal),
unicornDialWidget
])
: unicornDialWidget;
}

return mainFAB;
}
}
}
Loading

0 comments on commit 88da407

Please sign in to comment.