Skip to content

Instantly share code, notes, and snippets.

@petermnt
Last active Aug 25, 2022
Embed
What would you like to do?
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Slivers',
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.system,
home: const Page(),
);
}
}
class Page extends StatelessWidget {
const Page({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => BuildNotifyingWidget(
"Sliver list builder",
child: Text("Sliver list builder $index"),
),
childCount: 50,
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => BuildNotifyingWidget(
"Sliver list builder 2",
child: Text("Sliver list builder 2 $index"),
),
childCount: 50,
),
),
SliverList(
delegate: SliverChildListDelegate(
List.generate(
50,
(index) => BuildNotifyingWidget(
"Sliver list",
child: Text("Sliver list $index"),
),
).toList(),
),
),
SliverToBoxAdapter(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: List.generate(
50,
(index) => BuildNotifyingWidget(
"SliverToBoxAdapter",
child: Text("SliverToBoxAdapter $index"),
),
),
),
)
],
),
);
}
}
class BuildNotifyingWidget extends StatelessWidget {
final String prefix;
final Widget child;
BuildNotifyingWidget(this.prefix, {required this.child, Key? key})
: super(key: key) {
print("creating object from $prefix ($child)");
}
@override
Widget build(BuildContext context) {
print("building from $prefix ($child)");
return Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
height: 50,
child: child,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment