SliverAppBar 같은거를 쓸 때
CustomScrollView와 SliverList를 쓰게 될 일이 있다.
기존 ListView.builder 에서 변환하는 순서를 설명한다.
요약
import 'package:flutter/material.dart';
class SliverSliverApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SliverSliver(),
),
);
}
}
class SliverSliver extends StatelessWidget {
final data = "abcdefghi".split('');
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
return Container(
height: 50.0,
color: Colors.red[(index) * 100],
child: Center(
child: Text(
data[index],
style: TextStyle(
fontSize: 24.0,
color: Colors.black87,
fontWeight: FontWeight.w300),
),
),
);
},
);
}
}
바뀐 코드
import 'package:flutter/material.dart';
class SliverSliverApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SliverSliver(),
),
);
}
}
class SliverSliver extends StatelessWidget {
final data = "abcdefghi".split('');
@override
Widget build(BuildContext context) {
return SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return Container(
height: 50.0,
color: Colors.red[(index) * 100],
child: Center(
child: Text(
data[index],
style: TextStyle(
fontSize: 24.0,
color: Colors.black87,
fontWeight: FontWeight.w300),
),
),
);
},
childCount: data.length,
),
);
}
}