listView 和 row view 创建问题 flutter
listView and row view creation problems flutter
我的 listView 在没有 Row 代码的情况下也能正常工作,但我需要在 ListView 的最后一个元素下放置一个文本字段和一个按钮。两者在同一高度。这就是我考虑使用 Row 的原因。但是如果我尝试将它写在我的 listView 下并启动应用程序,我什么也看不到,甚至 listView 也看不到。作为一个错误,它只报告给我:
The relevant error-causing widget was:
ListView file:///Users/apple/Desktop/Work%202021/toduba-merchant/lib/tabs/home_tab.dart:95:22
When the exception was thrown, this was the stack:
#0 RenderViewportBase._paintContents (package:flutter/src/rendering/viewport.dart:649:25)
#1 RenderViewportBase.paint (package:flutter/src/rendering/viewport.dart:641:7)
#2 RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2322:7)
#3 PaintingContext._repaintCompositedChild (package:flutter/src/rendering/object.dart:140:11)
#4 PaintingContext.repaintCompositedChild (package:flutter/src/rendering/object.dart:100:5)
...
The following RenderObject was being processed when the exception was fired: RenderViewport#ba090
... needs compositing
... parentData: (can use size)
... constraints: BoxConstraints(w=375.0, h=474.9)
... layer: OffsetLayer#32030 DETACHED
... engine layer: Null#007db
... offset: Offset(0.0, 0.0)
... size: Size(375.0, 474.9)
... axisDirection: down
... crossAxisDirection: right
... offset: ScrollPositionWithSingleContext#d8a7b(offset: 0.0, range: null..null, viewport: 474.9, ScrollableState, BouncingScrollPhysics -> RangeMaintainingScrollPhysics, IdleScrollActivity#f75ff, ScrollDirection.idle)
... anchor: 0.0
RenderObject: RenderViewport#ba090
needs compositing
parentData: (can use size)
constraints: BoxConstraints(w=375.0, h=474.9)
layer: OffsetLayer#32030 DETACHED
engine layer: Null#007db
offset: Offset(0.0, 0.0)
size: Size(375.0, 474.9)
axisDirection: down
crossAxisDirection: right
offset: ScrollPositionWithSingleContext#d8a7b(offset: 0.0, range: null..null, viewport: 474.9, ScrollableState, BouncingScrollPhysics -> RangeMaintainingScrollPhysics, IdleScrollActivity#f75ff, ScrollDirection.idle)
anchor: 0.0
... center child: RenderSliverPadding#7222a relayoutBoundary=up1 NEEDS-PAINT
... needs compositing
... parentData: paintOffset=Offset(0.0, 0.0) (can use size)
... constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: 0.0, remainingPaintExtent: 474.9, crossAxisExtent: 375.0, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: 474.9, remainingCacheExtent: 724.9, cacheOrigin: 0.0)
... geometry: null
... padding: EdgeInsets.all(20.0)
... textDirection: ltr
... child: RenderSliverList#9d74e relayoutBoundary=up2 NEEDS-PAINT
... needs compositing
... parentData: paintOffset=Offset(0.0, 0.0) (can use size)
... constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: 0.0, remainingPaintExtent: 454.9, crossAxisExtent: 335.0, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: 474.9, remainingCacheExtent: 704.9, cacheOrigin: 0.0)
... geometry: null
... currently live children: 0 to 3
... child with index 0: RenderIndexedSemantics#3b488 relayoutBoundary=up3 NEEDS-PAINT
... needs compositing
... parentData: index=0; layoutOffset=0.0 (can use size)
... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity)
... semantic boundary
... size: Size(335.0, 29.0)
... index: 0
... child: RenderRepaintBoundary#8cd41 relayoutBoundary=up4 NEEDS-PAINT
... needs compositing
... parentData: (can use size)
... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity)
... size: Size(335.0, 29.0)
... usefulness ratio: no metrics collected yet (never painted)
... child with index 1: RenderIndexedSemantics#c3e1f relayoutBoundary=up3 NEEDS-PAINT
... needs compositing
... parentData: index=1; layoutOffset=29.0 (can use size)
... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity)
... semantic boundary
... size: Size(335.0, 260.0)
... index: 1
... child: RenderRepaintBoundary#16c99 relayoutBoundary=up4 NEEDS-PAINT
... needs compositing
... parentData: (can use size)
... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity)
... size: Size(335.0, 260.0)
... usefulness ratio: no metrics collected yet (never painted)
... child with index 2: RenderIndexedSemantics#967b1 relayoutBoundary=up3 NEEDS-PAINT
... needs compositing
... parentData: index=2; layoutOffset=289.0 (can use size)
... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity)
... semantic boundary
... size: Size(335.0, 22.0)
... index: 2
... child: RenderRepaintBoundary#348b5 relayoutBoundary=up4 NEEDS-PAINT
... needs compositing
... parentData: (can use size)
... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity)
... size: Size(335.0, 22.0)
... usefulness ratio: no metrics collected yet (never painted)
... child with index 3: RenderIndexedSemantics#047b6 relayoutBoundary=up3 NEEDS-PAINT
... needs compositing
... parentData: index=3; layoutOffset=311.0 (can use size)
... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity)
... semantic boundary
... size: MISSING
... index: 3
... child: RenderRepaintBoundary#af13c relayoutBoundary=up4 NEEDS-PAINT
... needs compositing
... parentData: (can use size)
... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity)
... size: MISSING
... usefulness ratio: no metrics collected yet (never painted)
return Container(
color: appColors.primaryColor,
child: ListView(
primary: false,
children: [
// TITOLO
Align(
alignment: Alignment.center,
child: Container(
child: Text(
localization.showQR,
style: Theme.of(context).primaryTextTheme.headline5.copyWith(
color: appColors.green,
fontWeight: FontWeight.bold,
),
),
),
),
Align(
alignment: Alignment.center,
child: Container(
child: qrCodeHolder
),
),
Align(
alignment: Alignment.center,
child: Container(
child: AutoSizeText(
localization.home_subLabel,
textAlign: TextAlign.center,
maxLines: 1,
style: Theme.of(context).primaryTextTheme.headline5.copyWith(
color: appColors.green,
fontWeight: FontWeight.bold,
),
),
),
),
Positioned(
//top: size.height * .02,
//right: size.width * .02,
child: Row(
children: [
TextField(
inputFormatters: [
new LengthLimitingTextInputFormatter(7),
],
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
),
),
IconButton(
icon: Image.asset("assets/images/send.png"),
onPressed: () => showDialog(context: context, builder: (_) => PaymentDialog()),
),
Text("dfjbsdifbsdf"),
]
),
),
],
padding: EdgeInsets.all(20.0),
),
);
您需要将 TextField 包装在扩展小部件或宽度有限的容器中。
这是因为 Row 假定它可以采用最大宽度,而 TextField 和 TextFormField 小部件默认情况下会尝试采用可能的最大宽度。
将它们放在一起,你就会得到你遇到的错误。
用于TextField
,一行内,必须用Expanded
或Flexible
包起来。
Row(
children: [
Expanded(
child: TextField ()
)
])
我的 listView 在没有 Row 代码的情况下也能正常工作,但我需要在 ListView 的最后一个元素下放置一个文本字段和一个按钮。两者在同一高度。这就是我考虑使用 Row 的原因。但是如果我尝试将它写在我的 listView 下并启动应用程序,我什么也看不到,甚至 listView 也看不到。作为一个错误,它只报告给我:
The relevant error-causing widget was: ListView file:///Users/apple/Desktop/Work%202021/toduba-merchant/lib/tabs/home_tab.dart:95:22 When the exception was thrown, this was the stack: #0 RenderViewportBase._paintContents (package:flutter/src/rendering/viewport.dart:649:25) #1 RenderViewportBase.paint (package:flutter/src/rendering/viewport.dart:641:7) #2 RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2322:7) #3 PaintingContext._repaintCompositedChild (package:flutter/src/rendering/object.dart:140:11) #4 PaintingContext.repaintCompositedChild (package:flutter/src/rendering/object.dart:100:5) ... The following RenderObject was being processed when the exception was fired: RenderViewport#ba090 ... needs compositing ... parentData: (can use size) ... constraints: BoxConstraints(w=375.0, h=474.9) ... layer: OffsetLayer#32030 DETACHED ... engine layer: Null#007db ... offset: Offset(0.0, 0.0) ... size: Size(375.0, 474.9) ... axisDirection: down ... crossAxisDirection: right ... offset: ScrollPositionWithSingleContext#d8a7b(offset: 0.0, range: null..null, viewport: 474.9, ScrollableState, BouncingScrollPhysics -> RangeMaintainingScrollPhysics, IdleScrollActivity#f75ff, ScrollDirection.idle) ... anchor: 0.0 RenderObject: RenderViewport#ba090 needs compositing parentData: (can use size) constraints: BoxConstraints(w=375.0, h=474.9) layer: OffsetLayer#32030 DETACHED engine layer: Null#007db offset: Offset(0.0, 0.0) size: Size(375.0, 474.9) axisDirection: down crossAxisDirection: right offset: ScrollPositionWithSingleContext#d8a7b(offset: 0.0, range: null..null, viewport: 474.9, ScrollableState, BouncingScrollPhysics -> RangeMaintainingScrollPhysics, IdleScrollActivity#f75ff, ScrollDirection.idle) anchor: 0.0 ... center child: RenderSliverPadding#7222a relayoutBoundary=up1 NEEDS-PAINT ... needs compositing ... parentData: paintOffset=Offset(0.0, 0.0) (can use size) ... constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: 0.0, remainingPaintExtent: 474.9, crossAxisExtent: 375.0, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: 474.9, remainingCacheExtent: 724.9, cacheOrigin: 0.0) ... geometry: null ... padding: EdgeInsets.all(20.0) ... textDirection: ltr ... child: RenderSliverList#9d74e relayoutBoundary=up2 NEEDS-PAINT ... needs compositing ... parentData: paintOffset=Offset(0.0, 0.0) (can use size) ... constraints: SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: 0.0, remainingPaintExtent: 454.9, crossAxisExtent: 335.0, crossAxisDirection: AxisDirection.right, viewportMainAxisExtent: 474.9, remainingCacheExtent: 704.9, cacheOrigin: 0.0) ... geometry: null ... currently live children: 0 to 3 ... child with index 0: RenderIndexedSemantics#3b488 relayoutBoundary=up3 NEEDS-PAINT ... needs compositing ... parentData: index=0; layoutOffset=0.0 (can use size) ... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity) ... semantic boundary ... size: Size(335.0, 29.0) ... index: 0 ... child: RenderRepaintBoundary#8cd41 relayoutBoundary=up4 NEEDS-PAINT ... needs compositing ... parentData: (can use size) ... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity) ... size: Size(335.0, 29.0) ... usefulness ratio: no metrics collected yet (never painted) ... child with index 1: RenderIndexedSemantics#c3e1f relayoutBoundary=up3 NEEDS-PAINT ... needs compositing ... parentData: index=1; layoutOffset=29.0 (can use size) ... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity) ... semantic boundary ... size: Size(335.0, 260.0) ... index: 1 ... child: RenderRepaintBoundary#16c99 relayoutBoundary=up4 NEEDS-PAINT ... needs compositing ... parentData: (can use size) ... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity) ... size: Size(335.0, 260.0) ... usefulness ratio: no metrics collected yet (never painted) ... child with index 2: RenderIndexedSemantics#967b1 relayoutBoundary=up3 NEEDS-PAINT ... needs compositing ... parentData: index=2; layoutOffset=289.0 (can use size) ... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity) ... semantic boundary ... size: Size(335.0, 22.0) ... index: 2 ... child: RenderRepaintBoundary#348b5 relayoutBoundary=up4 NEEDS-PAINT ... needs compositing ... parentData: (can use size) ... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity) ... size: Size(335.0, 22.0) ... usefulness ratio: no metrics collected yet (never painted) ... child with index 3: RenderIndexedSemantics#047b6 relayoutBoundary=up3 NEEDS-PAINT ... needs compositing ... parentData: index=3; layoutOffset=311.0 (can use size) ... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity) ... semantic boundary ... size: MISSING ... index: 3 ... child: RenderRepaintBoundary#af13c relayoutBoundary=up4 NEEDS-PAINT ... needs compositing ... parentData: (can use size) ... constraints: BoxConstraints(w=335.0, 0.0<=h<=Infinity) ... size: MISSING ... usefulness ratio: no metrics collected yet (never painted)
return Container(
color: appColors.primaryColor,
child: ListView(
primary: false,
children: [
// TITOLO
Align(
alignment: Alignment.center,
child: Container(
child: Text(
localization.showQR,
style: Theme.of(context).primaryTextTheme.headline5.copyWith(
color: appColors.green,
fontWeight: FontWeight.bold,
),
),
),
),
Align(
alignment: Alignment.center,
child: Container(
child: qrCodeHolder
),
),
Align(
alignment: Alignment.center,
child: Container(
child: AutoSizeText(
localization.home_subLabel,
textAlign: TextAlign.center,
maxLines: 1,
style: Theme.of(context).primaryTextTheme.headline5.copyWith(
color: appColors.green,
fontWeight: FontWeight.bold,
),
),
),
),
Positioned(
//top: size.height * .02,
//right: size.width * .02,
child: Row(
children: [
TextField(
inputFormatters: [
new LengthLimitingTextInputFormatter(7),
],
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
),
),
IconButton(
icon: Image.asset("assets/images/send.png"),
onPressed: () => showDialog(context: context, builder: (_) => PaymentDialog()),
),
Text("dfjbsdifbsdf"),
]
),
),
],
padding: EdgeInsets.all(20.0),
),
);
您需要将 TextField 包装在扩展小部件或宽度有限的容器中。 这是因为 Row 假定它可以采用最大宽度,而 TextField 和 TextFormField 小部件默认情况下会尝试采用可能的最大宽度。
将它们放在一起,你就会得到你遇到的错误。
用于TextField
,一行内,必须用Expanded
或Flexible
包起来。
Row(
children: [
Expanded(
child: TextField ()
)
])