当前位置: 首页 > 面试题库 >

在颤动中具有捕捉效果的水平滚动卡

拓拔谭三
2023-03-14
问题内容

我想创建一张卡的列表,从左或向右滑动时可以快速对齐以适应效果。

每张卡之间都有一定的间距,适合于屏幕

除此之外,这些水平可滚动列表元素应包含在垂直可滚动列表内。

我能做的就是在flutter docs中的以下示例之后仅显示水平滚动卡的列表。

class SnapCarousel extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = "Horizontal List";

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Container(
          margin: EdgeInsets.symmetric(vertical: 20.0),
          height: 200.0,
          child: ListView(
            scrollDirection: Axis.horizontal,
            children: <Widget>[
              Container(
                width: 160.0,
                color: Colors.red,
              ),
              Container(
                width: 160.0,
                color: Colors.blue,
              ),
              Container(
                width: 160.0,
                color: Colors.green,
              ),
              Container(
                width: 160.0,
                color: Colors.yellow,
              ),
              Container(
                width: 160.0,
                color: Colors.orange,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

问题答案:

使用PageViewListView

import 'package:flutter/material.dart';

main() => runApp(MaterialApp(home: MyHomePage()));

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Carousel in vertical scrollable'),
      ),
      body: ListView.builder(
        padding: EdgeInsets.symmetric(vertical: 16.0),
        itemBuilder: (BuildContext context, int index) {
          if(index % 2 == 0) {
            return _buildCarousel(context, index ~/ 2);
          }
          else {
            return Divider();
          }
        },
      ),
    );
  }

  Widget _buildCarousel(BuildContext context, int carouselIndex) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Text('Carousel $carouselIndex'),
        SizedBox(
          // you may want to use an aspect ratio here for tablet support
          height: 200.0,
          child: PageView.builder(
            // store this controller in a State to save the carousel scroll position
            controller: PageController(viewportFraction: 0.8),
            itemBuilder: (BuildContext context, int itemIndex) {
              return _buildCarouselItem(context, carouselIndex, itemIndex);
            },
          ),
        )
      ],
    );
  }

  Widget _buildCarouselItem(BuildContext context, int carouselIndex, int itemIndex) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 4.0),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.grey,
          borderRadius: BorderRadius.all(Radius.circular(4.0)),
        ),
      ),
    );
  }
}


 类似资料:
  • 问题内容: 我正在尝试创建一系列只能水平滚动的照片。 它应该看起来像这样的 但是,只有通过指定包含照片的的宽度,才能实现上述目的(因此它们不会“自动换行”)。如果我不放宽- 看起来像这样; 使用CSS可以防止固定宽度的图像发生变化,该怎么办。 谢谢 问题答案: 您可以使用与。这样写:

  • 我有一个水平ScrollView,它有两个元素:CardView和水平RecycerView。所以,当用户水平滚动时,我希望这两个元素滚动。 我想有这样的东西:Goal,橙色的线是CardView,黄色的线是RecycerView。当用户滚动时,两个元素滚动,如下所示:Scrolled Goal。 现在在我的应用程序中,当用户滚动时,只有RecycerView滚动。CardView保持在他的位置。

  • 问题内容: 我目前正在尝试在用户在X轴上滚动时修复表格中的第一列。我正在使用此结构: 用户将选择项目的数量,即表中可能是90个项目。这将需要在X轴上滚动。我得到的问题是: 如何确定(和中的)标记的位置? 我一直在看其他一些线程,但是它们并没有真正解释我如何实现固定列,这使我很难理解代码的作用和应做的事情。 我还检查了人们将标题栏拆分到另一个表中的解决方案。这对我来说是不可能的,因为稍后我会将数据导

  • 问题内容: 我有一个部门想要显示图像,然后单击以在灯箱中打开它们。我将它们向左浮动并在线显示它们。设置overflow- x进行滚动,但是一旦行空间不足,它仍然会将图像放在下面。我想让它们内联,并在需要时显示水平滚动。 注意: 我无法更改内部图像的结构。它必须是锚点内的img。我的灯箱就这样要求它。 HTML: CSS: 我知道这是非常基本的,但是我做不到。不知道怎么了 问题答案: HTML中可能

  • 制作列表滚动效果的核心就在于滚动框的scrollTop属性,它代表着我们滚动了多少像素。 当然为了让容器不显示滚动条我们会设置overflow:hidden; 来隐藏滚动条。 无缝滚动效果 准备HTML结构 <div class="container"> <h1>诗词推荐</h1> <div class="list"> <ul> <

  • 问题内容: 我一直在尝试一种方法来制作具有固定的第一列的表(表的其余部分具有水平的溢出),我看到有类似问题的帖子。但是固定的列位似乎没有解决。帮帮我? 问题答案: 我有一个类似的样式,如下所示: