Skip to content

Commit 854d788

Browse files
authored
Merge pull request #44 from FlutterOpen/dev
add the StreamBuilder widget to build a list.
2 parents e4d72df + 1f832c8 commit 854d788

File tree

6 files changed

+91
-3
lines changed

6 files changed

+91
-3
lines changed

lib/const/page_item_const.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,9 @@ const PAGE_ITEMS = [
277277
"img": PageImage.FLUTTER_OPEN,
278278
"click": PageName.ASYNC_FUTURE,
279279
},
280+
{
281+
"title": PageName.ASYNC_STREAM_BUILDER,
282+
"img": PageImage.FLUTTER_OPEN,
283+
"click": PageName.ASYNC_STREAM_BUILDER,
284+
},
280285
];

lib/const/page_name_const.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ class PageName {
6161
static const INTER_POINTER = "AbsorbPointer";
6262
static const INTER_SCROLLABLE = "Scrollable";
6363
static const ASYNC_FUTURE = "FutureBuilder";
64+
static const ASYNC_STREAM_BUILDER = "StreamBuilder";
6465
}

lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class FlutterOpenApp extends StatelessWidget {
7474
PageName.INTER_DISMISSIBLE: (context) => DismissiblePage(),
7575
PageName.INTER_POINTER: (context) => PointerPage(),
7676
PageName.ASYNC_FUTURE: (context) => FuturePage(),
77+
PageName.ASYNC_STREAM_BUILDER: (context) => StreamBuilderPage(),
7778
},
7879
);
7980
}

lib/page/async/FuturePage.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class _FutureState extends State<FuturePage> {
2020
Widget build(BuildContext context) {
2121
return Scaffold(
2222
appBar: AppBar(
23-
title: Text("Hello world"),
23+
title: Text(PageName.ASYNC_FUTURE),
2424
),
2525
body: Container(
2626
child: FutureBuilder<String>(
@@ -36,8 +36,7 @@ class _FutureState extends State<FuturePage> {
3636
if (snapshot.hasError) return Text('Error: ${snapshot.error}');
3737
return Text('Result: ${snapshot.data}');
3838
}
39-
return RefreshIndicator(
40-
child: null, onRefresh: null); // unreachable
39+
return null; // unreachable
4140
},
4241
),
4342
),
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
///
2+
/// Created by NieBin on 2019/6/13
3+
/// Github: https://github.com/nb312
4+
/// Email: niebin312@gmail.com
5+
import "package:flutter/material.dart";
6+
import 'package:flutter_widgets/const/_const.dart';
7+
8+
class StreamBuilderPage extends StatefulWidget {
9+
@override
10+
_StreamBuilderState createState() => _StreamBuilderState();
11+
}
12+
13+
class _StreamBuilderState extends State<StreamBuilderPage> {
14+
Stream<List<String>> _stream() =>
15+
Stream<List<String>>.fromFuture(_futureList());
16+
17+
Future<List<String>> _futureList() async {
18+
return _listData;
19+
}
20+
21+
List<String> _listData = [
22+
"Hello",
23+
"World",
24+
"World",
25+
"World",
26+
"World",
27+
"World",
28+
"World",
29+
"World",
30+
"World",
31+
"World",
32+
"World",
33+
"World",
34+
"World",
35+
"World",
36+
"World",
37+
"World",
38+
"World",
39+
"World",
40+
"World",
41+
"World",
42+
"World",
43+
"World",
44+
];
45+
46+
Widget _streamBuildList() => StreamBuilder(
47+
builder: (BuildContext context, AsyncSnapshot<List<String>> snapshot) {
48+
switch (snapshot.connectionState) {
49+
case ConnectionState.none:
50+
return Text('Press button to start.');
51+
case ConnectionState.active:
52+
case ConnectionState.waiting:
53+
return Text('Awaiting result...');
54+
case ConnectionState.done:
55+
if (snapshot.hasError) return Text('Error: ${snapshot.error}');
56+
return ListView.builder(
57+
itemCount: snapshot.data.length,
58+
itemBuilder: (context, index) => Container(
59+
constraints: BoxConstraints.expand(height: 100),
60+
color: index % 2 == 0 ? RED_LIGHT : PURPLE,
61+
child: Text(
62+
snapshot.data[index],
63+
style: TextStyle(color: TEXT_BLACK, fontSize: 20),
64+
),
65+
));
66+
}
67+
return null; // unreachable
68+
},
69+
stream: _stream(),
70+
);
71+
72+
@override
73+
Widget build(BuildContext context) {
74+
return Scaffold(
75+
appBar: AppBar(
76+
title: Text(PageName.ASYNC_STREAM_BUILDER),
77+
),
78+
body: _streamBuildList(),
79+
);
80+
}
81+
}

lib/page/async/_async.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/// Email: niebin312@gmail.com
55
///
66
export "FuturePage.dart";
7+
export 'StreamBuilderPage.dart';

0 commit comments

Comments
 (0)