Skip to content

Instantly share code, notes, and snippets.

@Rudiksz
Created August 18, 2020 12:13
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 Rudiksz/cede1a5fe88e992b158ee3bf15858bd9 to your computer and use it in GitHub Desktop.
Save Rudiksz/cede1a5fe88e992b158ee3bf15858bd9 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'counters.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home1(),
);
}
}
var counters = Counters();
class Home1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Observer(builder: (_) => Text('${counters.firstCounter}')),
RaisedButton(
onPressed: counters.incrementFirstCounter,
child: Text('+'),
),
// Both counters have voluntarily a different Curve and duration
Observer(builder: (_) => Text('${counters.secondCounter}')),
RaisedButton(
onPressed: counters.incrementSecondCounter,
child: Text('+'),
),
const Text('total:'),
// The total must take into account the animation of both counters
Observer(
builder: (_) =>
Text('${counters.firstCounter + counters.secondCounter}'),
),
],
),
),
);
}
}
///---------------------------------------------------------------------------------
import 'package:flutter/animation.dart';
import 'package:flutter/scheduler.dart';
import 'package:mobx/mobx.dart';
part 'counters.g.dart';
class Counters = _Counters with _$Counters;
abstract class _Counters with Store {
@observable
int firstCounter = 0;
var firstTween = IntTween(begin: 0, end: 0);
var firstAnimation = AnimationController(
duration: Duration(seconds: 5),
animationBehavior: AnimationBehavior.normal,
vsync: TickerProviderImpl(),
);
@observable
int secondCounter = 0;
var secondTween = IntTween(begin: 0, end: 0);
var secondAnimation = AnimationController(
duration: Duration(seconds: 2),
animationBehavior: AnimationBehavior.normal,
vsync: TickerProviderImpl(),
);
_Counters() {
firstAnimation.addListener(() {
firstCounter = firstTween.evaluate(firstAnimation);
});
secondAnimation.addListener(() {
secondCounter = secondTween.evaluate(secondAnimation);
});
}
@action
animateCounterTo(String counter, int value) {
final bool _counter = counter == "first";
var tween = _counter ? firstTween : secondTween;
tween.begin = _counter ? firstCounter : secondCounter;
tween.end = (_counter ? firstCounter : secondCounter) + value;
var animation = _counter ? firstAnimation : secondAnimation;
animation
..reset()
..forward();
}
incrementFirstCounter() => animateCounterTo("first", 100);
incrementSecondCounter() => animateCounterTo("second", 100);
}
class TickerProviderImpl extends TickerProvider {
@override
Ticker createTicker(TickerCallback onTick) => Ticker(onTick);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment