如何防止Flutter ListView中的AnimatedWidgets一起移动

时间:2019-05-03 22:19:40

标签: listview animation flutter

我遇到一个问题,即我开始在列表视图磁贴中添加前导图标,然后想为其设置动画。我能够按照动画tutorial进行跟踪,足以使他们开始对onTap进行动画处理,但是它们都可以一起动画。如何使它们独特?我考虑将密钥传递给AnimatedWidget构造函数,但这似乎无济于事。

这是演示我当前问题的代码。如果点击,它们都会旋转90度,如果长按,它们会向后动画90度

import 'package:flutter/material.dart';
import 'dart:math';

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

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

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 {
  int _counter = 0;
  AnimationController controller;
  Animation animation;

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

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

    controller =
        AnimationController(duration: const Duration(milliseconds: 285), vsync: this);
    animation = Tween<double>(begin: 0, end: pi/2).animate(controller);

  }


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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
            child: ListView(
              children: ListTile.divideTiles(
                context: context,
                tiles: [
                  ListTile(
                    leading: AnimatedChevron(animation: animation),
                    onTap: ()=> controller.forward(),
                    onLongPress: ()=> controller.reverse(),
                    title: Text('Sun'),
                  ),
                  ListTile(
                    leading: AnimatedChevron(animation: animation),
                    onTap: ()=> controller.forward(),
                    onLongPress: ()=> controller.reverse(),
                    title: Text('Moon'),
                  ),
                  ListTile(
                    leading: AnimatedChevron(animation: animation),
                    onTap: ()=> controller.forward(),
                    onLongPress: ()=> controller.reverse(),
                    title: Text('Star'),
                  ),
                ],
              ).toList(),
            ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class AnimatedChevron extends AnimatedWidget {
  AnimatedChevron({Key key, Animation<double> animation})
      : super(key: key, listenable: animation);

  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    return Transform.rotate(
        angle: animation.value,
        child: Icon(Icons.chevron_right, color: Colors.blue));
  }
}

enter image description here

我在这里用过另一个great tutorial on listview

0 个答案:

没有答案