如何使“堆叠式卡片列表视图”变得扑朔迷离?

时间:2019-02-02 14:35:00

标签: dart flutter flutter-layout

我想在flutter中构建类似于此链接的ui。

https://github.com/loopeer/CardStackView/blob/master/screenshot/screenshot1.gif

enter image description here

主要的理想功能如下。

  • 具有类似列表视图的功能,但卡片应堆叠在屏幕顶部。
  • 列表可以有无限的项目。因此,应回收旧卡以节省内存。
  • 我还想为每张卡设置不同的大小。

首先,我发现了一些像ui这样的“ tinder”,并尝试了它们。 https://blog.geekyants.com/tinder-swipe-in-flutter-7e4fc56021bc

但是,用户需要滑动每张卡,这需要用户滑动多次才能浏览列表项。

然后我可以以某种方式创建一个列表视图,其项目与下一个项目重叠。

import 'package:flutter/material.dart';

class StackedList extends StatelessWidget {
  List<ItemCard> cards = [];

  StackedList() {
    for (int i = 0; i < 20; i++) {
      cards.add(ItemCard(i));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('title')),
      body: Container(
        child: ListView.builder(
          itemBuilder: (context, index) {
            return Align(
              alignment: Alignment.topCenter,
              heightFactor: 0.8,
              child: cards[index],
            );
          },
          itemCount: cards.length,
        ),
      ),
    );
  }
}

class ItemCard extends StatelessWidget {
  int index;

  ItemCard(this.index);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        boxShadow: [
          BoxShadow(color: Colors.black, blurRadius: 20.0),
        ],
      ),
      child: SizedBox.fromSize(
        size: const Size(300, 400),
        child: Card(
          elevation: 5.0,
          color: index % 2 == 0 ? Colors.blue : Colors.red,
          child: Center(
            child: Text(index.toString()),
          ),
        ),
      ),
    );
  }
}

但是,项目不会停留在屏幕顶部,这并不是我想要的。 我想我可以通过定制ScrollController或ScrollPhysics来达到这种效果,但是我不确定应该在哪里更改。

1 个答案:

答案 0 :(得分:1)

您可以使用SliverPersistentHeaderCustomScrollView来实现类似的行为,还可以将卡包装在GestureDetector中,以通过更改SliverPersistentHeaderDelegate的值来修改其高度。的maxExtent参数。这是我写的一个小应用程序,可以实现与您想要的东西类似的东西:

import 'package:flutter/material.dart';
import 'dart:math' as math;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Stacked list example',
      home: Scaffold(
          appBar: AppBar(
            title: Text("Stacked list example"),
            backgroundColor: Colors.black,
          ),
          body: StackedList()),
    );
  }
}

class StackedList extends StatelessWidget {
  final List<Color> _colors = Colors.primaries;
  static const _minHeight = 16.0;
  static const _maxHeight = 120.0;

  @override
  Widget build(BuildContext context) => CustomScrollView(
        slivers: _colors
            .map(
              (color) => StackedListChild(
                minHeight: _minHeight,
                maxHeight: _colors.indexOf(color) == _colors.length - 1
                    ? MediaQuery.of(context).size.height
                    : _maxHeight,
                pinned: true,
                child: Container(
                  color: _colors.indexOf(color) == 0
                      ? Colors.black
                      : _colors[_colors.indexOf(color) - 1],
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.vertical(
                          top: Radius.circular(_minHeight)),
                      color: color,
                    ),
                  ),
                ),
              ),
            )
            .toList(),
      );
}

class StackedListChild extends StatelessWidget {
  final double minHeight;
  final double maxHeight;
  final bool pinned;
  final bool floating;
  final Widget child;

  SliverPersistentHeaderDelegate get _delegate => _StackedListDelegate(
      minHeight: minHeight, maxHeight: maxHeight, child: child);

  const StackedListChild({
    Key key,
    @required this.minHeight,
    @required this.maxHeight,
    @required this.child,
    this.pinned = false,
    this.floating = false,
  })  : assert(child != null),
        assert(minHeight != null),
        assert(maxHeight != null),
        assert(pinned != null),
        assert(floating != null),
        super(key: key);

  @override
  Widget build(BuildContext context) => SliverPersistentHeader(
      key: key, pinned: pinned, floating: floating, delegate: _delegate);
}

class _StackedListDelegate extends SliverPersistentHeaderDelegate {
  final double minHeight;
  final double maxHeight;
  final Widget child;

  _StackedListDelegate({
    @required this.minHeight,
    @required this.maxHeight,
    @required this.child,
  });

  @override
  double get minExtent => minHeight;

  @override
  double get maxExtent => math.max(maxHeight, minHeight);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new SizedBox.expand(child: child);
  }

  @override
  bool shouldRebuild(_StackedListDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}

这是实际情况:

Stacked list example .gif

这是一篇关于Flutter棉条的非常好的文章,在这方面可能会帮助您:

Slivers, demystified

希望这可以帮助您朝正确的方向前进。