大家好,我正在尝试构建一个可重用的 SliverList,我只是将 sliver 小部件作为列表传递到其中以显示,并加倍用于填充,但目前出现此错误
我正在尝试构建的可重用小部件的代码
class ScrollScreen extends StatelessWidget {
ScrollScreen({
@required this.pageTitle,
@required this.pageImage,
@required this.widgets,
this.color = Colors.white,
});
final String pageTitle;
final String pageImage;
final List<Widget> widgets;
final Color color;
@override
Widget build(BuildContext context) {
return CustomScrollView(physics: const BouncingScrollPhysics(), slivers: [
SliverBar(title: pageTitle, image: pageImage),
SliverListLayout(widgets: widgets),
]);
}
SliverListLayout 类
class SliverListLayout extends StatelessWidget {
final double paddingTB;
final double paddingLR;
final List<Widget> widgets;
SliverListLayout({
@required this.widgets,
this.paddingTB,
this.paddingLR,
});
@override
Widget build(BuildContext context) {
return SliverList(
delegate: SliverChildListDelegate([
...addPadding(widgets, paddingTB, paddingLR), //add padding wraps widgets in a padding widget and returns them,, not the issue works fine elsewhere
]),
);
}
} 堆栈跟踪:
The following NoSuchMethodError was thrown building
NotificationListener<KeepAliveNotification>:
The method '>=' was called on null.
Receiver: null
Tried calling: >=(0.0)
The relevant error-causing widget was
SliverList
lib\…\custom_widgets\sliverlist_layout.dart:15
When the exception was thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 EdgeInsetsGeometry.isNonNegative
package:flutter/…/painting/edge_insets.dart:52
#2 new RenderPadding
package:flutter/…/rendering/shifted_box.dart:107
#3 Padding.createRenderObject
package:flutter/…/widgets/basic.dart:1652
#4 RenderObjectElement.mount
颤振医生:
PS C:\Users\Usuario\Desktop\AppName> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Versión 10.0.19042.746], locale es-ES)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[!] Android Studio (version 4.1.0)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code (version 1.52.1)
[√] Connected device (1 available)
! Doctor found issues in 2 categories.
PS C:\Users\Usuario\Desktop\AppName>
添加填充
List<Widget> addPadding(List<Widget> widgets,
[double paddingTB = 25.0, double paddingLR = 25.0]) {
for (int i = 0; i < widgets.length; i++) {
widgets[i] = Padding(
padding: EdgeInsets.fromLTRB(
paddingLR,
paddingTB,
paddingLR,
paddingTB,
),
child: widgets[i],
);
}
return widgets;
}
我粘贴了 SliverListLayout 类中的代码(没有两个额外的参数),一切正常。SliverBar 还返回一个 SliverAppBar,它工作正常。非常感谢任何帮助。