sized_box_shrink_expand
Use 'SizedBox.{0}' to avoid needing to specify the 'height' and 'width'.
Description
#The analyzer produces this diagnostic when a SizedBox
constructor
invocation specifies the values of both height
and width
as either
0.0
or double.infinity
.
Examples
#The following code produces this diagnostic because both the height
and
width
are 0.0
:
dart
import 'package:flutter/material.dart';
Widget build() {
return SizedBox(
height: 0.0,
width: 0.0,
child: const Text(''),
);
}
The following code produces this diagnostic because both the height
and
width
are double.infinity
:
dart
import 'package:flutter/material.dart';
Widget build() {
return SizedBox(
height: double.infinity,
width: double.infinity,
child: const Text(''),
);
}
Common fixes
#If both are 0.0
, then use SizedBox.shrink
:
dart
import 'package:flutter/material.dart';
Widget build() {
return SizedBox.shrink(
child: const Text(''),
);
}
If both are double.infinity
, then use SizedBox.expand
:
dart
import 'package:flutter/material.dart';
Widget build() {
return SizedBox.expand(
child: const Text(''),
);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。