Last active
May 31, 2020 10:55
-
-
Save MariaMelnik/d5cdcdbd79081a06f0b5bdacfeb55c33 to your computer and use it in GitHub Desktop.
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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: MyHomePage(title: 'ReorderableListView issue demo',), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
final String title; | |
MyHomePage({Key key, this.title}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final List<int> data = List.generate(10, (index) => index); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
actions: [_buildRefreshBtn()], | |
), | |
body: Center( | |
child: ReorderableListView( | |
children: _buildTiles(), | |
onReorder: (_, __) {}, | |
) | |
), | |
); | |
} | |
Widget _buildRefreshBtn() { | |
return Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 20.0), | |
child: IconButton( | |
icon: Icon(Icons.refresh), | |
onPressed: (){setState(() {});} | |
), | |
); | |
} | |
List<Widget> _buildTiles() { | |
List<Widget> tiles = data.map((int val) { | |
return MyListTile( | |
key: ValueKey(val), | |
data: val, | |
title: "tile $val", | |
); | |
}).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> { | |
Stream<int> _msStream; | |
int _curMs; | |
@override | |
void initState() { | |
super.initState(); | |
print("init: ${widget.title}"); | |
_curMs = 0; | |
_msStream = Stream.periodic(const Duration(milliseconds: 100), (ms) => ms); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
padding: const EdgeInsets.symmetric(vertical: 20.0, horizontal: 30.0), | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: [ | |
Text("${widget.title}"), | |
StreamBuilder<int>( | |
stream: _msStream, | |
initialData: _curMs, | |
builder: (_, AsyncSnapshot<int> snapshot) { | |
if (!snapshot.hasData) return Center(child: CircularProgressIndicator()); | |
String ms = "${snapshot.data} ms"; | |
return Text(ms); | |
} | |
) | |
], | |
), | |
); | |
} | |
void dispose(){ | |
print("dispose: ${widget.title}"); | |
super.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment