Created
May 25, 2020 11:47
-
-
Save MariaMelnik/049c56e2d30c40209c0428cd7b2df3de to your computer and use it in GitHub Desktop.
flutter: ReorderableListView issue demo (rebuilds children every time even if Key-Type of children wasn't changed)
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/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: MyHomePage(title: 'ReorderableListView issue demo'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
List<int> myData = List.generate(10, (index) => index); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: ReorderableListView( | |
children: _buildTiles(), | |
onReorder: (_, __) {}, | |
), | |
), | |
floatingActionButton: _buildReorderButton(), | |
); | |
} | |
Widget _buildReorderButton() { | |
return FloatingActionButton( | |
child: Icon(Icons.refresh), | |
onPressed: () => setState(() {}), | |
); | |
} | |
List<Widget> _buildTiles() { | |
List<Widget> tiles = myData.map((int i) => MyListTile(i, "tile $i", key: ValueKey(i),)).toList(); | |
return tiles; | |
} | |
} | |
class MyListTile extends StatefulWidget { | |
final int data; | |
final String title; | |
MyListTile(this.data, this.title, {Key key}): super(key: key); | |
@override | |
_MyListTileState createState() => _MyListTileState(); | |
} | |
class _MyListTileState extends State<MyListTile> { | |
int _curData; | |
@override | |
void initState() { | |
super.initState(); | |
_curData = widget.data ?? 0; | |
} | |
@override | |
Widget build(BuildContext context) { | |
print("build ${widget.data}"); | |
return Container( | |
padding: const EdgeInsets.symmetric(vertical: 20.0, horizontal: 30.0), | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: [ | |
Text("${widget.title}. Cur value is: $_curData"), | |
IconButton(icon: Icon(Icons.add), onPressed: _increment) | |
], | |
), | |
); | |
} | |
void _increment() { | |
setState(() => _curData ++); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment