実現したいこと
Flutterで ListView.builder の中に さらに、ListView.builder を入れたいです!
Flutterでカレンダーのようなシステムを作っています。
scrollDirection: Axis.horizontal,で横並びに指定したListView.builder のそれぞれの中に 縦並びのListView.builderを入れようと思っています!
追記: 解決しました!
ta.fu さんのコードをもとにして GridView の中に ListView を入れることで解決しました。
dart
1import 'package:flutter/material.dart'; 2import 'package:flutter/rendering.dart'; 3 4void main() => runApp(const MyApp()); 5class MyApp extends StatelessWidget { 6 const MyApp({super.key}); 7 8 // This widget is the root of your application. 9 10 Widget build(BuildContext context) { 11 return MaterialApp(//return MaterialAppはアプリ内で1つ 12 home: calender() 13 ); 14 } 15 16} 17 18class calender extends StatefulWidget { 19 const calender({Key? key}) : super(key: key); 20 21 22 State<calender> createState() => calenderState(); 23} 24List<String> days = ["月","火","水","木","金","土","日"]; 25class calenderState extends State<calender> { 26 27 Widget build(BuildContext context) { 28 return Scaffold( 29 body:Column( 30 //Expandedの親はColumnかRow 31 children: [ 32 Expanded( 33 child: Row( 34 children: [ 35 const Text("Row"), 36 Expanded( 37 child: GridView.builder( 38 itemCount: 7, 39 itemBuilder: (context, index) { 40 String today = days[index]; 41 return Column( 42 children: [ 43 Text(today), 44 Expanded(child: ListView.builder( 45 itemCount: 24, 46 itemBuilder: (context,index){ 47 return Container( 48 child: Text("$index:00"), 49 ); 50 }),) 51 ], 52 ); 53 }, 54 gridDelegate: 55 const SliverGridDelegateWithFixedCrossAxisCount( 56 crossAxisCount: 7, 57 ), 58 ), 59 ), 60 ], 61 ), 62 ), 63 ], 64 ) 65 66 ); 67 68 } 69}
発生している問題・エラーメッセージ
EDS-COMPOSITING-BITS-UPDATE //の後に以下の内容と同じ内容のエラーがいくつも出ています。そのため長すぎるエラーになるためその同じ中の一つを抜粋させてもらいます。 The following assertion was thrown during performLayout(): RenderBox was not laid out: RenderPointerListener#068ee relayoutBoundary=up15 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': Failed assertion: line 2001 pos 12: 'hasSize' Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause. In either case, please report this assertion by filing a bug on GitHub: https://github.com/flutter/flutter/issues/new?template=2_bug.md The relevant error-causing widget was: ListView ListView:file:///C:/Users/inohi/StudioProjects/test6/lib/main.dart:41:42 When the exception was thrown, this was the stack: #2 RenderBox.size (package:flutter/src/rendering/box.dart:2001:12) #3 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:121:21) #4 RenderObject.layout (package:flutter/src/rendering/object.dart:2135:7) #5 RenderBox.layout (package:flutter/src/rendering/box.dart:2418:11) #6 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14) #7 RenderObject.layout (package:flutter/src/rendering/object.dart:2135:7) #8 RenderBox.layout (package:flutter/src/rendering/box.dart:2418:11) #9 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:120:14) #10 RenderObject.layout (package:flutter/src/rendering/object.dart:2135:7) ---# は省略させてもらいます。 (elided 6 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer, and dart:async-patch) The following RenderObject was being processed when the exception was fired: RenderSemanticsGestureHandler#a73c0 relayoutBoundary=up14 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE ... parentData: <none> (can use size) ... constraints: BoxConstraints(0.0<=w<=Infinity, h=681.0) ... size: MISSING ... behavior: opaque ... gestures: <none> RenderObject: RenderSemanticsGestureHandler#a73c0 relayoutBoundary=up14 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE parentData: <none> (can use size) constraints: BoxConstraints(0.0<=w<=Infinity, h=681.0) size: MISSING behavior: opaque gestures: <none> ... child: RenderPointerListener#068ee relayoutBoundary=up15 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE ... parentData: <none> (can use size) ... constraints: BoxConstraints(0.0<=w<=Infinity, h=681.0) ... size: MISSING ... behavior: opaque ... listeners: down, panZoomStart ... child: RenderSemanticsAnnotations#f1cb4 relayoutBoundary=up16 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE ... parentData: <none> (can use size) ... constraints: BoxConstraints(0.0<=w<=Infinity, h=681.0) ... size: MISSING ... child: RenderIgnorePointer#dd22f relayoutBoundary=up17 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE ... parentData: <none> (can use size) ... constraints: BoxConstraints(0.0<=w<=Infinity, h=681.0) ... size: MISSING ... ignoring: false ... ignoringSemantics: false ... child: RenderViewport#b5460 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE ... needs compositing ... parentData: <none> (can use size) ... constraints: BoxConstraints(0.0<=w<=Infinity, h=681.0) ... size: MISSING ... axisDirection: down ... crossAxisDirection: right ... offset: ScrollPositionWithSingleContext#f1581(offset: 0.0, range: null..null, viewport: null, ScrollableState, AlwaysScrollableScrollPhysics -> ClampingScrollPhysics -> RangeMaintainingScrollPhysics, IdleScrollActivity#37b94, ScrollDirection.idle) ... anchor: 0.0
エラーが起きたソースコード
dart
1import 'package:flutter/material.dart'; 2import 'package:flutter/rendering.dart'; 3 4void main() => runApp(const MyApp()); 5class MyApp extends StatelessWidget { 6 const MyApp({super.key}); 7 8 // This widget is the root of your application. 9 10 Widget build(BuildContext context) { 11 return MaterialApp(//return MaterialAppはアプリ内で1つ 12 home: calender() 13 ); 14 } 15 16} 17 18class calender extends StatefulWidget { 19 const calender({Key? key}) : super(key: key); 20 21 22 State<calender> createState() => calenderState(); 23} 24 25class calenderState extends State<calender> { 26 27 Widget build(BuildContext context) { 28 return Scaffold( 29 body:Column(//Expandedの親はColumnかRow 30 children: [ 31 Expanded(child: Row( 32 children: [ 33 Text("Row"), 34 Expanded(child: 35 ListView.builder( 36 scrollDirection: Axis.horizontal, 37 itemCount: 7, 38 itemBuilder: (context,index){ 39 return Column(children: [ 40 Expanded( 41 child:ListView.builder( 42 itemCount:1, 43 itemBuilder: (context,index){ 44 return Container( 45 child:Text("縦の内容"), 46 ); 47 }) 48 ) 49 ],); 50 } 51 ),), 52 53 ], 54 ),), 55 ], 56 ) 57 58 ); 59 60 } 61}
試したこと
エラーにFailed assertion: line 2001 pos 12: 'hasSize'書いてあったので調べるとエラーの原因になっている要素をExpanded()で囲むと治ると書いてあったので、2つのListView.BuilderをExpanded()で囲んでみました。下の階層のほう(scrollDirection: Axis.horizontal,がない縦並び)のListView.Builderを削除し、横並びのListView.builderのみにするとエラーなく動作しています。
補足情報(FW/ツールのバージョンなど)
Flutter version 3.3.0 を使用しています。
回答よろしくお願いします!

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。