【问题标题】:How to hide/minimize a ListView with an animation?如何使用动画隐藏/最小化 ListView?
【发布时间】:2020-03-05 09:35:30
【问题描述】:

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

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

有什么方法可以制作动画吗?

【问题讨论】:

    标签: flutter dart flutter-layout flutter-animation


    【解决方案1】:

    在不滥用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
    

    【讨论】:

    • 非常感谢!这正是我想要的。超级解释,超级详细,超级解决方案。谢谢大佬
    猜你喜欢
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 2013-09-08
    • 2011-07-06
    • 2012-07-27
    • 1970-01-01
    相关资源
    最近更新 更多