Skip to content

Instantly share code, notes, and snippets.

@andrija78
Last active July 30, 2020 10:07
Show Gist options
  • Save andrija78/b1e3b9e842748f2fbe2a61f055643b9d to your computer and use it in GitHub Desktop.
Save andrija78/b1e3b9e842748f2fbe2a61f055643b9d to your computer and use it in GitHub Desktop.
Basic example of Animation from Provider
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(ChangeNotifierProvider(
create: (context) => MyProvider(), child: MyApp()));
}
class AnimationEvent {
List<int> shapeId;
bool moved;
String dialogText;
AnimationEvent({this.shapeId, this.moved, this.dialogText});
}
class MyProvider extends ChangeNotifier {
var moved = List<bool>.filled(4, false);
String dialogText;
void createEvents() {
var events = <AnimationEvent>[];
events.add(AnimationEvent(shapeId: [0], moved: true));
events.add(AnimationEvent(shapeId: [1], moved: true));
events.add(AnimationEvent(shapeId: [2], moved: true));
events.add(AnimationEvent(shapeId: [3], moved: true));
events.add(AnimationEvent(dialogText: "All 4 cards moved"));
// This is commented for now - since it would cancel each individual animation
//events.add(AnimationEvent(shapeId: [0, 1, 2, 3], moved: false));
for (var event in events) {
processEvent(event);
}
}
void processEvent(AnimationEvent event) {
if (event.shapeId != null) {
for (var shape in event.shapeId) {
moved[shape] = event.moved;
}
}
if (event.dialogText != null) {
dialogText = event.dialogText;
}
notifyListeners();
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sequential Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Sequential Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void _move(BuildContext context) {
var provider = Provider.of<MyProvider>(context, listen: false);
provider.createEvents();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Consumer<MyProvider>(builder: (context, model, child) {
if ((model.dialogText ?? "") != "") {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: Text(model.dialogText),
children: <Widget>[
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, 'ok');
},
child: const Text('Ok'),
)
]);
});
model.dialogText = "";
});
}
return Stack(
children: [
...List.generate(
4,
(index) => AnimatedPositioned(
top: (index * 120 + 50).toDouble(),
left: model.moved[index] ? 300 : 50,
duration: Duration(seconds: 3),
child: Container(
child: Text('Card $index'),
color: Colors.blue,
height: 100,
width: 50,
)))
],
);
}),
floatingActionButton: FloatingActionButton(
onPressed: () => _move(context),
tooltip: 'Move',
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment