Flutter의 Sliver와 CustomScrollView는 스크롤 가능한 영역에서 고급 레이아웃을 구현하는 데 매우 유용한 도구입니다.
이 글에서는 Sliver의 개념과 다양한 Sliver 위젯을 활용하여 고급 레이아웃을 만드는 방법을 다뤄보겠습니다.
Sliver는 Flutter에서 "스크롤 가능한" 위젯을 뜻합니다. Sliver는 스크롤 가능한 화면의 한 부분을 정의하며, 스크롤 동작에 따라 크기나 배치를 동적으로 조정할 수 있습니다. 주로 CustomScrollView와 함께 사용됩니다.
CustomScrollView는 여러 Sliver를 조합하여 복잡한 스크롤 가능한 레이아웃을 생성합니다.
CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 200.0,
flexibleSpace: FlexibleSpaceBar(
title: Text('Sliver AppBar'),
background: Image.network(
'https://example.com/image.jpg',
fit: BoxFit.cover,
),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(
title: Text('Item #$index'),
),
childCount: 50,
),
),
],
)
스크롤 가능한 앱바를 만듭니다. 확장, 축소, 고정 등의 동작을 지원합니다.
SliverAppBar(
floating: true,
expandedHeight: 200.0,
flexibleSpace: FlexibleSpaceBar(
title: Text('Sliver AppBar'),
background: Image.network(
'https://example.com/image.jpg',
fit: BoxFit.cover,
),
),
)
스크롤 가능한 리스트를 구현합니다.
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(
title: Text('Item #$index'),
),
childCount: 20,
),
)
스크롤 가능한 그리드를 생성합니다.
SliverGrid(
delegate: SliverChildBuilderDelegate(
(context, index) => Container(
color: Colors.blue,
child: Center(child: Text('Item $index')),
),
childCount: 10,
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
)
화면의 남은 공간을 채우는 위젯입니다.
SliverFillRemaining(
child: Center(
child: Text('End of List'),
),
)
아래는 여러 Sliver 위젯을 조합하여 복잡한 레이아웃을 만드는 예제입니다.
import 'package:flutter/material.dart';
class SliverExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 200.0,
flexibleSpace: FlexibleSpaceBar(
title: Text('Sliver Example'),
background: Image.network(
'https://example.com/image.jpg',
fit: BoxFit.cover,
),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(
title: Text('List Item #$index'),
),
childCount: 10,
),
),
SliverGrid(
delegate: SliverChildBuilderDelegate(
(context, index) => Container(
margin: EdgeInsets.all(10),
color: Colors.amber,
child: Center(
child: Text('Grid Item $index'),
),
),
childCount: 6,
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
),
SliverFillRemaining(
child: Center(
child: Text('No More Content'),
),
),
],
),
);
}
}
Sliver와 CustomScrollView는 Flutter에서 스크롤 가능한 고급 레이아웃을 구현하는 데 필수적인 도구입니다.
다양한 Sliver 위젯과 CustomScrollView를 조합하여 더욱 생동감 있는 UI를 만들어 보세요.
다음 글에서는 Sliver의 애니메이션 효과와 동적 데이터 적용에 대해 다뤄보겠습니다.
Sliver와 애니메이션을 심화적으로 활용하는 고급 사례 (0) | 2025.01.01 |
---|---|
Sliver의 애니메이션 효과와 동적 데이터 적용 (1) | 2025.01.01 |
Flutter 반응형 레이아웃 디자인과 고급 레이아웃 패턴 (0) | 2025.01.01 |
Flutter 레이아웃 가이드: 기본 레이아웃 구성하기 (0) | 2025.01.01 |
Flutter 애니메이션 가이드: 기본 애니메이션 사용법 (0) | 2025.01.01 |