Created
December 28, 2020 16:53
-
-
Save sooxt98/626c935bc15da3dc69122830ce9fd26e to your computer and use it in GitHub Desktop.
Card Swap Z-Index Hack
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: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
bool isSwap = false; | |
bool changeIndex = false; | |
_animate() { | |
setState(() { | |
isSwap = !isSwap; | |
}); | |
Future.delayed(const Duration(milliseconds: 500), () { | |
// Here you can write your code | |
setState(() { | |
isSwap = !isSwap; | |
// changeIndex = !changeIndex; | |
}); | |
}); | |
if (isSwap) | |
Future.delayed(const Duration(milliseconds: 500), () { | |
setState(() { | |
changeIndex = !changeIndex; | |
}); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Stack( | |
overflow: Overflow.visible, | |
//alignment: Alignment.center, | |
children: <Widget>[ | |
AnimatedPositioned( | |
duration: Duration(milliseconds: 500), | |
left: isSwap ? 60 : 10, | |
bottom: isSwap ? 60 : 10, | |
child: Container( | |
height: 100, | |
width: 100, | |
child: Center(child: Text('1')), | |
color: Colors.cyan, | |
), | |
), | |
AnimatedPositioned( | |
duration: Duration(milliseconds: 500), | |
right: isSwap ? 60 : 10, | |
top: isSwap ? 60 : 10, | |
child: Container( | |
height: 100, | |
width: 100, | |
child: Center(child: Text('2')), | |
color: Colors.amber, | |
), | |
), | |
Container( | |
height: 100, | |
width: 100, | |
color: Colors.transparent, | |
), | |
AnimatedPositioned( | |
duration: Duration(milliseconds: 500), | |
left: isSwap ? 60 : 10, | |
bottom: isSwap ? 60 : 10, | |
child: Opacity( | |
opacity: changeIndex ? 1.0 : 0.0, | |
child: Container( | |
height: 100, | |
width: 100, | |
child: Center(child: Text('2')), | |
color: Colors.cyan, | |
)), | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _animate, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment