flutter - 如何使用动画隐藏/最小化 ListView?

标签 flutter dart flutter-layout flutter-animation

我想要一个按钮,当我单击它时,listView 会随着 animation 消失,就像“上升”动画一样。我设法在没有 animation 的情况下使用 ListView 的以下代码实现了这一点:

Visibility(
        // O valor aqui e o inicial
        visible: isExpanded,
        child: ListView.builder(
            scrollDirection: Axis.vertical,
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            itemCount: 2,
            itemBuilder: (BuildContext context, int index) {
              return Column(
                children: <Widget>[
                  SizedBox(height: 30),
                  GameCard(locale: widget.locale)
                ],
              );
            }),
      )

然后按钮执行此操作:

onTapHeader() {
    setState(() {
      if (isExpanded) {
        _controller.forward();
      } else {
        _controller.reverse();
      }
      isExpanded = !isExpanded;
    });
  }

有什么方法可以让它动起来吗?

最佳答案

如果不滥用 ExpansionPanel 小部件或做您自己的事情,似乎没有一种本地方法可以做到这一点。我的解决方案是使用 expandable库,因为它基本上是您自己做事时必须做的事情。

这是我的代码。它应该足以让您调整并做您想做的事。

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

void main() {
  runApp(MaterialApp(
    theme: ThemeData(accentColor: Colors.black87),
    home: ListExpansion(),
  ));
}

List<String> generateItems(int numberOfItems) {
  return List.generate(numberOfItems, (int index) {
    return 'Item $index';
  });
}

class ListExpansion extends StatefulWidget {
  @override
  _ListExpansionState createState() => _ListExpansionState();
}

class _ListExpansionState extends State<ListExpansion> {
  ExpandableController _controller;
  List<String> _data = generateItems(8);

  @override
  void initState() {
    super.initState();
    _controller = ExpandableController();
  }

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("List Expansion"),
          actions: <Widget>[
            IconButton(
              tooltip: _controller.expanded ? "Collapse" : "Expand",
              icon: _controller.expanded ? Icon(Icons.expand_less) : Icon(Icons.expand_more),
              onPressed: () {
                setState(() {
                  _controller.toggle();
                });
              },
            ),
          ],
        ),
        body: ExpandablePanel(
          controller: _controller,
          hasIcon: false,
          tapHeaderToExpand: false,
          tapBodyToCollapse: false,
          collapsed: ListTile(
            title: Text("I am currently collapsed. Tap the button to expand me and see a list"),
          ),
          expanded: ListView.builder(
            itemCount: _data.length,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(
                title: Text(_data[index]),
              );
            },
          ),
        ),
      ),
    );
  }
}

确保像这样添加 pub 依赖项:

dependencies:
  flutter:
    sdk: flutter

  expandable: ^3.0.1

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2

Sample of expanding a list

关于flutter - 如何使用动画隐藏/最小化 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58771867/

相关文章:

Flutter 全屏应用程序。摆脱Android应用程序底部的空白区域

image - Flutter qrImage 转换为图片

flutter - CustomPainter 和 CustomClipper 有什么区别?

flutter - 如何在 Flutter 中模糊小部件

flutter - 如何将底部导航项与停靠的晶圆厂完美对齐?

dart - Flutter 应用程序中图像显示和用户 react 的准确时间

flutter - 在Flutter的Inkwell小部件中添加边框半径

android - 在 Flutter 中的操作栏和标题之间添加一些空间

Flutter AnimatedList - 将 CurvedAnimation 添加到 SlideTransition

flutter - 如何修复 ' FlatButton hidden behind the keyboard'?