forked from firka/flutter
Add tests for stream_builder.0.dart API example. (#147832)
This PR contributes to https://github.com/flutter/flutter/issues/130459 ### Description - Updates `examples/api/lib/widgets/async/stream_builder.0.dart` - Adds test for `examples/api/lib/widgets/async/stream_builder.0.dart`
This commit is contained in:
committed by
GitHub
parent
1128eabec6
commit
456d176a70
@@ -13,34 +13,54 @@ void main() => runApp(const StreamBuilderExampleApp());
|
||||
class StreamBuilderExampleApp extends StatelessWidget {
|
||||
const StreamBuilderExampleApp({super.key});
|
||||
|
||||
static const Duration delay = Duration(seconds: 1);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: StreamBuilderExample(),
|
||||
home: StreamBuilderExample(delay: delay),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class StreamBuilderExample extends StatefulWidget {
|
||||
const StreamBuilderExample({super.key});
|
||||
const StreamBuilderExample({
|
||||
required this.delay,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final Duration delay;
|
||||
|
||||
@override
|
||||
State<StreamBuilderExample> createState() => _StreamBuilderExampleState();
|
||||
}
|
||||
|
||||
class _StreamBuilderExampleState extends State<StreamBuilderExample> {
|
||||
final Stream<int> _bids = (() {
|
||||
late final StreamController<int> controller;
|
||||
controller = StreamController<int>(
|
||||
onListen: () async {
|
||||
await Future<void>.delayed(const Duration(seconds: 1));
|
||||
controller.add(1);
|
||||
await Future<void>.delayed(const Duration(seconds: 1));
|
||||
await controller.close();
|
||||
},
|
||||
);
|
||||
return controller.stream;
|
||||
})();
|
||||
late final StreamController<int> _controller = StreamController<int>(
|
||||
onListen: () async {
|
||||
await Future<void>.delayed(widget.delay);
|
||||
|
||||
if (!_controller.isClosed) {
|
||||
_controller.add(1);
|
||||
}
|
||||
|
||||
await Future<void>.delayed(widget.delay);
|
||||
|
||||
if (!_controller.isClosed) {
|
||||
_controller.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
Stream<int> get _bids => _controller.stream;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (!_controller.isClosed) {
|
||||
_controller.close();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -50,86 +70,108 @@ class _StreamBuilderExampleState extends State<StreamBuilderExample> {
|
||||
child: Container(
|
||||
alignment: FractionalOffset.center,
|
||||
color: Colors.white,
|
||||
child: StreamBuilder<int>(
|
||||
stream: _bids,
|
||||
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
|
||||
List<Widget> children;
|
||||
if (snapshot.hasError) {
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
color: Colors.red,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('Error: ${snapshot.error}'),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text('Stack trace: ${snapshot.stackTrace}'),
|
||||
),
|
||||
];
|
||||
} else {
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.none:
|
||||
children = const <Widget>[
|
||||
Icon(
|
||||
Icons.info,
|
||||
color: Colors.blue,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: Text('Select a lot'),
|
||||
),
|
||||
];
|
||||
case ConnectionState.waiting:
|
||||
children = const <Widget>[
|
||||
SizedBox(
|
||||
width: 60,
|
||||
height: 60,
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: Text('Awaiting bids...'),
|
||||
),
|
||||
];
|
||||
case ConnectionState.active:
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.check_circle_outline,
|
||||
color: Colors.green,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('\$${snapshot.data}'),
|
||||
),
|
||||
];
|
||||
case ConnectionState.done:
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.info,
|
||||
color: Colors.blue,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('\$${snapshot.data} (closed)'),
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: children,
|
||||
);
|
||||
},
|
||||
),
|
||||
child: BidsStatus(bids: _bids),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BidsStatus extends StatelessWidget {
|
||||
const BidsStatus({
|
||||
required this.bids,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final Stream<int>? bids;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return StreamBuilder<int>(
|
||||
stream: bids,
|
||||
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
|
||||
List<Widget> children;
|
||||
if (snapshot.hasError) {
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
color: Colors.red,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('Error: ${snapshot.error}'),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
'Stack trace: ${snapshot.stackTrace}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
];
|
||||
} else {
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.none:
|
||||
children = const <Widget>[
|
||||
Icon(
|
||||
Icons.info,
|
||||
color: Colors.blue,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: Text('Select a lot'),
|
||||
),
|
||||
];
|
||||
case ConnectionState.waiting:
|
||||
children = const <Widget>[
|
||||
SizedBox(
|
||||
width: 60,
|
||||
height: 60,
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: Text('Awaiting bids...'),
|
||||
),
|
||||
];
|
||||
case ConnectionState.active:
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.check_circle_outline,
|
||||
color: Colors.green,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text('\$${snapshot.data}'),
|
||||
),
|
||||
];
|
||||
case ConnectionState.done:
|
||||
children = <Widget>[
|
||||
const Icon(
|
||||
Icons.info,
|
||||
color: Colors.blue,
|
||||
size: 60,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text(
|
||||
snapshot.hasData
|
||||
? '\$${snapshot.data} (closed)'
|
||||
: '(closed)',
|
||||
),
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: children,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user