cytrinox
cytrinox

Reputation: 1946

Flutter: Spinning sync icon in AppBar

How can I animate an IconButton placed in a AppBar? The sync icon should spinning while a database synchronisation is running.

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dashboard"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.sync), // <-- Icon
            onPressed: () {
              print("sync");
              // start spinning
              syncDatabaseFull(); // Returns future and resolves when sync is finish
            },
          )
        ],
      ),
      body: Center(
        child: RaisedButton(
          child: Text('HOME screen'),
          onPressed: () {
          },
        ),
      ),
    );
  }
}

Upvotes: 3

Views: 4458

Answers (2)

Chandler
Chandler

Reputation: 3295

CustomedSpinningIconButton class

import 'package:flutter/material.dart';

class SpinningIconButton extends AnimatedWidget {
  final VoidCallback onPressed;
  final IconData iconData;
  final AnimationController controller;
  SpinningIconButton({Key key, this.controller, this.iconData, this.onPressed})
      : super(key: key, listenable: controller);

  Widget build(BuildContext context) {
    final Animation<double> _animation = CurvedAnimation(
      parent: controller,
      // Use whatever curve you would like, for more details refer to the Curves class
      curve: Curves.linearToEaseOut,
    );

    return RotationTransition(
      turns: _animation,
      child: IconButton(
        icon: Icon(iconData),
        onPressed: onPressed,
      ),
    );
  }
}

How to use it:

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
  AnimationController _animationController;

  @override
  void initState() {
    super.initState();

    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1)
    );
  }

  @override
  Widget build(BuildContext context) {
    ...
        actions: <Widget>[
          SpinningIconButton(
            controller: _animationController,
            iconData: Icons.sync,
            onPressed: () async {
              // Play the animation infinitely
              _animationController.repeat();

              // Sleep 1.5 seconds or await the Async method
              print('Something has finished.');

              // Complete current cycle of the animation
              _animationController.forward(from: _animationController.value);
            },
          )
        ],
    ...
  }

Upvotes: 3

chunhunghan
chunhunghan

Reputation: 54427

You can copy paste run full code below
You can extend AnimatedWidget and pass callback
example code below simulate syncDatabaseFull run for 5 seconds

code snippet

class AnimatedSync extends AnimatedWidget {
  VoidCallback callback;
  AnimatedSync({Key key, Animation<double> animation, this.callback})
      : super(key: key, listenable: animation);

  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    return Transform.rotate(
      angle: animation.value,
      child: IconButton(
          icon: Icon(Icons.sync), // <-- Icon
          onPressed: () => callback()),
    );
  }
}

actions: <Widget>[
      AnimatedSync(
        animation: rotateAnimation,
        callback: () async{
          controller.forward();
          await syncDatabaseFull();
          controller.stop();
          controller.reset(); 
        },
      ),
    ],

working demo

enter image description here

full code

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class AnimatedSync extends AnimatedWidget {
  VoidCallback callback;
  AnimatedSync({Key key, Animation<double> animation, this.callback})
      : super(key: key, listenable: animation);

  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    return Transform.rotate(
      angle: animation.value,
      child: IconButton(
          icon: Icon(Icons.sync), // <-- Icon
          onPressed: () => callback()),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation colorAnimation;
  Animation rotateAnimation;

  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  Future<bool> syncDatabaseFull() async{
    await Future.delayed(Duration(seconds: 5), () {

    });
    return Future.value(true);
  }

  @override
  void initState() {
    controller =
        AnimationController(vsync: this, duration: Duration(seconds: 200));
    rotateAnimation = Tween<double>(begin: 0.0, end: 360.0).animate(controller);

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          AnimatedSync(
            animation: rotateAnimation,
            callback: () async{
              controller.forward();
              await syncDatabaseFull();
              controller.stop();
              controller.reset();
            },
          ),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Upvotes: 6

Related Questions