Skip to content

Instantly share code, notes, and snippets.

@t-artikov
Created May 13, 2019 15:37

Revisions

  1. t-artikov created this gist May 13, 2019.
    52 changes: 52 additions & 0 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    import 'package:flutter/material.dart';
    import 'package:flutter_hooks/flutter_hooks.dart';

    void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    home: MyHomePage(),
    );
    }
    }

    double smooth(double value,
    {duration = const Duration(milliseconds: 250), curve: Curves.easeInOut}) {
    final controller = useAnimationController(
    initialValue: value,
    lowerBound: double.negativeInfinity,
    upperBound: double.infinity,
    );

    useValueChanged(value, (_, __) {
    controller.animateTo(value, duration: duration, curve: curve);
    });

    return useAnimation(controller);
    }

    class MyHomePage extends HookWidget {
    @override
    Widget build(BuildContext context) {
    final counter = useState(0.0);

    return Scaffold(
    appBar: AppBar(
    title: Text('Hooks'),
    ),
    body: Center(
    child: Text(
    smooth(counter.value).toStringAsFixed(2),
    ),
    ),
    floatingActionButton: FloatingActionButton(
    onPressed: () {
    counter.value++;
    },
    child: Icon(Icons.add),
    ),
    );
    }
    }