一个 ListView 在 AnimatedCrossFade 中不可滚动

One ListView isn't scrollable in AnimatedCrossFade

我是 flutter 开发的新手,遇到了一个问题: 我创建了新的 AnimatedCrossFade 并将两个不同的 ListView 添加到第一个和第二个 child,但总是一个 child ListView 可滚动。如何在第一个和第二个 child 中实现可滚动的 ListView?

(与简单的 GridView 情况相同。)

这看起来像是 AnimatedCrossFade. I filed an issue 中的错误。

您可以通过不使用 AnimatedCrossFade 并使用 FadeTransition 构建您自己的动画来解决这个问题。源代码如下。点击播放按钮,会这样开始

然后这样结束:

您可以通过再次单击该按钮来回淡入淡出。两个列表都是可滚动的,它会记住您的滚动位置。

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

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

class MyApp extends StatefulWidget {
  createState() => new MyAppState();
}

class MyAppState extends State<MyApp> with TickerProviderStateMixin {
  AnimationController _controller;
  Key _key1 = new GlobalKey();
  Key _key2 = new GlobalKey();

  @override
  void initState() {
    _controller = new AnimationController(
      duration: const Duration(milliseconds: 700),
      vsync: this,
    );
  }

  Widget _buildList(Key key, Color color, double opacity) {
    return new Opacity(
      opacity: opacity,
      child: new Container(
        // Keep the hidden ListView around to maintain its scroll position
        // but set its height to 0.0 so it can't interfere with touches
        height: opacity == 0.0 ? 0.0 : null,
        decoration: new BoxDecoration(color: color),
        child: new ListView(
          key: new GlobalObjectKey(color),
          children: new List.generate(
            100,
            (index) => new Text('Item $index'),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new Scaffold(
        floatingActionButton: new FloatingActionButton(
          child: new Icon(Icons.play_arrow),
          onPressed: () {
            if (_controller.status == AnimationStatus.dismissed ||
              _controller.status == AnimationStatus.reverse) {
              _controller.forward();
            } else {
              _controller.reverse();
            }
          },
        ),
        body: new Center(
          child: new Container(
            width: 100.0,
            height: 100.0,
            child: new AnimatedBuilder(
            animation: _controller,
            builder: (BuildContext context, Widget child) {
              return new Stack(
                children: [
                  _buildList(_key1, Colors.red[200], _controller.value),
                  _buildList(_key2, Colors.blue[200], 1.0 - _controller.value),
                ],
              );
            }
          ),
        ),
        ),
      ),
    );
  }
}
}