Flutter에서 ListView 위젯은 여러 가지 방법으로 사용할 수 있습니다
기본적인 ListView를 생성하는 가장 간단한 방법은 ListView 클래스의 생성자에 항목 위젯의 리스트를 제공하는 것입니다. 각 항목은 일반적으로 ListTile 위젯을 사용하여 생성되지만 다른 위젯도 사용할 수 있습니다.
ListView(
children: <Widget>[
ListTile(
leading: Icon(Icons.map),
title: Text('Map'),
),
ListTile(
leading: Icon(Icons.photo_album),
title: Text('Album'),
),
ListTile(
leading: Icon(Icons.phone),
title: Text('Phone'),
),
],
)
ListView.builder는 보이는 항목만을 렌더링하는 "lazy loading" 기법을 사용하여 대용량 데이터를 효율적으로 처리합니다. 이 방법은 수천 개의 항목이 있는 리스트를 생성할 때 매우 유용합니다.
ListView.builder(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: Text('Item $index'),
);
},
)
이 경우, itemCount는 리스트 항목의 총 개수를, itemBuilder는 각 항목의 위젯을 생성하는 함수를 지정합니다.
ListView.separated는 항목과 항목 사이에 구분자를 추가하는 기능을 제공합니다. 이 방법은 리스트의 가독성을 향상시키는 데 유용합니다.
ListView.separated(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: Text('Item $index'),
);
},
separatorBuilder: (context, index) {
return Divider();
},
)
여기서 separatorBuilder는 각 항목 사이에 위치할 위젯을 생성하는 함수를 지정합니다.