Skip to content

Instantly share code, notes, and snippets.

@mkiisoft
Last active May 17, 2020 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkiisoft/6eb2597df8829c6b99897a1e57be9a12 to your computer and use it in GitHub Desktop.
Save mkiisoft/6eb2597df8829c6b99897a1e57be9a12 to your computer and use it in GitHub Desktop.
Card + ListView Animation by Mariano Zorrilla
/*
MIT License
Copyright (c) 2020 Mariano Zorrilla
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import 'package:flutter/material.dart';
import 'dart:convert';
class Movie {
String title;
int year;
String image;
String release;
Movie.toObject(Map<String, dynamic> json)
: title = json['title'],
year = json['year'],
image = json['image'],
release = json['release'];
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'SFXP Meetup',
home: HomeScreen());
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
color: const Color(0xFFDDDDDD),
child: SafeArea(
child: Stack(
children: [
Container(
margin: const EdgeInsets.only(top: 240),
color: Colors.deepPurple[700],
),
Container(
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
child: Column(
children: [
_Header(),
const SizedBox(height: 25),
_Body(),
],
),
),
Container(
child: Padding(
padding: const EdgeInsets.only(top: 370),
child: FoldListView(),
),
),
],
),
),
);
}
}
class _Body extends StatefulWidget {
@override
_BodyState createState() => _BodyState();
}
class _BodyState extends State<_Body> {
double localX = 0;
double localY = 0;
bool defaultPosition = true;
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
double percentageX = (localX / (size.width - 40)) * 100;
double percentageY = (localY / 230) * 100;
return Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.001)
..rotateX(defaultPosition ? 0 : (0.3 * (percentageY / 50) + -0.3))
..rotateY(defaultPosition ? 0 : (-0.3 * (percentageX / 50) + 0.3)),
alignment: FractionalOffset.center,
child: Container(
width: double.infinity,
height: 230,
decoration: BoxDecoration(
color: Color(0xFFCCCCCC),
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
offset: Offset(0, 60),
color: Color.fromARGB(120, 0, 0, 0),
blurRadius: 22,
spreadRadius: -20),
],
),
child: GestureDetector(
onPanCancel: () => setState(() => defaultPosition = true),
onPanDown: (_) => setState(() => defaultPosition = false),
onPanEnd: (_) => setState(() {
localY = 115;
localX = (size.width - 40) / 2;
defaultPosition = true;
}),
onPanUpdate: (details) {
if (mounted) setState(() => defaultPosition = false);
if (details.localPosition.dx > 0 &&
details.localPosition.dy < 230) {
if (details.localPosition.dx < size.width - 40 &&
details.localPosition.dy > 0) {
localX = details.localPosition.dx;
localY = details.localPosition.dy;
}
}
},
child: ClipRRect(
borderRadius: BorderRadius.circular(15),
child: Container(
color: Colors.black,
child: Stack(
fit: StackFit.expand,
children: [
Transform(
transform: Matrix4.identity()
..translate(
defaultPosition ? 0.0 : (8 * (percentageX / 50) + -8),
defaultPosition ? 0.0 : (8 * (percentageY / 50) + -8),
0.0),
alignment: FractionalOffset.center,
child: Opacity(
opacity: 0.4,
child: Image.network(
'https://firebasestorage.googleapis.com/v0/b/sfxp-crossplatform.appspot.com/o/resources%2Fcardbg.jpg?alt=media',
fit: BoxFit.cover,
),
),
),
Stack(
children: <Widget>[
Transform(
transform: Matrix4.translationValues(
(size.width - 90) - localX,
(230 - 50) - localY,
0.0,
),
child: AnimatedOpacity(
opacity: defaultPosition ? 0 : 0.99,
duration: Duration(milliseconds: 500),
curve: Curves.decelerate,
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(boxShadow: [
BoxShadow(
color: Colors.white.withOpacity(0.22),
blurRadius: 100,
spreadRadius: 40,
)
]),
),
),
),
],
),
Transform(
transform: Matrix4.identity()
..translate(
defaultPosition
? 0.0
: (15 * (percentageX / 50) + -15),
defaultPosition
? 0.0
: (15 * (percentageY / 50) + -15),
0.0),
alignment: FractionalOffset.center,
child: Padding(
padding: const EdgeInsets.only(right: 22),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.only(left: 22, top: 15),
child: Padding(
padding: const EdgeInsets.only(top: 5),
child: Image.network(
"https://firebasestorage.googleapis.com/v0/b/sfxp-crossplatform.appspot.com/o/resources%2Fsfxp_white.png?alt=media",
width: 90,
color: Colors.white),
),
),
),
Expanded(child: Container()),
Text(
'5048 3817 4921 8497',
style: TextStyle(
fontFamily: "CreditCard",
color: Colors.white.withOpacity(0.9),
fontSize: 18,
),
),
SizedBox(height: 10),
Row(
children: [
Expanded(child: Container()),
Text(
'THRU 09/22',
style: TextStyle(
fontFamily: "CreditCard",
color: Colors.white.withOpacity(0.8),
fontSize: 12),
),
],
),
SizedBox(height: 20),
],
),
),
)
],
),
),
),
),
),
);
}
}
class _Header extends StatelessWidget {
const _Header({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Row(
children: [
Container(
height: 50,
width: 50,
child: Icon(Icons.menu, color: Colors.black),
decoration: BoxDecoration(
color: Color(0xFFDDDDDD),
borderRadius: BorderRadius.circular(25),
boxShadow: [
BoxShadow(
offset: Offset(5, 5),
color: Color.fromARGB(40, 0, 0, 0),
blurRadius: 8),
BoxShadow(
offset: Offset(-5, -5),
color: Color.fromARGB(150, 255, 255, 255),
blurRadius: 8)
],
),
),
Expanded(
child: Center(
child: Text(
'Cinema Plus',
style: TextStyle(
fontFamily: "Product",
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 30,
),
),
),
),
SizedBox(width: 50)
],
);
}
}
class FoldListView extends StatefulWidget {
@override
_FoldListViewState createState() => _FoldListViewState();
}
class _FoldListViewState extends State<FoldListView> {
var _scrollController = ScrollController();
var topScroll = 0.0;
var percentageY = 1.0;
var currentIndex = 0;
var opacity = 1.0;
var list = [];
static const cardSize = 225;
String jsonData = '[ { "title": "Mission: Impossible – Fallout", "year": 2018, "image": "https://www.gstatic.com/tv/thumb/v22vodart/13492451/p13492451_v_v8_ad.jpg", "release": "July 27, 2018" }, { "title": "Incredibles 2", "year": 2018, "image": "https://www.gstatic.com/tv/thumb/v22vodart/13446354/p13446354_v_v8_ay.jpg", "release": "June 15, 2018" }, { "title": "Once Upon a Time in Hollywood", "year": 2019, "image": "https://www.gstatic.com/tv/thumb/v22vodart/15226224/p15226224_v_v8_ad.jpg", "release": "July 26, 2019" }, { "title": "John Henry", "year": 2020, "image": "https://www.gstatic.com/tv/thumb/v22vodart/17733489/p17733489_v_v8_aa.jpg", "release": "January 24, 2020" }, { "title": "Timmy Failure: Mistakes Were Made", "year": 2020, "image": "https://miro.medium.com/max/500/0*7ZUeYQc4vUx_i1qQ.jpg", "release": "January 25, 2020" }, { "title": "Avengers: Endgame", "year": 2019, "image": "https://www.gstatic.com/tv/thumb/v22vodart/15366809/p15366809_v_v8_af.jpg", "release": "April 26, 2019" }, { "title": "Joker", "year": 2019, "image": "https://pbs.twimg.com/media/EDEsh0gU4AUTO3P?format=jpg&name=900x900", "release": "October 4, 2019" }, { "title": "Spider-Man: Into the Spider-Verse", "year": 2018, "image": "https://www.gstatic.com/tv/thumb/v22vodart/14939602/p14939602_v_v8_ae.jpg", "release": "December 14, 2018" }, { "title": "First Man", "year": 2018, "image": "https://www.gstatic.com/tv/thumb/v22vodart/15398283/p15398283_v_v8_ae.jpg", "release": "October 10, 2018" } ]';
void mockData() {
var json = jsonDecode(jsonData);
setState(() {
list = (json as List).map((object) => Movie.toObject(object)).toList();
});
}
@override
void initState() {
super.initState();
mockData();
_scrollController.addListener(() {
setState(() {
currentIndex = _scrollController.offset ~/ cardSize;
int currentSize = cardSize * currentIndex;
topScroll = _scrollController.offset - currentSize;
percentageY = (topScroll / cardSize) * 100;
var rawOpacity = 1 - (percentageY / 100);
var offsetOpacity = (percentageY / 100) * 0.25;
opacity = rawOpacity - offsetOpacity;
});
});
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return ScrollConfiguration(
behavior: NoScrollBehavior(),
child: ListView.builder(
itemBuilder: (context, index) {
final Movie movie = list[index];
return Transform(
alignment: FractionalOffset.center,
transform: Matrix4.identity()
..setEntry(3, 2, 0.0015)
..rotateX((currentIndex == index) ? (-1.25 * (percentageY / 50)) : index < currentIndex ? 99 : 0),
child: Padding(
padding: const EdgeInsets.only(left: 20, right: 20, bottom: 25),
child: SizedBox(
height: 200,
child: Opacity(
opacity: currentIndex == index ? (opacity < 0.0) ? 0 : (opacity > 1.0) ? 1 : opacity : 1,
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
child: Stack(
children: [
ClipPath(
clipper: HorizontalClipper(),
child: Align(
alignment: Alignment.centerLeft,
child: ClipRRect(
borderRadius:
BorderRadius.only(topLeft: Radius.circular(15), bottomLeft: Radius.circular(15)),
child: Image.network(movie.image, fit: BoxFit.fitWidth, width: (size.width - 40) * 0.45),
),
),
),
Column(
mainAxisSize: MainAxisSize.max,
children: [
Container(
margin: EdgeInsets.only(top: 20, left: (size.width - 40) * 0.45, right: 20),
child: Align(
alignment: Alignment.topLeft,
child: Text(
movie.title,
style: TextStyle(
fontFamily: "Product",
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 24,
),
),
),
),
Expanded(child: Container()),
Padding(
padding: const EdgeInsets.only(bottom: 10, right: 15),
child: Align(
alignment: Alignment.bottomRight,
child: Text(
movie.release,
style: TextStyle(
fontFamily: "Product",
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18,
),
),
),
)
],
)
],
),
),
),
),
),
);
},
itemCount: list.length,
controller: _scrollController,
padding: const EdgeInsets.only(top: 30, bottom: 20),
),
);
}
}
class HorizontalClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
Path path = Path();
path.lineTo(size.width * 0.35, 0);
path.lineTo(size.width * 0.45, size.height);
path.lineTo(0, size.height);
path.close();
return path;
}
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
class NoScrollBehavior extends ScrollBehavior {
@override
Widget buildViewportChrome(BuildContext context, Widget child, AxisDirection axisDirection) {
return child;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment