import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart' hide Store; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:mobx/mobx.dart'; import 'package:provider/provider.dart'; export 'package:provider/provider.dart'; typedef MobxBuilder = Widget Function(BuildContext, T); typedef MobxListener = void Function(BuildContext, T); class ObserverBuilder extends StatelessWidget { final T? store; final MobxBuilder builder; const ObserverBuilder({ super.key, this.store, required this.builder, }); @override Widget build(BuildContext context) { return Observer( builder: (context) { return builder( context, store ?? context.read(), ); }, ); } } class ObserverListener extends HookWidget { final T? store; final MobxListener listener; final Widget child; const ObserverListener({ super.key, this.store, required this.listener, required this.child, }); @override Widget build(BuildContext context) { useEffect(() { final disposer = autorun( (_) => listener(context, store ?? context.read()), ); return disposer; }, []); return child; } } class ObserverConsumer extends HookWidget { final T? store; final MobxListener listener; final MobxBuilder builder; const ObserverConsumer({ super.key, this.store, required this.listener, required this.builder, }); @override Widget build(BuildContext context) { return ObserverListener( listener: listener, child: ObserverBuilder(store: store, builder: builder), ); } }