Geo*_*rge 35
对于像我这样的人,想知道什么是显示空窗口小部件的“正确方法”-官方材料代码库使用此方法:
Widget build(BuildContext context) {
return SizedBox.shrink();
}
Run Code Online (Sandbox Code Playgroud)
SizedBox.shrink()是与背景不同Container或Material没有背景或任何装饰的小部件。如果不受父约束的影响,它会将自己的大小调整为可能的最小区域。
Rah*_*dik 27
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
);
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以简单地返回一个空的Container并避免使用Scaffold完全。但是如果这是您应用程序中唯一的主要小部件,这将导致黑屏color,Container如果您想防止黑色背景,您可以设置 的属性。
例子:
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white // This is optional
);
}
}
Run Code Online (Sandbox Code Playgroud)
Cop*_*oad 20
它有很多可能的解决方案。喜欢
Widget build(context) => Container();
Run Code Online (Sandbox Code Playgroud)Widget build(context) => SizedBox();
Run Code Online (Sandbox Code Playgroud)Widget build(context) => Scaffold();
Run Code Online (Sandbox Code Playgroud)Mam*_*ezo 14
这可能为时已晚,但所有这些解决方案都不适合某些场景,例如玩PopupMenuItems或影响 UI 渲染!
空安全更新
\n [\n .\n .\n .\n if(condition)...[//Conditionally widget(s) here\n Something(...),\n ],\n .\n .\n .\n],\nRun Code Online (Sandbox Code Playgroud)\n解决方案是在传递给渲染组件之前删除空项:
\nColumn(\n children: [\n Text(\'Title\'),\n name != \'\' \n ? Text(name) //show name\n : null // just pass a null we will filter it in next line!\n ].where((e) => e != null).toList()// Filter list and make it List again!\n)\nRun Code Online (Sandbox Code Playgroud)\n这样,我们可以有很多null,并且UI不会受到任何空的影响Widget。
PopupMenuButton我们无法传递 SizedBox 的示例:
PopupMenuButton(\n icon: Icon(Icons.add),\n itemBuilder: (context) => [\n PopupMenuItem(\n child: Row(children:[ Icon(Icons.folder), Text(\'So something\')]),\n value: \'do.something\',\n ),\n 1 > 2 //\xe2\x9a\xa0\xef\xb8\x8f A false condition\n ? PopupMenuItem(\n child: Row(children: [ Icon(Icons.file_upload), Text(\'\xe2\x9a\xa0\xef\xb8\x8fNo way to display \')]),\n \'no.way.to.display\',\n )\n : null,// \xe2\x9a\xa0\xef\xb8\x8f Passing null\n PopupMenuItem(\n child: Row(children: [ Icon(Icons.file_upload), Text(\'Do something else\')]),\n \'do.something.else\',\n )\n ].where((e) => e != null).toList(),//\xe2\x84\xb9\xef\xb8\x8f Removing null items \n onSelected: (item) {}\n)\n\nRun Code Online (Sandbox Code Playgroud)\n这可以用作 APIextension:
extension NotNulls on List {\n ///Returns items that are not null, for UI Widgets/PopupMenuItems etc.\n notNulls() {\n return where((e) => e != null).toList();\n }\n}\n\n//Usage:\nPopupMenuButton(\n icon: Icon(Icons.add),\n itemBuilder: (context) => [\n PopupMenuItem(\n child: Row(children:[ Icon(Icons.folder), Text(\'So something\')]),\n value: \'do.something\',\n ),\n 1 > 2 //\xe2\x9a\xa0\xef\xb8\x8f A false condition\n ? PopupMenuItem(\n child: Row(children: [ Icon(Icons.file_upload), Text(\'\xe2\x9a\xa0\xef\xb8\x8fNo way to display \')]),\n \'no.way.to.display\',\n )\n : null,// \xe2\x9a\xa0\xef\xb8\x8f Passing null\n PopupMenuItem(\n child: Row(children: [ Icon(Icons.file_upload), Text(\'Do something else\')]),\n \'do.something.else\',\n )\n ].notNulls(),//\xe2\x84\xb9\xef\xb8\x8f Removing null items \n onSelected: (item) {}\n)\n\n\nRun Code Online (Sandbox Code Playgroud)\n
当构建函数返回 null 时,flutter 的错误消息是:
构建函数绝不能返回 null。要返回导致建筑小部件填充可用空间的空白空间,请返回“new Container()”。要返回占用尽可能少空间的空白空间,请返回“新容器(宽度:0.0,高度:0.0)”。
容器 = 166,173 毫秒
SizedBox.shrink = 164,523 毫秒
DIY
main() async {
testWidgets('test', (WidgetTester tester) async {
await tester.pumpWidget( Container());
final Stopwatch timer = new Stopwatch()..start();
for (int index = 0; index < 5000000; index += 1) {
await tester.pump();
}
timer.stop();
debugPrint('Time taken: ${timer.elapsedMilliseconds}ms');
});
}
Run Code Online (Sandbox Code Playgroud)
建议不显示任何内容的小部件是使用SizedBox。
SizedBox(
width: 200.0,
height: 300.0,
child: const Card(child: Text('Hello World!')),
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8349 次 |
| 最近记录: |