一个ListView在AnimatedCrossFade中不可滚动

时间:2017-05-18 09:42:01

标签: android listview dart scrollable flutter

我是扑克式开发的新手,我遇到了一个问题: 我创建了新的AnimatedCrossFade,并为第一个和第二个子项添加了两个不同的ListView,但总是有一个子ListView可滚动。如何在第一个和第二个孩子中进行可滚动的ListView?

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

1 个答案:

答案 0 :(得分:0)

这看起来像AnimatedCrossFade中的错误。我提交了issue

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

blue

并以此结束:

red

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

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