简体   繁体   English

当滚动方向为水平时,如何显示设置的 ListView 的中间孩子?

[英]How to show the set middle child of ListView when scroll Direction is horizontal?

So, I am trying to show pricing cards.所以,我正在尝试展示定价卡。

Where idea is to show them horizontally scrollable, so I used listView and set the scrollDirection: Axis.horizontal but, now as it gets render it show the first child in a weird position.想法是让它们水平滚动,所以我使用 listView 并设置scrollDirection: Axis.horizontal但是,现在当它渲染时,它会在奇怪的 position 中显示第一个孩子。

我的结果

So, I was trying if I can show the middle child on-screen rather than the first.因此,我正在尝试是否可以在屏幕上显示中间的孩子而不是第一个。 Like shown here:就像这里显示的: 在此处输入图像描述

Also, is it possible to add more margin to the children from ListView?另外,是否可以从 ListView 为子项添加更多边距? or any other way.或任何其他方式。

Following is my code:以下是我的代码:

Container _priceCard() {
    return Container(
      margin: new EdgeInsets.only(top: 35),
      height: 480,
      child: ListView(
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        children: <Widget>[
          Card(
            child: InkWell(
              // splashColor: Colors.blue.withAlpha(30),
              onTap: () {
                print('Card tapped.');
              },
              child: Container(
                width: 300,
                height: 450,
                padding: new EdgeInsets.all(30),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      '3 month Test Pass',
                      style:
                          TextStyle(fontSize: 23, fontWeight: FontWeight.bold),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 10),
                      child: Text(
                        '50% off for early Birds',
                        style: TextStyle(color: Colors.black54, fontSize: 16),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 40),
                      child: Text(
                        'INR 49/month',
                        style: TextStyle(
                            fontSize: 21, fontWeight: FontWeight.bold),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 7),
                      child: Text(
                        'INR 147 for 90 days',
                        style: TextStyle(
                            color: Colors.black54,
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 30),
                      child: Text(
                        'New live exam every Monday',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 15),
                      child: Text(
                        'Unlimited practise tests series',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 15),
                      child: Text(
                        'Paper tailored by AI for you',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 15),
                      child: Text(
                        'Solved previous year questions',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 35),
                      child: RaisedButton(
                        padding: const EdgeInsets.only(
                            top: 10, bottom: 10, left: 40, right: 40),
                        textColor: Colors.black,
                        color: Colors.green,
                        child: Text(
                          'Buy Now',
                          style: TextStyle(fontSize: 20),
                        ),
                        onPressed: null,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          Card(
            child: InkWell(
              // splashColor: Colors.blue.withAlpha(30),
              onTap: () {
                print('Card tapped.');
              },
              child: Container(
                width: 300,
                height: 450,
                padding: new EdgeInsets.all(30),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      '3 month Test Pass',
                      style:
                          TextStyle(fontSize: 23, fontWeight: FontWeight.bold),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 10),
                      child: Text(
                        '50% off for early Birds',
                        style: TextStyle(color: Colors.black54, fontSize: 16),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 40),
                      child: Text(
                        'INR 49/month',
                        style: TextStyle(
                            fontSize: 21, fontWeight: FontWeight.bold),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 7),
                      child: Text(
                        'INR 147 for 90 days',
                        style: TextStyle(
                            color: Colors.black54,
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 30),
                      child: Text(
                        'New live exam every Monday',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 15),
                      child: Text(
                        'Unlimited practise tests series',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 15),
                      child: Text(
                        'Paper tailored by AI for you',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 15),
                      child: Text(
                        'Solved previous year questions',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 35),
                      child: RaisedButton(
                        padding: const EdgeInsets.only(
                            top: 10, bottom: 10, left: 40, right: 40),
                        textColor: Colors.black,
                        color: Colors.green,
                        child: Text(
                          'Buy Now',
                          style: TextStyle(fontSize: 20),
                        ),
                        onPressed: null,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          Card(
            child: InkWell(
              // splashColor: Colors.blue.withAlpha(30),
              onTap: () {
                print('Card tapped.');
              },
              child: Container(
                width: 300,
                height: 450,
                padding: new EdgeInsets.all(30),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      '3 month Test Pass',
                      style:
                          TextStyle(fontSize: 23, fontWeight: FontWeight.bold),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 10),
                      child: Text(
                        '50% off for early Birds',
                        style: TextStyle(color: Colors.black54, fontSize: 16),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 40),
                      child: Text(
                        'INR 49/month',
                        style: TextStyle(
                            fontSize: 21, fontWeight: FontWeight.bold),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 7),
                      child: Text(
                        'INR 147 for 90 days',
                        style: TextStyle(
                            color: Colors.black54,
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 30),
                      child: Text(
                        'New live exam every Monday',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 15),
                      child: Text(
                        'Unlimited practise tests series',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 15),
                      child: Text(
                        'Paper tailored by AI for you',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 15),
                      child: Text(
                        'Solved previous year questions',
                        style: TextStyle(
                          color: Colors.black87,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Container(
                      margin: new EdgeInsets.only(top: 35),
                      child: RaisedButton(
                        padding: const EdgeInsets.only(
                            top: 10, bottom: 10, left: 40, right: 40),
                        textColor: Colors.black,
                        color: Colors.green,
                        child: Text(
                          'Buy Now',
                          style: TextStyle(fontSize: 20),
                        ),
                        onPressed: null,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

Currently, the children are the same.目前,孩子们都是一样的。 Also, for now, my children will be 3 only.此外,目前,我的孩子只有 3 岁。 Hence the middle one to show.因此要显示中间的。

Example of PageView PageView示例

class SO extends StatefulWidget {
  @override
  _SOState createState() => _SOState();
}

class _SOState extends State<SO> {
  var colors = [Colors.red, Colors.pink, Colors.yellow, Colors.green];
  PageController _controller;

  @override
  void initState() {
    super.initState();
    _controller = PageController(viewportFraction: .9,initialPage: 1);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      backgroundColor: Colors.blue,
      body: PageView.builder(
        controller: _controller,
        itemCount: colors.length,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            margin: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
            child: Card(
              color: colors[index],
            ),
          );
        },
      ),
    );
  }
}

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

相关问题 如何在 flutter 中从左到右和从左到右两个方向滚动水平列表视图 - How to scroll horizontal listview by both direction left to right and right left in flutter 当滚动方向为水平时,文本溢出不起作用。 TextOverflow.ellipsis 如何才能正常工作? - When scroll direction is horizontal, text overflow is not working. How can I TextOverflow.ellipsis work properly? 如何在 SingleChildScroll 内制作水平滚动 ListView - How to make Horizontal Scroll ListView inside the SingleChildScroll 如何同时滚动两个水平 ListView? - How to Scroll two Horizontal ListView at the same time? 为什么滚动方向水平不起作用? - Why scroll direction horizontal is not working? Flutter:如何将Listview的水平滚动条放在列表的中间? - Flutter: How to put a horizontal scrollbar of the Listview to be at the middle of the list? 将 ListView.Builder 滚动方向从水平更改为垂直时出错 - Getting error while changing ListView.Builder scroll direction from horizontal to verticle 如何在水平滚动视图中显示所有日期 - How to show all dates in a horizontal scroll view 滚动时水平 ListView.builder 溢出宽度限制 - Horizontal ListView.builder overflow width bound when scroll 在水平ListView中,如何单独设置items的高度? - In a Horizontal ListView, How to Set the Height of Items Individually?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM