Last active
August 2, 2018 12:07
-
-
Save wmleler/626677c5206340ce0b16b69a2cd68439 to your computer and use it in GitHub Desktop.
Matrix #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Perspective', | |
theme: ThemeData(primarySwatch: Colors.blue), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> | |
with SingleTickerProviderStateMixin { | |
Offset _offset = Offset.zero; | |
int _counter = 0; | |
AnimationController _controller; | |
Animation<Offset> _flingAnimation; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController(vsync: this) | |
..addListener(() => setState(() { | |
_offset = _flingAnimation.value; | |
})); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedBuilder( | |
animation: _controller, | |
child: GestureDetector( | |
onDoubleTap: () => setState(() { | |
_offset = Offset.zero; | |
_counter = 0; | |
}), | |
onPanStart: (details) => setState(() { | |
_controller.stop(); | |
}), | |
onPanUpdate: (details) => setState(() => _offset += details.delta), | |
onPanEnd: (details) { | |
print('$_offset, End: $details'); | |
}, | |
child: _defaultApp(context), | |
), | |
builder: (BuildContext context, Widget child) { | |
return Transform( | |
transform: Matrix4.identity() | |
..setEntry(3, 2, 0.001) // perspective | |
..rotateX(0.01 * _offset.dy) | |
..rotateY(-0.01 * _offset.dx), | |
alignment: FractionalOffset.center, | |
child: child, | |
); | |
}); | |
} | |
// from the original default app | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
_defaultApp(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Tilt-a-Whirl'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.display1, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment