Flutter:AppBar中的旋转同步图标

tri

如何设置放置在AppBar中的IconButton的动画?在运行数据库同步时,同步图标应该旋转。

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: () {
          },
        ),
      ),
    );
  }
}
chunhunghan

您可以在下面复制粘贴运行的完整代码,
可以在下面扩展AnimatedWidget并传递callback
下面的示例代码,模拟syncDatabaseFull运行5秒钟

程式码片段

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(); 
        },
      ),
    ],

工作演示

在此处输入图片说明

完整的代码

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),
      ),
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Flutter中更改AppBar返回图标的大小

来自分类Dev

如何在Android中创建旋转图标

来自分类Dev

旋转并替换android中的fab图标

来自分类Dev

CSS在IE11中旋转图像/图标(旋转器)

来自分类Dev

圆圈中的图标/图像随着圆圈的旋转而旋转

来自分类Dev

是什么导致Java中较长的旋转和同步时间?

来自分类Dev

如何在JavaFX Canvas中同步多个图像上的旋转

来自分类Dev

如何在React Native中旋转字体真棒图标?

来自分类Dev

如何在Mapbox中旋转由geoJSON指定的图标?

来自分类Dev

如何更改Fabric.js中的旋转图标

来自分类Dev

在Flutter或Dart中同步运行Future

来自分类Dev

Flutter:如何在Appbar中制作圆形头像

来自分类Dev

在Flutter中创建可重复使用的appBar

来自分类Dev

删除AppBar但将StatusBar保持在Flutter中

来自分类Dev

在 Flutter iconButton 中单击 appBar 但错误即将到来

来自分类Dev

如何在Flutter中调整图标/图标按钮的大小?

来自分类Dev

旋转图标刷新

来自分类Dev

CSS旋转图标动画

来自分类Dev

围绕中心旋转图标

来自分类Dev

CSS旋转图标动画

来自分类Dev

围绕中心旋转图标

来自分类Dev

如何让图标旋转

来自分类Dev

Flutter:如何在AppBar中使用动画图标-我想在Flutter应用程序的应用栏中使用此动画图标代替Animatedless Icon

来自分类Dev

如何在Flutter中更改Drawer图标?

来自分类Dev

Flutter Appbar左操作

来自分类Dev

Flutter Uniqe appBar动画

来自分类Dev

Flutter AppBar前导文字

来自分类Dev

如何从不同类中实现的事件更新AppBar图标通知(状态)?

来自分类Dev

如何将 AppBar 居中以及如何减小 PrefrerredSize 中的前导图标大小?

Related 相关文章

热门标签

归档