Skip to content

Instantly share code, notes, and snippets.

@artem-zaitsev
Created October 22, 2021 15:25
Show Gist options
  • Save artem-zaitsev/23d88273015afeb54c3844f9e3d059de to your computer and use it in GitHub Desktop.
Save artem-zaitsev/23d88273015afeb54c3844f9e3d059de to your computer and use it in GitHub Desktop.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
MyApp(
items: List<String>.generate(10000, (i) => 'Item $i'),
),
);
}
class MyApp extends StatelessWidget {
final List<String> items;
const MyApp({Key? key, required this.items}) : super(key: key);
@override
Widget build(BuildContext context) {
const title = 'Long List';
return MaterialApp(
title: title,
home: StatefulList(items: items),
);
}
}
class StatefulList extends StatefulWidget {
final List items;
const StatefulList({Key? key, required this.items}) : super(key: key);
@override
_StatefulListState createState() => _StatefulListState();
}
class _StatefulListState extends State<StatefulList> {
bool some = false;
@override
Widget build(BuildContext context) {
const title = 'Long List';
return Scaffold(
appBar: AppBar(
title: const Text(title),
),
floatingActionButton: FloatingActionButton(
onPressed: _toggle,
),
body: ListView.builder(
itemCount: widget.items.length,
itemBuilder: (context, index) {
return StatefulItem(child: some ? Text('$index') : Text('test'));
},
),
);
}
void _toggle() {
setState(() {
some = !some;
});
}
}
class StatefulItem extends StatefulWidget {
final Widget child;
const StatefulItem({Key? key, required this.child}) : super(key: key);
@override
_StatefulItemState createState() => _StatefulItemState();
}
class _StatefulItemState extends State<StatefulItem> {
@override
Widget build(BuildContext ctx) => ListTile(
title: widget.child,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment