Last active
February 9, 2020 08:24
-
-
Save t-artikov/c20abd67d2cde74263ba1b49b368e062 to your computer and use it in GitHub Desktop.
Flutter rebuild optimization
This file contains hidden or 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/material.dart'; | |
void main() { | |
final app = MaterialApp( | |
home: MyHomePage(), | |
); | |
runApp(app); | |
} | |
class Item { | |
const Item(this.title, this.subtitle); | |
final String title; | |
final String subtitle; | |
static Item createByIndex(int index) { | |
return Item('Item $index', 'Subtitle $index'); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final items = List.generate(5, Item.createByIndex); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.white, | |
appBar: AppBar( | |
title: Text('Rebuild Optimization'), | |
), | |
body: ListView.builder( | |
itemCount: items.length, | |
itemBuilder: (context, index) { | |
return ItemView(items[index]); | |
}, | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () { | |
setState(() { | |
items.add(Item.createByIndex(items.length)); | |
}); | |
}, | |
), | |
); | |
} | |
} | |
class ItemView extends StatelessWidget { | |
const ItemView(this.item); | |
final Item item; | |
@override | |
// ignore: invalid_override_of_non_virtual_member | |
bool operator ==(Object other) { | |
if (identical(this, other)) return true; | |
if (other is ItemView) { | |
return identical(item, other.item); | |
} | |
return false; | |
} | |
@override | |
// ignore: invalid_override_of_non_virtual_member | |
int get hashCode => item.hashCode; | |
@override | |
Widget build(BuildContext context) { | |
print('build ${item.title}'); | |
return ListTile( | |
title: Text(item.title), | |
subtitle: Text(item.subtitle), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment