ListView Widget
- ๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์คํฌ๋กค ์์ ฏ
- ๋ช ์์ ์ผ๋ก children์ผ๋ก ๋๊น
- ์ ์ ์์ ์์ดํ ์ ๊ฐ์ง ๋์ ์ ํฉ
- ListView ๋ก๋ ์์ ์ ๋ชจ๋ child๊ฐ ์์ฑ
class ExplicitListConstructing extends StatelessWidget { @override Widget build(BuildContext context) { return ListView( padding: const EdgeInsets.all(8), children: [ HeaderTile(), PersonTile(people[0]), PersonTile(people[1]), PersonTile(people[2]), PersonTile(people[3]), PersonTile(people[4]), PersonTile(people[5]), ], ); } }
ListView({ Key? key, Axis scrollDirection = Axis.vertical, bool reverse = false, ScrollController? controller, bool? primary, ScrollPhysics? physics, bool shrinkWrap = false, EdgeInsetsGeometry? padding, this.itemExtent, bool addAutomaticKeepAlives = true, bool addRepaintBoundaries = true, bool addSemanticIndexes = true, double? cacheExtent, List children = const [], int? semanticChildCount, DragStartBehavior dragStartBehavior = DragStartBehavior.start, ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual, String? restorationId, Clip clipBehavior = Clip.hardEdge, }) : childrenDelegate = SliverChildListDelegate( children, addAutomaticKeepAlives: addAutomaticKeepAlives, addRepaintBoundaries: addRepaintBoundaries, addSemanticIndexes: addSemanticIndexes, ), super( key: key, scrollDirection: scrollDirection, reverse: reverse, controller: controller, primary: primary, physics: physics, shrinkWrap: shrinkWrap, padding: padding, cacheExtent: cacheExtent, semanticChildCount: semanticChildCount ?? children.length, dragStartBehavior: dragStartBehavior, keyboardDismissBehavior: keyboardDismissBehavior, restorationId: restorationId, clipBehavior: clipBehavior, );
- padding : children์ ์ฝ์ ํ ๊ณต๊ฐ => final EdgeInsetsGeometry? padding;
- scrollDirection : ์คํฌ๋กค ๋ฐฉํฅ ์ค์ ์์ฑ => final Axis scrollDirection;
IndexedWidgetBuilder : typedef IndexedWidgetBuilder = Widget Function(BuildContext context, int index);
=> itemBuilder์ ์์ฑ์
=> int index : child๊ฐ ListView ๋ช๋ฒ์งธ ์์์ธ์ง๋ฅผ ๋ํ๋
- ํ์ ํญ๋ชฉ ์๊ฐ ๋ง๊ฑฐ๋ ๋ฌดํ์ธ ๊ฒฝ์ฐ์ ์ ํฉ
- itemCount : ๋ฆฌ์คํธ์ ๊ทธ๋ ค์ง ์ด child ๊ฐ์
- itemBuilder : ๋ฆฌ์คํธ์ ๊ทธ๋ ค์ง ํญ๋ชฉ์ Lazyํ๊ฒ, ํด๋น child๊ฐ ํ๋ฉด์ ๋ณด์ฌ์ผ ํ ๋ ์์ฑ
class UsingBuilderListConstructing extends StatelessWidget { @override Widget build(BuildContext context) { return ListView.builder( padding: const EdgeInsets.all(8), itemCount: people.length + 1, itemBuilder: (BuildContext context, int index) { if (index == 0) return HeaderTile(); return PersonTile(people[index-1]); }, ); } }
ListView.builder({ Key? key, Axis scrollDirection = Axis.vertical, bool reverse = false, ScrollController? controller, bool? primary, ScrollPhysics? physics, bool shrinkWrap = false, EdgeInsetsGeometry? padding, this.itemExtent, required IndexedWidgetBuilder itemBuilder, int? itemCount, bool addAutomaticKeepAlives = true, bool addRepaintBoundaries = true, bool addSemanticIndexes = true, double? cacheExtent, int? semanticChildCount, DragStartBehavior dragStartBehavior = DragStartBehavior.start, ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual, String? restorationId, Clip clipBehavior = Clip.hardEdge, }) : assert(itemCount == null || itemCount >= 0), assert(semanticChildCount == null || semanticChildCount <= itemCount!), childrenDelegate = SliverChildBuilderDelegate( itemBuilder, childCount: itemCount, addAutomaticKeepAlives: addAutomaticKeepAlives, addRepaintBoundaries: addRepaintBoundaries, addSemanticIndexes: addSemanticIndexes, ), super( key: key, scrollDirection: scrollDirection, reverse: reverse, controller: controller, primary: primary, physics: physics, shrinkWrap: shrinkWrap, padding: padding, cacheExtent: cacheExtent, semanticChildCount: semanticChildCount ?? itemCount, dragStartBehavior: dragStartBehavior, keyboardDismissBehavior: keyboardDismissBehavior, restorationId: restorationId, clipBehavior: clipBehavior, );
#### >>> ListView.builder ์ฃผ์ ์์ฑ
- scrollDirection : ์คํฌ๋กค ๋ฐฉํฅ ์ค์ ์์ฑ => final Axis scrollDirection;
- padding : children์ ์ฝ์ ํ ๊ณต๊ฐ => final EdgeInsetsGeometry? padding;
- itemCount : child์ ๊ฐ์ => int? itemCount
- itemBuilder : child๋ค์ ์ด๋ป๊ฒ ๋ฐฐ์นํ ์ง ์ ํ๋ ์์ฑ => required IndexedWidgetBuilder itemBuilder
- ์คํฌ๋กค์ด ์ ์ฉ๋์ง ์๋ ๋ถ๋ถ์ ์ ์ํ๊ณ ์ถ์ ๋ ํ์ฉ
- ๋ ๊ฐ์ IndexedWidgetBuilder๋ฅผ ์ฌ์ฉ
- ์คํฌ๋กค ์ํฅ item - ๊ตฌ๋ถ ๊ธฐํธ - ์คํฌ๋กค ์ํฅ item ์์ผ๋ก ๊ณ์ ๋ฐ๋ณต
- ListView.seperated -> separatorBuilder ์ด๋ฐ ์์ผ๋ก ์ฝ๋ฉ
- separatorCount = itemCount-1
class UsingSeparateListConstructing extends StatelessWidget { @override Widget build(BuildContext context) { return ListView.separated( itemCount: people.length + 1, itemBuilder: (context, index) { if (index == 0) return HeaderTile(); return PersonTile(people[index - 1]); }, separatorBuilder: (context, index) { if (index == 0) return SizedBox.shrink(); return const Divider(); }, ); } }
ListView.separated({ Key? key, Axis scrollDirection = Axis.vertical, bool reverse = false, ScrollController? controller, bool? primary, ScrollPhysics? physics, bool shrinkWrap = false, EdgeInsetsGeometry? padding, required IndexedWidgetBuilder itemBuilder, required IndexedWidgetBuilder separatorBuilder, required int itemCount, bool addAutomaticKeepAlives = true, bool addRepaintBoundaries = true, bool addSemanticIndexes = true, double? cacheExtent, DragStartBehavior dragStartBehavior = DragStartBehavior.start, ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual, String? restorationId, Clip clipBehavior = Clip.hardEdge, }) : assert(itemBuilder != null), assert(separatorBuilder != null), assert(itemCount != null && itemCount >= 0), itemExtent = null, childrenDelegate = SliverChildBuilderDelegate( (BuildContext context, int index) { final int itemIndex = index ~/ 2; final Widget widget; if (index.isEven) { widget = itemBuilder(context, itemIndex); } else { widget = separatorBuilder(context, itemIndex); assert(() { if (widget == null) { throw FlutterError('separatorBuilder cannot return null.'); } return true; }()); } return widget; }, childCount: _computeActualChildCount(itemCount), addAutomaticKeepAlives: addAutomaticKeepAlives, addRepaintBoundaries: addRepaintBoundaries, addSemanticIndexes: addSemanticIndexes, semanticIndexCallback: (Widget _, int index) { return index.isEven ? index ~/ 2 : null; }, ), super( key: key, scrollDirection: scrollDirection, reverse: reverse, controller: controller, primary: primary, physics: physics, shrinkWrap: shrinkWrap, padding: padding, cacheExtent: cacheExtent, semanticChildCount: itemCount, dragStartBehavior: dragStartBehavior, keyboardDismissBehavior: keyboardDismissBehavior, restorationId: restorationId, clipBehavior: clipBehavior, );
- padding : children์ ์ฝ์ ํ ๊ณต๊ฐ => final EdgeInsetsGeometry? padding;
- scrollerDirection : ์คํฌ๋กค ๋ฐฉํฅ ์ค์ ์์ฑ => final Axis scrollDirection;
- itemBuilder : child๋ค์ ์ด๋ป๊ฒ ๋ฐฐ์นํ ์ง ์ ํ๋ ์์ฑ => required IndexedWidgetBuilder itemBuilder
- separatorBuilder : ๊ตฌ๋ถ์๋ฅผ ์ด๋ป๊ฒ ๊ทธ๋ฆด์ง ์ ํ๋ ์์ฑ => required IndexedWidgetBuilder separatorBuilder
- itemCount : child์ ๊ฐ์ => required int itemCount