ListView中的项目获取高度

时间:2020-04-30 09:16:17

标签: flutter

我正在尝试使用某些卡片创建水平列表视图。 我希望列表视图的高度为X,卡片的高度为Y,我不知道为什么,但是卡片的高度为列表视图的高度。 这就是我所拥有的:

class FinanceApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Container(
          color: Color(0x180000),
          child: Column(
            children: <Widget>[
              Header(),
              SizedBox(
                height: 20,
              ),
              Expanded(
                child: Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(32.0),
                      topRight: Radius.circular(32.0),
                    ),
                  ),
                  child: Column(
                    children: <Widget>[
                      Container(
                        height: 250,
                        child: ListView(
                          scrollDirection: Axis.horizontal,
                          children: <Widget>[
                            CustomCard(),
                            CustomCard(),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

编辑:对我来说唯一有效的方法是将卡片容器包装在另一个容器中,使用填充物获得所需的大小,但这似乎不是一个很好的解决方案。

1 个答案:

答案 0 :(得分:3)

检查一下:

      import 'package:flutter/material.dart';


      class StackOverFlow extends StatefulWidget {
        @override
        _StackOverFlowState createState() => _StackOverFlowState();
      }

      class _StackOverFlowState extends State<StackOverFlow> {
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body:

            Center(
              child: Container(
                color: Colors.blue,
                height: 200.0,
                width: double.infinity,
                child: ListView.builder(
                  physics: BouncingScrollPhysics(),
                  scrollDirection: Axis.horizontal,
                  padding: const EdgeInsets.all(16.0),
                  itemCount: 100,
                  itemBuilder: _buildItem,
                ),
              ),
            ),
          );
        }

        Widget _buildItem (BuildContext context, int index) {
          return Center(
            child: Card(
              child: Text(index.toString()),
            ),
          );
        }
      }

enter image description here

要给孩子相同的尺寸,请考虑将卡片包装在容器中:

        Widget _buildItem (BuildContext context, int index) {
          return Center(
            child: Container(
              height: 100.0,
              width: 100.0,
              child: Card(
                child: Center(child: Text(index.toString())),
              ),
            ),
          );
        }

enter image description here