-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream_builder_screen.dart
61 lines (54 loc) · 1.65 KB
/
stream_builder_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class StreamBuilderScreen extends StatefulWidget {
@override
_StreamBuilderScreenState createState() => _StreamBuilderScreenState();
}
class _StreamBuilderScreenState extends State<StreamBuilderScreen> {
StreamController<String> _controller;
Stream<String> _streamTimes;
@override
void initState() {
_streamTimes = Stream.periodic(Duration(seconds: 1),
(_) => DateFormat("yy/MM/dd HH:mm:ss").format(DateTime.now()));
_controller = StreamController<String>();
_controller.addStream(_streamTimes);
super.initState();
}
@override
void dispose() {
_controller.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stream Builder Demo'),
),
body: StreamBuilder<String>(
stream: _controller.stream,
initialData: "",
builder: (context, snapshot) {
if (snapshot.data.isEmpty)
return Center(child: CircularProgressIndicator());
if (snapshot.hasError)
return Center(
child: Text(
"${snapshot.error}",
style: TextStyle(fontSize: 36, color: Colors.blue),
),
);
if (snapshot.hasData)
return Center(
child: Text(
'${snapshot.data}',
style: TextStyle(fontSize: 36, color: Colors.blue),
),
);
return Center(child: CircularProgressIndicator());
}),
);
}
}