Skip to content

Instantly share code, notes, and snippets.

@Norbert515
Created July 17, 2018 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Norbert515/9ef5fffb1fdd2621b4f0b9535c3b4197 to your computer and use it in GitHub Desktop.
Save Norbert515/9ef5fffb1fdd2621b4f0b9535c3b4197 to your computer and use it in GitHub Desktop.
implicitly animated count example
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _count = 0;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text("Animated Count example"),
),
body: new Center(
child: new AnimatedCount(
count: _count,
curve: Curves.ease,
duration: Duration(seconds: 1),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_count += 100;
});
},
child: Icon(Icons.add),
),
);
}
}
class AnimatedCount extends ImplicitlyAnimatedWidget {
final int count;
AnimatedCount({
Key key,
@required this.count,
@required Duration duration,
Curve curve = Curves.linear
}) : super(duration: duration, curve: curve, key: key);
@override
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() => _AnimatedCountState();
}
class _AnimatedCountState extends AnimatedWidgetBaseState<AnimatedCount> {
IntTween _count;
@override
Widget build(BuildContext context) {
return new Text(_count.evaluate(animation).toString());
}
@override
void forEachTween(TweenVisitor visitor) {
_count = visitor(_count, widget.count, (dynamic value) => new IntTween(begin: value));
}
}
@atishpaul
Copy link

Great work! And is it possible to use double values as well?

@atishpaul
Copy link

I got a solution for double values and negative double values .
For those who need, try this. and let me know if it is woking for you as well.

import 'package:flutter/material.dart';
import 'package:basic_utils/basic_utils.dart' as Utilss;

AnimatedCount(
    decimalValuesAsString: '23.25',
    animatedStyle: TextStyle(color: Colors.white),
    maxLines: 1,
    curve: Curves.ease,
    duration: Duration(milliseconds: 1200),
)
class AnimatedCount extends ImplicitlyAnimatedWidget {
  final String decimalValuesAsString;
  final TextStyle animatedStyle;
  final int maxLines;

  AnimatedCount({
    Key key,
    @required this.decimalValuesAsString,
    @required Duration duration,
    this.animatedStyle,
    this.maxLines,
    Curve curve = Curves.linear
  }) : super(duration: duration, curve: curve, key: key);

  @override
  ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() => _AnimatedCountState();
}

class _AnimatedCountState extends AnimatedWidgetBaseState<AnimatedCount> {
  IntTween _count;
  var decimalIndex;
  @override
  Widget build(BuildContext context) {
    String s = _count.evaluate(animation).toString();
    if(decimalIndex != -1) {
      s = Utilss.StringUtils.addCharAtPosition(s, ".", decimalIndex);
    }
    return new Text(s,style: widget.animatedStyle,maxLines: widget.maxLines,);
  }

  @override
  void forEachTween(TweenVisitor visitor) {
    decimalIndex = widget.decimalValuesAsString.toString().indexOf('.');
    if(decimalIndex != -1) {
      // decimal present
      var withoutDecimals = widget.decimalValuesAsString.toString().replaceAll('.', '');
      _count = visitor(_count, int.parse(withoutDecimals), (dynamic value) => new IntTween(begin: value));
    } else if (decimalIndex == -1) {
      // decimal not present
      _count = visitor(_count, int.parse(widget.decimalValuesAsString), (dynamic value) => new IntTween(begin: value));
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment