Download flutter/examples/api/lib/widgets/async/stream_builder.0.dart from luom2645/agent-workspace: direct link, hf CLI and curl.
- Browser
- Download file 4.3 kB
-
https://huggingface.co/luom2645/agent-workspace/resolve/7d4c5ffdb4518a5d7456ef06ab77458fcf42663a/flutter/examples/api/lib/widgets/async/stream_builder.0.dart
- Command line
-
hf download hf://luom2645/agent-workspace@7d4c5ffdb4518a5d7456ef06ab77458fcf42663a/flutter/examples/api/lib/widgets/async/stream_builder.0.dart
-
curl -L -o stream_builder.0.dart https://huggingface.co/luom2645/agent-workspace/resolve/7d4c5ffdb4518a5d7456ef06ab77458fcf42663a/flutter/examples/api/lib/widgets/async/stream_builder.0.dart
4.3 kB
| // Copyright 2014 The Flutter Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import 'dart:async'; | |
| import 'package:flutter/material.dart'; | |
| /// Flutter code sample for [StreamBuilder]. | |
| void main() => runApp(const StreamBuilderExampleApp()); | |
| class StreamBuilderExampleApp extends StatelessWidget { | |
| const StreamBuilderExampleApp({super.key}); | |
| Widget build(BuildContext context) { | |
| return const MaterialApp( | |
| home: StreamBuilderExample(), | |
| ); | |
| } | |
| } | |
| class StreamBuilderExample extends StatefulWidget { | |
| const StreamBuilderExample({super.key}); | |
| 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; | |
| })(); | |
| Widget build(BuildContext context) { | |
| return DefaultTextStyle( | |
| style: Theme.of(context).textTheme.displayMedium!, | |
| textAlign: TextAlign.center, | |
| 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, | |
| ); | |
| }, | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |