parent
3708677863
commit
dac07065ff
@ -0,0 +1,37 @@ |
|||||||
|
import 'dart:async'; |
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart'; |
||||||
|
import 'package:thesis_shop/utils/disposable.dart'; |
||||||
|
|
||||||
|
@immutable |
||||||
|
class UserEvent { |
||||||
|
final bool isSignedIn; |
||||||
|
|
||||||
|
const UserEvent({required this.isSignedIn}); |
||||||
|
} |
||||||
|
|
||||||
|
class UserBloc implements Disposable { |
||||||
|
bool _isSignedIn = false; |
||||||
|
bool get isSignedIn => _isSignedIn; |
||||||
|
|
||||||
|
final _isSignedInController = StreamController<bool>.broadcast(); |
||||||
|
Stream<bool> get isSignedInStream => _isSignedInController.stream; |
||||||
|
|
||||||
|
final _userEventController = StreamController<UserEvent>.broadcast(); |
||||||
|
Sink<UserEvent> get userEventSink => _userEventController.sink; |
||||||
|
|
||||||
|
UserBloc() { |
||||||
|
_userEventController.stream.listen(_handleEvent); |
||||||
|
} |
||||||
|
|
||||||
|
void _handleEvent(UserEvent event) { |
||||||
|
_isSignedIn = event.isSignedIn; |
||||||
|
_isSignedInController.add(event.isSignedIn); |
||||||
|
} |
||||||
|
|
||||||
|
@override |
||||||
|
Future<void> dispose() async { |
||||||
|
await _isSignedInController.close(); |
||||||
|
await _userEventController.close(); |
||||||
|
} |
||||||
|
} |
@ -1,26 +1,51 @@ |
|||||||
import 'package:flutter/material.dart'; |
import 'package:flutter/widgets.dart'; |
||||||
|
import 'package:thesis_shop/bloc/user_bloc.dart'; |
||||||
|
|
||||||
class BlocProvider { |
import 'utils/disposable.dart'; |
||||||
// TODO TBD |
|
||||||
|
class BlocProvider implements Disposable { |
||||||
|
final UserBloc userBloc; |
||||||
|
|
||||||
|
const BlocProvider({required this.userBloc}); |
||||||
|
|
||||||
|
@override |
||||||
|
Future<void> dispose() async { |
||||||
|
await Future.wait<void>([ |
||||||
|
userBloc.dispose(), |
||||||
|
]); |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
class AppState extends InheritedWidget { |
class AppState extends StatefulWidget { |
||||||
final BlocProvider blocs; |
final Widget child; |
||||||
|
final BlocProvider blocProvider; |
||||||
const AppState({ |
const AppState({ |
||||||
Key? key, |
Key? key, |
||||||
required this.blocs, |
required this.blocProvider, |
||||||
required Widget child, |
required this.child, |
||||||
}) : super(key: key, child: child); |
}) : super(key: key); |
||||||
|
|
||||||
static AppState of(BuildContext context) { |
static AppState of(BuildContext context) { |
||||||
final AppState? result = |
final AppState? state = context.findAncestorWidgetOfExactType(); |
||||||
context.dependOnInheritedWidgetOfExactType<AppState>(); |
if (state == null) { |
||||||
assert(result != null, 'No AppState found in context'); |
throw Exception("AppState is not an ancestor of this widget"); |
||||||
return result!; |
} |
||||||
|
return state; |
||||||
|
} |
||||||
|
|
||||||
|
@override |
||||||
|
_AppStateState createState() => _AppStateState(); |
||||||
|
} |
||||||
|
|
||||||
|
class _AppStateState extends State<AppState> { |
||||||
|
@override |
||||||
|
Widget build(BuildContext context) { |
||||||
|
return widget.child; |
||||||
} |
} |
||||||
|
|
||||||
@override |
@override |
||||||
bool updateShouldNotify(AppState oldWidget) { |
void dispose() { |
||||||
return true; |
widget.blocProvider.dispose(); |
||||||
|
super.dispose(); |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -0,0 +1,5 @@ |
|||||||
|
import 'dart:async'; |
||||||
|
|
||||||
|
abstract class Disposable { |
||||||
|
FutureOr<void> dispose(); |
||||||
|
} |
Loading…
Reference in new issue