|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:math'; |
| 3 | + |
| 4 | +import 'package:drinkshop/models.dart'; |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | + |
| 7 | +class DrinkSwitchMenu extends StatefulWidget { |
| 8 | + final StreamController<DrinkType> drinkTypeStream; |
| 9 | + |
| 10 | + DrinkSwitchMenu(this.drinkTypeStream); |
| 11 | + |
| 12 | + @override |
| 13 | + State createState() => new DrinkSwitchMenuState(); |
| 14 | +} |
| 15 | + |
| 16 | +class DrinkSwitchMenuState extends State<DrinkSwitchMenu> |
| 17 | + with TickerProviderStateMixin { |
| 18 | + AnimationController controller; |
| 19 | + |
| 20 | + Animation<double> drinksScaleAnimation; |
| 21 | + Animation<double> drinksTiltAnimation; |
| 22 | + Animation<double> drinksTranslateAnimation; |
| 23 | + |
| 24 | + static Key frappeKey = new Key("__frappe__"); |
| 25 | + static Key glassKey = new Key("__glass__"); |
| 26 | + Key active = frappeKey; |
| 27 | + |
| 28 | + @override |
| 29 | + void initState() { |
| 30 | + super.initState(); |
| 31 | + |
| 32 | + controller = new AnimationController( |
| 33 | + vsync: this, duration: new Duration(milliseconds: 200)); |
| 34 | + drinksScaleAnimation = new Tween<double>(begin: 1.0, end: 1.5).animate( |
| 35 | + new CurvedAnimation( |
| 36 | + parent: controller, |
| 37 | + curve: new Interval(0.0, 0.5, curve: Curves.easeIn))); |
| 38 | + drinksTiltAnimation = new Tween<double>(begin: 0.0, end: 40.0).animate( |
| 39 | + new CurvedAnimation( |
| 40 | + parent: controller, |
| 41 | + curve: new Interval(0.0, 0.5, curve: Curves.easeIn))); |
| 42 | + drinksTranslateAnimation = new Tween<double>(begin: 0.0, end: 30.0).animate( |
| 43 | + new CurvedAnimation( |
| 44 | + parent: controller, |
| 45 | + curve: new Interval(0.0, 0.5, curve: Curves.easeIn))); |
| 46 | + |
| 47 | + drinksScaleAnimation.addListener(() { |
| 48 | + setState(() {}); |
| 49 | + }); |
| 50 | + drinksTiltAnimation.addListener(() { |
| 51 | + setState(() {}); |
| 52 | + }); |
| 53 | + drinksTranslateAnimation.addListener(() { |
| 54 | + setState(() {}); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + @override |
| 59 | + void dispose() { |
| 60 | + super.dispose(); |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + Widget build(BuildContext context) { |
| 65 | + final inactiveTransform = new Matrix4.identity() |
| 66 | + ..translate(-10.0 - drinksTranslateAnimation.value, 0.0, 0.0) |
| 67 | + ..scale(drinksScaleAnimation.value, drinksScaleAnimation.value) |
| 68 | + ..rotateZ(-drinksTiltAnimation.value * (PI / 180)); |
| 69 | + final activeTransform = new Matrix4.identity() |
| 70 | + ..translate(0.0, 10.0 + drinksTranslateAnimation.value, 0.0) |
| 71 | + ..scale(drinksScaleAnimation.value, drinksScaleAnimation.value) |
| 72 | + ..rotateZ(drinksTiltAnimation.value * (PI / 180)); |
| 73 | + |
| 74 | + final glass = new Transform( |
| 75 | + key: glassKey, |
| 76 | + transform: active == glassKey ? activeTransform : inactiveTransform, |
| 77 | + origin: new Offset(20.0, 20.0), |
| 78 | + child: new Image.asset( |
| 79 | + "assets/images/glass.png", |
| 80 | + width: 35.0, |
| 81 | + height: 35.0, |
| 82 | + ), |
| 83 | + ); |
| 84 | + |
| 85 | + final frappe = new Transform( |
| 86 | + key: frappeKey, |
| 87 | + transform: active == frappeKey ? activeTransform : inactiveTransform, |
| 88 | + origin: new Offset(20.0, 20.0), |
| 89 | + child: new Image.asset( |
| 90 | + "assets/images/frappe.png", |
| 91 | + width: 35.0, |
| 92 | + height: 35.0, |
| 93 | + ), |
| 94 | + ); |
| 95 | + |
| 96 | + return new GestureDetector( |
| 97 | + onTap: () { |
| 98 | + swapDrinks(); |
| 99 | + }, |
| 100 | + child: new Container( |
| 101 | + child: new Stack( |
| 102 | + children: active == glassKey |
| 103 | + ? <Widget>[frappe, glass] |
| 104 | + : <Widget>[glass, frappe], |
| 105 | + ), |
| 106 | + ), |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + void swapDrinks() async { |
| 111 | + widget.drinkTypeStream |
| 112 | + .add(active == frappeKey ? DrinkType.frappe : DrinkType.glass); |
| 113 | + await controller.forward(); |
| 114 | + await controller.reverse(); |
| 115 | + |
| 116 | + setState(() { |
| 117 | + if (active == glassKey) { |
| 118 | + active = frappeKey; |
| 119 | + } else if (active == frappeKey) { |
| 120 | + active = glassKey; |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
0 commit comments