Created
June 7, 2020 10:51
-
-
Save MariaMelnik/893aa6c1ca65302d52e4812fdc2c1837 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', | |
), | |
); | |
} | |
} | |
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 = []; | |
StreamController<List<int>> listStream; | |
initState() { | |
super.initState(); | |
listStream = StreamController(); | |
} | |
dispose() { | |
listStream?.close(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
actions: [_buildAddButton(), _buildRemoveButton()], | |
), | |
body: Center( | |
child: StreamBuilder( | |
stream: listStream.stream, | |
initialData: data, | |
builder: (context, snapshot) { | |
return ListView( | |
children: _buildTiles(), | |
); | |
}, | |
), | |
), | |
); | |
} | |
Widget _buildAddButton() { | |
return Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 20.0), | |
child: IconButton( | |
icon: Icon(Icons.add), | |
onPressed: () { | |
setState( | |
() { | |
data.add(data.length); | |
}, | |
); | |
}, | |
), | |
); | |
} | |
Widget _buildRemoveButton() { | |
return Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 20.0), | |
child: IconButton( | |
icon: Icon(Icons.remove), | |
onPressed: () { | |
data.removeLast(); | |
listStream.add(data); | |
}, | |
), | |
); | |
} | |
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