收缩包装属性在颤振中有何作用? [英] What does the shrink wrap property do in flutter?

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

问题描述

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

解决方案

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

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

看看这个例子:

import 'package:flutter/material.dart';void main() =>运行应用程序(应用程序());类 App 扩展了 StatelessWidget {@覆盖小部件构建(BuildContext 上下文){返回 MaterialApp(主页: 脚手架(应用栏:应用栏(),身体:中心(孩子:容器(边距:EdgeInsets.all(32),装饰:BoxDecoration(边框:Border.all(颜色:Colors.red)),孩子:列表视图(收缩包装:假,孩子们:<小部件>[ListTile(title: Text('Item 1')),ListTile(title: Text('Item 2')),ListTile(title: Text('Item 3')),],),),),),);}}

使用 shr​​inkWrap: false:

使用 shr​​inkWrap: true:

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

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.

解决方案

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.

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).

Take a look at this example:

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')),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

With shrinkWrap: false:

With shrinkWrap: true:

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天全站免登陆