Hibok
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

102 rader
3.2 KiB

  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'log.dart';
  5. typedef WidgetBuilder<T> = Widget Function(BuildContext context, T snapshot);
  6. class FutureLoadingBuilder<T> extends StatefulWidget {
  7. const FutureLoadingBuilder({
  8. Key key,
  9. @required this.future,
  10. this.initialData,
  11. @required this.builder,
  12. this.mutable = false,
  13. this.loadingIndicator,
  14. }) : assert(builder != null),
  15. super(key: key);
  16. /// The asynchronous computation to which this builder is currently connected,
  17. /// possibly null.
  18. ///
  19. /// If no future has yet completed, including in the case where [future] is
  20. /// null, the data provided to the [builder] will be set to [initialData].
  21. final Future<T> future;
  22. final WidgetBuilder<T> builder;
  23. /// The data that will be used to create the snapshots provided until a
  24. /// non-null [future] has completed.
  25. ///
  26. /// If the future completes with an error, the data in the [AsyncSnapshot]
  27. /// provided to the [builder] will become null, regardless of [initialData].
  28. /// (The error itself will be available in [AsyncSnapshot.error], and
  29. /// [AsyncSnapshot.hasError] will be true.)
  30. final T initialData;
  31. /// default is true
  32. ///
  33. /// set to false if the future will change.
  34. final bool mutable;
  35. final Widget loadingIndicator;
  36. @override
  37. _FutureLoadingBuilderState<T> createState() =>
  38. _FutureLoadingBuilderState<T>();
  39. }
  40. class _FutureLoadingBuilderState<T> extends State<FutureLoadingBuilder<T>> {
  41. Future<T> future;
  42. @override
  43. void initState() {
  44. super.initState();
  45. future = widget.future;
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. return FutureBuilder<T>(
  50. future: widget.mutable ? widget.future : future,
  51. initialData: widget.initialData,
  52. builder: (BuildContext context, AsyncSnapshot snapshot) {
  53. switch (snapshot.connectionState) {
  54. case ConnectionState.none:
  55. case ConnectionState.active:
  56. case ConnectionState.waiting:
  57. return widget.loadingIndicator ??
  58. Center(child: CircularProgressIndicator());
  59. case ConnectionState.done:
  60. if (snapshot.hasError) {
  61. var error = snapshot.error;
  62. if (error is SocketException) {
  63. d('SocketException-> ${error.message}');
  64. return Center(
  65. child: Text(
  66. 'Please check your connection',
  67. overflow: TextOverflow.fade,
  68. ),
  69. );
  70. } else if (error is PlatformException &&
  71. error.code == 'ERROR_GEOCODING_COORDINATES') {
  72. return Text(
  73. 'Please check your connection',
  74. overflow: TextOverflow.fade,
  75. );
  76. } else {
  77. d('Unknow error: $error');
  78. return Center(child: Text('Unknown error'));
  79. }
  80. }
  81. return widget.builder(context, snapshot.data);
  82. }
  83. return widget.builder(context, snapshot.data);
  84. },
  85. );
  86. }
  87. }