|
| 1 | +// 🐦 Twitter https://twitter.com/vandadnp |
| 2 | +// 🔵 LinkedIn https://linkedin.com/in/vandadnp |
| 3 | +// 🎥 YouTube https://youtube.com/c/vandadnp |
| 4 | +// 💙 Free Flutter Course https://linktr.ee/vandadnp |
| 5 | +// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY |
| 6 | +// 🔶 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI |
| 7 | +// 🦄 8+ Hours RxSwift Coursde https://youtu.be/xBFWMYmm9ro |
| 8 | +// 🤝 Want to support my work? https://buymeacoffee.com/vandad |
| 9 | + |
| 10 | +import 'package:flutter/material.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + runApp( |
| 14 | + const App(), |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +class App extends StatelessWidget { |
| 19 | + const App({ |
| 20 | + Key? key, |
| 21 | + }) : super(key: key); |
| 22 | + |
| 23 | + @override |
| 24 | + Widget build(BuildContext context) { |
| 25 | + return MaterialApp( |
| 26 | + theme: ThemeData.dark(), |
| 27 | + debugShowCheckedModeBanner: false, |
| 28 | + home: const HomePage(), |
| 29 | + ); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +extension PresentAsyncSnapshot<E> on AsyncSnapshot<E> { |
| 34 | + Widget present({ |
| 35 | + required BuildContext context, |
| 36 | + Widget Function(BuildContext context)? onNone, |
| 37 | + Widget Function(BuildContext context, E data)? onData, |
| 38 | + Widget Function(BuildContext, Object error, StackTrace stackTrace)? onError, |
| 39 | + Widget Function(BuildContext context)? onDoneWitNeitherDataNorError, |
| 40 | + Widget Function(BuildContext context)? onWaiting, |
| 41 | + }) { |
| 42 | + switch (connectionState) { |
| 43 | + case ConnectionState.none: |
| 44 | + return onNone?.call(context) ?? const SizedBox.shrink(); |
| 45 | + case ConnectionState.waiting: |
| 46 | + return onWaiting?.call(context) ?? const CircularProgressIndicator(); |
| 47 | + case ConnectionState.active: |
| 48 | + case ConnectionState.done: |
| 49 | + if (hasError) { |
| 50 | + return onError?.call(context, error!, stackTrace!) ?? |
| 51 | + const SizedBox.shrink(); |
| 52 | + } else if (hasData) { |
| 53 | + return onData?.call(context, data as E) ?? const SizedBox.shrink(); |
| 54 | + } else { |
| 55 | + return onDoneWitNeitherDataNorError?.call(context) ?? |
| 56 | + const SizedBox.shrink(); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +Stream<String> getDateTime() => Stream.periodic( |
| 63 | + const Duration(seconds: 1), |
| 64 | + (_) => DateTime.now().toIso8601String(), |
| 65 | + ); |
| 66 | + |
| 67 | +extension PresentStream<E> on Stream<E> { |
| 68 | + Widget present({ |
| 69 | + Widget Function(BuildContext context)? onNone, |
| 70 | + Widget Function(BuildContext context, E data)? onData, |
| 71 | + Widget Function(BuildContext, Object error, StackTrace stackTrace)? onError, |
| 72 | + Widget Function(BuildContext context)? onDoneWitNeitherDataNorError, |
| 73 | + Widget Function(BuildContext context)? onWaiting, |
| 74 | + }) { |
| 75 | + return StreamBuilder<E>( |
| 76 | + stream: this, |
| 77 | + builder: (context, snapshot) => snapshot.present( |
| 78 | + context: context, |
| 79 | + onNone: onNone, |
| 80 | + onData: onData, |
| 81 | + onError: onError, |
| 82 | + onDoneWitNeitherDataNorError: onDoneWitNeitherDataNorError, |
| 83 | + onWaiting: onWaiting, |
| 84 | + ), |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +class HomePage extends StatelessWidget { |
| 90 | + const HomePage({Key? key}) : super(key: key); |
| 91 | + |
| 92 | + @override |
| 93 | + Widget build(BuildContext context) { |
| 94 | + return Scaffold( |
| 95 | + body: SafeArea( |
| 96 | + child: getDateTime().present( |
| 97 | + onData: (_, dateTime) => Text(dateTime), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments