passo_mobile_app/lib/screens/sos/index.dart

129 lines
4.3 KiB
Dart
Raw Permalink Normal View History

2023-04-13 08:45:19 +00:00
import 'dart:async';
import 'package:animate_do/animate_do.dart';
2023-04-11 06:08:10 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
2023-04-13 08:45:19 +00:00
import 'package:flutter_svg/svg.dart';
2023-04-11 06:08:10 +00:00
import 'package:unit2/bloc/sos/sos_bloc.dart';
2023-04-13 08:45:19 +00:00
import 'package:unit2/screens/sos/components/acknnowledge.dart';
2023-04-11 06:08:10 +00:00
import 'package:unit2/screens/sos/components/add_mobile.dart';
2023-04-13 08:45:19 +00:00
import 'package:unit2/screens/sos/components/request_sos.dart';
2023-04-11 06:08:10 +00:00
import 'package:unit2/theme-data.dart/colors.dart';
2023-04-13 08:45:19 +00:00
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/error_state.dart';
import '../../utils/global.dart';
import '../../widgets/wave.dart';
import 'components/request_sos.dart';
import 'components/sos_received.dart';
2023-04-11 06:08:10 +00:00
class SosScreen extends StatefulWidget {
const SosScreen({super.key});
@override
State<SosScreen> createState() => _SosScreenState();
}
class _SosScreenState extends State<SosScreen> {
2023-04-13 08:45:19 +00:00
Timer? timer;
@override
void dispose() {
timer != null ? timer!.cancel() : timer = null;
super.dispose();
}
@override
void initState() {
timer = Timer(Duration.zero, () {});
super.initState();
}
2023-04-11 06:08:10 +00:00
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(title:const Text("SOS"),backgroundColor: primary,centerTitle: true,),
2023-04-11 06:08:10 +00:00
resizeToAvoidBottomInset: true,
body: ProgressHUD(
2023-04-13 08:45:19 +00:00
padding: const EdgeInsets.all(32),
2023-04-11 06:08:10 +00:00
backgroundColor: Colors.black87,
indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
2023-04-13 08:45:19 +00:00
child: BlocConsumer<SosBloc, SosState>(
2023-04-11 06:08:10 +00:00
listener: (context, state) {
2023-04-13 08:45:19 +00:00
if (state is ErrorState) {
final progress = ProgressHUD.of(context);
progress!.dismiss();
2023-04-11 06:08:10 +00:00
}
////loading state
if (state is LoadingState) {
final progress = ProgressHUD.of(context);
2023-04-13 08:45:19 +00:00
progress!.showWithText(state.message);
}
//// dismiss progress
if (state is ErrorState || state is SOSReceivedState || state is RequestSosState || state is UserLocationLoaded || state is SoSAcknowledgementConfirm) {
final progress = ProgressHUD.of(context);
progress!.dismiss();
2023-04-11 06:08:10 +00:00
}
},
builder: (context, state) {
2023-04-13 08:45:19 +00:00
//// error state
if (state is ErrorState) {
timer!.cancel();
return SomethingWentWrong(
message: state.message,
onpressed: () {
context.read<SosBloc>().add(LoadUserLocation());
});
} //// user location loaded
if (state is UserLocationLoaded) {
return AddMobile();
//// request sos state
}
if (state is RequestSosState) {
return const RequestSOS();
}
//// received sos state
if (state is SOSReceivedState) {
timer = Timer.periodic(const Duration(seconds: 10), (timer) {
context.read<SosBloc>().add(
CheckAcknowledgement(sessionToken: state.sessionToken));
});
return SOSreceived(onpressed: () async {
timer!.cancel();
await SOS!.delete('session_token');
Navigator.pop(context);
});
}
///// sos acknowledge and confirm
if (state is SoSAcknowledgementConfirm) {
return SosAcknowledged(
onpressed: () async {
timer!.cancel();
await SOS!.delete('session_token');
Navigator.pop(context);
},
sessionData: state.sessionData);
}
return SizedBox(
height: screenHeight,
child: Stack(children: [
Positioned(
bottom: 0,
right: 0,
child: WaveReverse(height: blockSizeVertical * 8))
]),
);
2023-04-11 06:08:10 +00:00
},
),
),
));
}
}