flutter_suspense 0.0.1 copy "flutter_suspense: ^0.0.1" to clipboard
flutter_suspense: ^0.0.1 copied to clipboard

discontinued

Place holder for work in progress

example/lib/main.dart

// ignore_for_file: omit_local_variable_types
import 'package:flutter/material.dart';
import 'package:flutter_suspense/suspense.dart';
import 'package:functional_widget_annotation/functional_widget_annotation.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

part 'main.g.dart';

void main() => runApp(HooksGalleryApp());

/// An App that demonstrates how to use hooks. It includes examples that cover
/// the hooks provided by this library as well as examples that demonstrate
/// how to write custom hooks.
class HooksGalleryApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(title: 'Flutter Hooks Gallery', home: const Test());
  }
}

@hwidget
Widget test() {
  int value = 0;
  // ignore: close_sinks
  final controller = useStreamController<int>();
  return Scaffold(
    appBar: AppBar(),
    body: Suspense(
      fallback: Center(child: CircularProgressIndicator()),
      child: Center(
        child: Foo(controller.stream),
      ),
    ),
    floatingActionButton: FloatingActionButton(
      child: Icon(Icons.plus_one),
      backgroundColor: Colors.red,
      onPressed: () {
        controller.add(value++);
      },
    ),
  );
}

@hwidget
Widget foo(BuildContext context, Stream<int> stream) {
  final counter = useState(0);

  useState(0);

  final streamed = useStreamSuspense(stream);

  final future = useMemoized(
    () {
      return Future<void>.delayed(const Duration(seconds: 1)).then((_) => 42);
    },
    <dynamic>[counter.value],
  );

  final future2 = useMemoized(
    () {
      if (counter.value <= 4) {
        return Future.value(0);
      }
      return Future<void>.delayed(const Duration(seconds: 5)).then((_) => 84);
    },
    <dynamic>[counter.value > 4],
  );
  final value = useFutureSuspense(future);
  final value3 = useFutureSuspense(future);
  final value2 = useFutureSuspense(future2);

  return GestureDetector(
    onTap: () => counter.value++,
    child: Text('Tapped : ${counter.value} $value $value2 $value3 $streamed'),
  );
}