1

英雄般的动画

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

4

1 回答 1

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,
            ),
          ),
        ),
      ),
    );
  }
}
于 2019-11-06T08:05:48.453 回答