async_value_view.dart 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. class AsyncValueView<T> extends StatelessWidget {
  4. const AsyncValueView({
  5. super.key,
  6. required this.value,
  7. required this.data,
  8. this.loadingMessage = 'Loading...',
  9. });
  10. final AsyncValue<T> value;
  11. final Widget Function(T data) data;
  12. final String loadingMessage;
  13. @override
  14. Widget build(BuildContext context) {
  15. return value.when(
  16. data: data,
  17. loading: () => Card(
  18. child: Padding(
  19. padding: const EdgeInsets.all(20),
  20. child: Row(
  21. children: [
  22. const SizedBox(
  23. width: 18,
  24. height: 18,
  25. child: CircularProgressIndicator(strokeWidth: 2.2),
  26. ),
  27. const SizedBox(width: 12),
  28. Expanded(child: Text(loadingMessage)),
  29. ],
  30. ),
  31. ),
  32. ),
  33. error: (error, stackTrace) => Card(
  34. child: Padding(
  35. padding: const EdgeInsets.all(20),
  36. child: Text('Request failed: $error'),
  37. ),
  38. ),
  39. );
  40. }
  41. }