stream_builder.0.dart 4.2 KB
Newer Older
1 2 3 4
// 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.

5 6
import 'dart:async';

7 8
import 'package:flutter/material.dart';

9
/// Flutter code sample for [StreamBuilder].
10

11
void main() => runApp(const StreamBuilderExampleApp());
12

13 14
class StreamBuilderExampleApp extends StatelessWidget {
  const StreamBuilderExampleApp({super.key});
15 16 17 18

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
19
      home: StreamBuilderExample(),
20 21 22 23
    );
  }
}

24 25
class StreamBuilderExample extends StatefulWidget {
  const StreamBuilderExample({super.key});
26 27

  @override
28
  State<StreamBuilderExample> createState() => _StreamBuilderExampleState();
29 30
}

31
class _StreamBuilderExampleState extends State<StreamBuilderExample> {
32 33 34 35 36 37 38 39 40 41 42
  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;
43 44 45 46 47
  })();

  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
48
      style: Theme.of(context).textTheme.displayMedium!,
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      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'),
85
                    ),
86 87 88 89 90 91
                  ];
                case ConnectionState.waiting:
                  children = const <Widget>[
                    SizedBox(
                      width: 60,
                      height: 60,
92
                      child: CircularProgressIndicator(),
93 94 95 96
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 16),
                      child: Text('Awaiting bids...'),
97
                    ),
98 99 100 101 102 103 104 105 106 107 108
                  ];
                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}'),
109
                    ),
110 111 112 113 114 115 116 117 118 119 120
                  ];
                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)'),
121
                    ),
122 123 124 125 126 127 128 129 130 131 132 133 134 135
                  ];
              }
            }

            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: children,
            );
          },
        ),
      ),
    );
  }
}