Initial draft
This commit is contained in:
31
lib/widget/operations/bloc/operation_bloc.dart
Normal file
31
lib/widget/operations/bloc/operation_bloc.dart
Normal file
@ -0,0 +1,31 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
|
||||
import 'package:hl_lieferservice/widget/operations/bloc/operation_state.dart';
|
||||
|
||||
class OperationBloc extends Bloc<OperationEvent, OperationState> {
|
||||
OperationBloc() : super(OperationIdle()) {
|
||||
on<LoadOperation>(_loadOperation);
|
||||
on<FailOperation>(_failOperation);
|
||||
on<FinishOperation>(_finishOperation);
|
||||
}
|
||||
|
||||
Future<void> _loadOperation(LoadOperation event, Emitter<OperationState> emit) async {
|
||||
emit(OperationLoading());
|
||||
}
|
||||
|
||||
Future<void> _failOperation(FailOperation event, Emitter<OperationState> emit) async {
|
||||
emit(OperationFailed(message: event.message));
|
||||
|
||||
await Future.delayed(Duration(seconds: 5));
|
||||
|
||||
emit(OperationIdle());
|
||||
}
|
||||
|
||||
Future<void> _finishOperation(FinishOperation event, Emitter<OperationState> emit) async {
|
||||
emit(OperationFinished(message: event.message));
|
||||
|
||||
await Future.delayed(Duration(seconds: 5));
|
||||
|
||||
emit(OperationIdle());
|
||||
}
|
||||
}
|
||||
15
lib/widget/operations/bloc/operation_event.dart
Normal file
15
lib/widget/operations/bloc/operation_event.dart
Normal file
@ -0,0 +1,15 @@
|
||||
abstract class OperationEvent {}
|
||||
|
||||
class LoadOperation extends OperationEvent {}
|
||||
|
||||
class FailOperation extends OperationEvent {
|
||||
String message;
|
||||
|
||||
FailOperation({required this.message});
|
||||
}
|
||||
|
||||
class FinishOperation extends OperationEvent {
|
||||
String? message;
|
||||
|
||||
FinishOperation({this.message});
|
||||
}
|
||||
17
lib/widget/operations/bloc/operation_state.dart
Normal file
17
lib/widget/operations/bloc/operation_state.dart
Normal file
@ -0,0 +1,17 @@
|
||||
abstract class OperationState {}
|
||||
|
||||
class OperationIdle extends OperationState {}
|
||||
|
||||
class OperationLoading extends OperationState {}
|
||||
|
||||
class OperationFailed extends OperationState {
|
||||
String message;
|
||||
|
||||
OperationFailed({required this.message});
|
||||
}
|
||||
|
||||
class OperationFinished extends OperationState {
|
||||
String? message;
|
||||
|
||||
OperationFinished({this.message});
|
||||
}
|
||||
30
lib/widget/operations/presentation/message_container.dart
Normal file
30
lib/widget/operations/presentation/message_container.dart
Normal file
@ -0,0 +1,30 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ErrorContainer extends MessageContainer {
|
||||
const ErrorContainer({super.key, required super.message})
|
||||
: super(color: Colors.deepOrangeAccent);
|
||||
}
|
||||
|
||||
class SuccessContainer extends MessageContainer {
|
||||
const SuccessContainer({super.key, required super.message})
|
||||
: super(color: Colors.greenAccent);
|
||||
}
|
||||
|
||||
class MessageContainer extends StatelessWidget {
|
||||
final String message;
|
||||
final Color color;
|
||||
|
||||
const MessageContainer({
|
||||
super.key,
|
||||
required this.message,
|
||||
this.color = Colors.deepOrange,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(color: color),
|
||||
child: Center(child: Text(message)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,73 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
|
||||
import 'package:hl_lieferservice/widget/operations/presentation/message_container.dart';
|
||||
|
||||
import '../bloc/operation_state.dart';
|
||||
|
||||
/// OperationViewEnforcer
|
||||
///
|
||||
/// A view that encapsulates the functionality to react to asynchronous operations.
|
||||
/// It is capable of showing a loading indicator while an operation is ongoing and it shows
|
||||
/// a error message if the operation failed.
|
||||
class OperationViewEnforcer extends StatefulWidget {
|
||||
final Widget child;
|
||||
const OperationViewEnforcer({super.key, required this.child});
|
||||
@override
|
||||
State<OperationViewEnforcer> createState() => _OperationViewEnforcerState();
|
||||
}
|
||||
|
||||
class _OperationViewEnforcerState extends State<OperationViewEnforcer> {
|
||||
OverlayEntry? _overlayEntry;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_overlayEntry?.remove();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<OperationBloc, OperationState>(
|
||||
listener: (context, state) {
|
||||
if (state is OperationLoading) {
|
||||
if (_overlayEntry == null) {
|
||||
_overlayEntry = _createOverlayEntry(context);
|
||||
Overlay.of(context).insert(_overlayEntry!);
|
||||
}
|
||||
} else {
|
||||
_overlayEntry?.remove();
|
||||
_overlayEntry = null;
|
||||
}
|
||||
|
||||
if (state is OperationFinished) {
|
||||
if (state.message != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(state.message!)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (state is OperationFailed) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(state.message)),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: widget.child,
|
||||
);
|
||||
}
|
||||
|
||||
OverlayEntry _createOverlayEntry(BuildContext context) {
|
||||
return OverlayEntry(
|
||||
builder: (context) => DecoratedBox(
|
||||
decoration: const BoxDecoration(
|
||||
color: Color.fromRGBO(128, 128, 128, 0.8),
|
||||
),
|
||||
child: const Center(
|
||||
child: CircularProgressIndicator(color: Colors.white),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user