【问题标题】:Flutter Hero like animation between list view items列表视图项之间的 Flutter Hero 类似动画
【发布时间】:2020-03-02 14:09:30
【问题描述】:

英雄般的动画

我有一个标准的 ListView,里面有一些项目。当位置发生变化时,我想以类似 hero 的方式为它们设置动画。我怎样才能做到这一点?感谢您提前回答。

【问题讨论】:

    标签: flutter flutter-layout flutter-animation flutter-listview


    【解决方案1】:

    简单示例

    import 'package:flutter/material.dart';
    
    void main() => runApp(MaterialApp(
          home: Home(),
        ));
    
    class Home extends StatelessWidget {
      final List<String> litems = [
        "Item 1",
        "Item 2",
        "Item 3",
        "Item 4",
        "Item 5",
        "Item 6",
        "Item 7"
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Hero List'),
          ),
          body: ListView.builder(
            itemCount: litems.length,
            itemBuilder: (BuildContext ctxt, int index) {
              return ListTile(
                title: Hero(
                  tag: '${litems[index]}__heroTag',
                  child: Text(
                    litems[index],
                    style: Theme.of(context).textTheme.title,
                  ),
                ),
                trailing: IconButton(
                    icon: Icon(Icons.navigate_next),
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => ItemScreen(
                                  itemName: litems[index],
                                )),
                      );
                    }),
              );
            },
          ),
        );
      }
    }
    
    class ItemScreen extends StatelessWidget {
      final String itemName;
    
      const ItemScreen({Key key, @required this.itemName}) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(itemName),
          ),
          body: Container(
            child: Center(
              child: Hero(
                tag: '${itemName}__heroTag',
                child: Text(
                  itemName,
                  style: Theme.of(context).textTheme.title,
                ),
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 2020-07-22
      • 1970-01-01
      • 2019-04-04
      • 2021-10-07
      • 2015-04-02
      • 2019-12-16
      • 1970-01-01
      • 2019-06-25
      相关资源
      最近更新 更多