Skip to content

Instantly share code, notes, and snippets.

@davidmartos96
Created December 10, 2021 09:03
Show Gist options
  • Save davidmartos96/8811db2c38680835a929811cc12eaf29 to your computer and use it in GitHub Desktop.
Save davidmartos96/8811db2c38680835a929811cc12eaf29 to your computer and use it in GitHub Desktop.
#26072 with slivers, renders all slivers
import 'dart:math';
import 'package:flutter/material.dart';
const numLists = 250;
const numberOfItemsPerList = 10;
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: 'Flutter Demo',
theme: ThemeData(),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<SliverList> innerLists = [];
@override
void initState() {
super.initState();
for (int i = 0; i < numLists; i++) {
final _innerList = <ColorRow>[];
for (int j = 0; j < numberOfItemsPerList; j++) {
_innerList.add(ColorRow("List $i - Nested $j"));
}
innerLists.add(
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) => _innerList[index],
childCount: numberOfItemsPerList,
),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [...innerLists],
),
);
}
}
class ColorRow extends StatefulWidget {
const ColorRow(this.rowId, {Key? key}) : super(key: key);
final String rowId;
@override
State createState() => ColorRowState();
}
class ColorRowState extends State<ColorRow> {
Color? color;
@override
void initState() {
super.initState();
color = randomColor();
}
@override
Widget build(BuildContext context) {
print('Building ColorRowState ${widget.rowId}');
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
randomColor(),
randomColor(),
],
),
),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(height: 50, width: 50, color: Colors.white),
),
Flexible(
child: Column(
children: const <Widget>[
Padding(
padding: EdgeInsets.all(8),
child: Text('I\'m a widget!', style: TextStyle(color: Colors.white)),
),
],
),
),
],
),
);
}
}
Color randomColor() => Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment