如何根据视口高度在Flutter中约束ListView

问题描述 投票:0回答:1

我正在尝试基于视口高度限制ListView,以便在其中存在更多项目时滚动它,但是我还没有找到一种方法来成功做到这一点,而无需在SizedBox上提供固定数字。

布局看起来像这样:

- SingleChildScrollView
       - SizedBox (height equal to MediaQuery.of(context).size.height)
               - Column
                   - Text
                     - TextField
                         - SizedBox (because I found no other way)
                              - ListView

这里要注意的一点是,我不希望列表沿视图的其余部分滚动,而是始终在其自身的离开文本字段中滚动。

enter image description here

listview flutter scroll constraints flutter-layout
1个回答
1
投票

对于超出设备高度的数据时的整个屏幕滚动,那么您需要使用SingleChildScrollView小部件,当超出设备高度的数据时滚动。我创建了一个示例,其中我使用MediaQuery来确定窗口小部件的正确大小。请检查以下解决方案,如果有问题请告知我。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class HomeScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _HomeScreen();
  }
}

class _HomeScreen extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return _buildPage();
  }

  Widget _buildPage() {
    return SafeArea(
      top: true,
      child: Scaffold(
        body: SingleChildScrollView (

            child: ConstrainedBox(
              constraints: BoxConstraints(maxHeight: MediaQuery.of(context).size.height),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Container(
                    margin: const EdgeInsets.only(top: 20.0),
                    height: MediaQuery
                        .of(context)
                        .size
                        .height * 0.2,
                    child: Align(
                      alignment: Alignment.center,
                      child: Text("Ravindra Kushwaha", style: TextStyle(
                          fontSize: 20.0
                      ),),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(4.0),
                    child: TextField(

                    ),
                  ),

                  Expanded(
                    child: _buildList(),
                  ),
                ],
              ),
            )
        ),
      ),
    );
  }

  Widget _buildList() {
    return ListView(
      children: <Widget>[
        ListTile(
            leading: Icon(Icons.ac_unit),
            title: Text('First'),
            trailing: Icon(Icons.arrow_forward_ios)

        ),
        ListTile(
            leading: Icon(Icons.ac_unit),
            title: Text('Seond'),
            trailing: Icon(Icons.arrow_forward_ios)

        ),
        ListTile(
            leading: Icon(Icons.ac_unit),
            title: Text('Third'),
            trailing: Icon(Icons.arrow_forward_ios)

        ),
      ],
    );
  }
}

然后将显示输出。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.