Created
May 11, 2020 11:57
-
-
Save kungfuslippers/fcae96675fb76c10f7bb5051b66ed87e to your computer and use it in GitHub Desktop.
ListView Performace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp( | |
itemsA: List<String>.generate(250, (i) => "Item $i"), | |
itemsB: List<String>.generate(250, (i) => "Item $i"))); | |
} | |
class MyApp extends StatefulWidget { | |
final List<String> itemsA; | |
final List<String> itemsB; | |
MyApp({Key key, @required this.itemsA, this.itemsB}) : super(key: key); | |
@override | |
MyAppState createState() => MyAppState(); | |
} | |
class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin { | |
TabController _tabController; | |
final Key listKeyA = PageStorageKey('listA'); | |
final Key listKeyB = PageStorageKey('listB'); | |
@override | |
void initState() { | |
super.initState(); | |
_tabController = TabController(vsync: this, length: 2); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final title = 'Long List'; | |
return MaterialApp( | |
title: title, | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
), | |
body: TabBarView(controller: _tabController, children: [ | |
ListWidget(key: listKeyA, items: widget.itemsA), | |
ListWidget(key: listKeyB, items: widget.itemsB), | |
]), | |
bottomNavigationBar: Container( | |
color: Colors.blue, | |
child: TabBar( | |
controller: _tabController, | |
indicatorColor: Colors.white, | |
tabs: <Widget>[Tab(text: "ListA"), Tab(text: "ListB")], | |
), | |
), | |
)); | |
} | |
} | |
class ListWidget extends StatefulWidget { | |
final List<String> items; | |
const ListWidget({ | |
Key key, | |
@required this.items, | |
}) : super(key: key); | |
@override | |
ListWidgetState createState() => ListWidgetState(); | |
} | |
class ListWidgetState extends State<ListWidget> { | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemCount: widget.items.length, | |
itemBuilder: (context, index) { | |
return ListTile( | |
title: Text('${widget.items[index]}'), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment