收缩包装属性在颤振中有何作用? [英] What does the shrink wrap property do in flutter?
问题描述
我是 Flutter 的新手,非常渴望学习这项技术.我无法理解列表视图中的收缩包装属性的工作.我无法理解颤振文档.有人可以帮忙吗?提前致谢.
通常是一个 ListView
(以及 GridView
、PageView
和 CustomScrollView
) 尝试填充父元素给定的所有可用空间,即使列表项需要较少的空间.
使用 shrinkWrap: 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')),],),),),),);}}
使用 shrinkWrap: false
:
使用 shrinkWrap: true
:
您可以在AlertDialog
s 中使用它:当只有几个项目时,使对话框尽可能小.当有很多项目时,填充屏幕高度并使列表可滚动:
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 AlertDialog
s: 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屋!