简体   繁体   English

如何在pageview中包含两个item作为页面主要内容——Flutter

[英]How to include two items as a page main content in pageview -- Flutter

Is it possible to use PageView to reach the following work?是否可以使用PageView来完成以下工作?

I'd like to use two containers as the main content for a page in PageView .我想在PageView使用两个容器作为页面的主要内容。

I've tried to adjust viewPortFraction but it doesn't work as I expected.我试图调整viewPortFraction但它没有按我预期的那样工作。

The following effect is used by Apple App store and Prime Video and many apps. Apple App storePrime Video以及许多应用程序都使用了以下效果。

Thanks.谢谢。

在此处输入图片说明

在此处输入图片说明

您可以尝试在physics参数中使用带有PageScrollPhysics()的水平ListView

Instead of using PageView, you could just use a ListView with除了使用 PageView,您可以只使用 ListView

scrollDirection: Axis.horizontal

This way you could achieve the same result.通过这种方式,您可以获得相同的结果。 You would need to somehow provide an explicit height to your ListView if I am not mistaken.如果我没记错的话,您需要以某种方式为您的 ListView 提供一个明确的高度。

Below is a working sample.下面是一个工作示例。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Example'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
              "Top Games",
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
            ),
          ),
          Container(
            height: 200,
            child: ListView(
              scrollDirection: Axis.horizontal,
              children: <Widget>[
                GameCard(
                  "The Game 1",
                  "A puzzle game",
                  "https://lorempixel.com/200/200/abstract/",
                ),
                GameCard(
                  "The Game 2",
                  "An even beter game",
                  "https://lorempixel.com/200/200/sports/2/",
                ),
                GameCard(
                  "SportMania",
                  "Editors Choice",
                  "https://lorempixel.com/200/200/sports/3/",
                ),
                GameCard(
                  "MonkeySports",
                  "Monkeys playing Sport",
                  "https://lorempixel.com/200/200/abstract",
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}

class GameCard extends StatelessWidget {
  final String gameTitle;
  final String gameDescr;
  final String imgUrl;
  GameCard(
    this.gameTitle,
    this.gameDescr,
    this.imgUrl,
  );

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 3,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
      margin: EdgeInsets.all(5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Image.network(
              imgUrl,
              width: 180,
              height: 130,
              fit: BoxFit.cover,
              alignment: Alignment.center,
            ),
          ),
          Container(
            padding: EdgeInsets.all(4),
            width: 180,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  gameTitle,
                  maxLines: 2,
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                Text(
                  gameDescr,
                  maxLines: 2,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

例子

Hope this gets you started.希望这能让你开始。

您可以在PageController使用viewportFraction参数;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM