Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/const/page_item_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,9 @@ const PAGE_ITEMS = [
"img": PageImage.FLUTTER_OPEN,
"click": PageName.ASYNC_FUTURE,
},
{
"title": PageName.ASYNC_STREAM_BUILDER,
"img": PageImage.FLUTTER_OPEN,
"click": PageName.ASYNC_STREAM_BUILDER,
},
];
1 change: 1 addition & 0 deletions lib/const/page_name_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ class PageName {
static const INTER_POINTER = "AbsorbPointer";
static const INTER_SCROLLABLE = "Scrollable";
static const ASYNC_FUTURE = "FutureBuilder";
static const ASYNC_STREAM_BUILDER = "StreamBuilder";
}
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class FlutterOpenApp extends StatelessWidget {
PageName.INTER_DISMISSIBLE: (context) => DismissiblePage(),
PageName.INTER_POINTER: (context) => PointerPage(),
PageName.ASYNC_FUTURE: (context) => FuturePage(),
PageName.ASYNC_STREAM_BUILDER: (context) => StreamBuilderPage(),
},
);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/page/async/FuturePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class _FutureState extends State<FuturePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Hello world"),
title: Text(PageName.ASYNC_FUTURE),
),
body: Container(
child: FutureBuilder<String>(
Expand All @@ -36,8 +36,7 @@ class _FutureState extends State<FuturePage> {
if (snapshot.hasError) return Text('Error: ${snapshot.error}');
return Text('Result: ${snapshot.data}');
}
return RefreshIndicator(
child: null, onRefresh: null); // unreachable
return null; // unreachable
},
),
),
Expand Down
81 changes: 81 additions & 0 deletions lib/page/async/StreamBuilderPage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
///
/// Created by NieBin on 2019/6/13
/// Github: https://github.com/nb312
/// Email: niebin312@gmail.com
import "package:flutter/material.dart";
import 'package:flutter_widgets/const/_const.dart';

class StreamBuilderPage extends StatefulWidget {
@override
_StreamBuilderState createState() => _StreamBuilderState();
}

class _StreamBuilderState extends State<StreamBuilderPage> {
Stream<List<String>> _stream() =>
Stream<List<String>>.fromFuture(_futureList());

Future<List<String>> _futureList() async {
return _listData;
}

List<String> _listData = [
"Hello",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
"World",
];

Widget _streamBuildList() => StreamBuilder(
builder: (BuildContext context, AsyncSnapshot<List<String>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Press button to start.');
case ConnectionState.active:
case ConnectionState.waiting:
return Text('Awaiting result...');
case ConnectionState.done:
if (snapshot.hasError) return Text('Error: ${snapshot.error}');
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) => Container(
constraints: BoxConstraints.expand(height: 100),
color: index % 2 == 0 ? RED_LIGHT : PURPLE,
child: Text(
snapshot.data[index],
style: TextStyle(color: TEXT_BLACK, fontSize: 20),
),
));
}
return null; // unreachable
},
stream: _stream(),
);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(PageName.ASYNC_STREAM_BUILDER),
),
body: _streamBuildList(),
);
}
}
1 change: 1 addition & 0 deletions lib/page/async/_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/// Email: niebin312@gmail.com
///
export "FuturePage.dart";
export 'StreamBuilderPage.dart';