收缩包裹特性在颤振中起什么作用? [英] What does the shrink wrap property do in flutter?

查看:100
本文介绍了收缩包裹特性在颤振中起什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flutter的新手,非常渴望学习这项技术.我无法理解列表视图中的收缩包装属性的工作.我听不懂Flutter文档.有人可以帮忙吗?预先感谢.

I am new to Flutter and very eager to learn this technology. I cannot understand the work of shrink wrap property in list view. I couldn't understand the flutter documentation. Can somebody help? Thanks in advance.

推荐答案

通常ListView(以及GridViewPageViewCustomScrollView)尝试填充父元素给定的所有可用空间,即使列表项需要较少的空间.

Usually a ListView (as well as GridView, PageView and CustomScrollView) tries to fill all the available space given by the parent element, even when the list items would require less space.

使用shrinkWrap: true,您可以更改此行为,以使ListView仅占用其所需的空间(当有更多项目时,它仍会滚动).

With shrinkWrap: true, you can change this behavior so that the ListView only occupies the space it needs (it will still scroll when there more items).

看看这个例子:

import 'package:flutter/material.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: Center(
          child: Container(
            margin: EdgeInsets.all(32),
            decoration: BoxDecoration(border: Border.all(color: Colors.red)),
            child: ListView(
              shrinkWrap: false,
              children: <Widget>[
                ListTile(title: Text('Item 1')),
                ListTile(title: Text('Item 2')),
                ListTile(title: Text('Item 3')),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

使用shrinkWrap: false:

使用shrinkWrap: true:

您可以在AlertDialog s中使用它:当只有几个项目时,使对话框尽可能小.如果有很多项目,请填充屏幕高度并使列表可滚动:

You can use this in AlertDialogs: When there are only a few items, make the dialog as small as possible. When there are many items, fill the screen height and make the list scrollable:

这篇关于收缩包裹特性在颤振中起什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆