Skip to content

Instantly share code, notes, and snippets.

@MariaMelnik
Created June 7, 2020 16:56
Show Gist options
  • Save MariaMelnik/5a760b61db67bdc4042db42363076a5e to your computer and use it in GitHub Desktop.
Save MariaMelnik/5a760b61db67bdc4042db42363076a5e to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
static const Widget _home = MyHomePage();
@override
Widget build(BuildContext context) => MaterialApp(
home: _home,
);
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _rebuild = true;
List<Widget> _noRebuilding;
List<Widget> get _rebuilding => [
for (int i = 0; i < 5; ++i)
ChronoListTile(
key: ValueKey('$i'),
index: i,
),
];
@override
void initState() {
super.initState();
_noRebuilding = _rebuilding;
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(_rebuild ? 'will rebuild' : 'wont rebuild'),
actions: [
Padding(
padding: const EdgeInsets.only(right: 160),
child: Switch(
value: _rebuild,
onChanged: (bool _bool) => setState(() => _rebuild = _bool),
),
),
]),
body: ListView(
children: _rebuild ? _rebuilding : _noRebuilding,
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.refresh), onPressed: () => setState(() {})),
);
}
class ChronoListTile extends StatefulWidget {
const ChronoListTile({@required this.index, Key key}) : super(key: key);
final int index;
@override
State<StatefulWidget> createState() => ChronoListTileState();
}
class ChronoListTileState extends State<ChronoListTile> {
Stream<int> _stream;
@override
void initState() {
super.initState();
_stream = Stream<int>.periodic(
const Duration(milliseconds: 100),
(int ms) => ms,
);
}
@override
Widget build(BuildContext context) => ListTile(
leading: Text('${widget.index}'),
trailing: StreamBuilder<int>(
stream: _stream,
initialData: 0,
builder: (
BuildContext context,
AsyncSnapshot<int> snapshot,
) =>
Text(
snapshot.hasError
? 'error'
: !snapshot.hasData ? 'waiting' : '${snapshot.data}',
),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment