first commit

This commit is contained in:
kicap
2024-08-10 07:35:00 +08:00
commit 88390a01ce
34 changed files with 2850 additions and 0 deletions

View File

@ -0,0 +1,110 @@
import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import '../../../../app/themes/app_text.dart';
import './log_data_view_model.dart';
class LogDataView extends StatelessWidget {
const LogDataView({super.key});
@override
Widget build(BuildContext context) {
return ViewModelBuilder<LogDataViewModel>.reactive(
viewModelBuilder: () => LogDataViewModel(),
onViewModelReady: (LogDataViewModel model) async {
await model.init();
},
builder: (
BuildContext context,
LogDataViewModel model,
Widget? child,
) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(30),
child: model.isBusy
? const Center(
child: CircularProgressIndicator(),
)
: Table(
border: TableBorder.all(),
children: [
const TableRow(
children: [
TableCell(
child: Center(
child: Text(
'Waktu',
style: boldTextStyle,
),
),
),
TableCell(
child: Center(
child: Text(
'Status',
style: boldTextStyle,
),
),
),
TableCell(
child: Center(
child: Text(
'Ketinggian Air',
style: boldTextStyle,
),
),
),
],
),
...model.dataList.map((data) {
return TableRow(
children: [
TableCell(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10,
),
child: Text(
data.createdAt!,
style: regularTextStyle,
textAlign: TextAlign.center,
),
),
),
TableCell(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 10),
child: Text(
data.status! == 0
? "Air Dalam \nTahap Normal"
: data.status! == 1
? "Air Dalam \nTahap Warning"
: "Air Dalam \nTahap Danger",
style: regularTextStyle,
textAlign: TextAlign.center,
),
),
),
TableCell(
child: Center(
child: Text(
data.status == 2
? "${data.waterHeight!} m"
: "-",
style: regularTextStyle,
),
),
),
],
);
}),
],
),
),
);
},
);
}
}

View File

@ -0,0 +1,33 @@
import 'package:flood_app/model/data_model.dart';
import '../../../../app/app.logger.dart';
import '../../../../app/core/custom_base_view_model.dart';
class LogDataViewModel extends CustomBaseViewModel {
final log = getLogger('LogDataViewModel');
List<DataModel> dataList = [];
Future<void> init() async {
await getData(null);
}
getData(String? date) async {
setBusy(true);
try {
// wait 2 seconds
await Future.delayed(const Duration(seconds: 2));
var response = await httpService.get("");
var data = response.data;
data = data['data'];
log.i(data);
for (var i = 0; i < data.length; i++) {
dataList.add(DataModel.fromJson(data[i]));
}
notifyListeners();
} catch (e) {
log.e(e);
} finally {
setBusy(false);
}
}
}

View File

@ -0,0 +1,119 @@
import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import '../../../../app/themes/app_colors.dart';
import '../../../../app/themes/app_text.dart';
import './monitoring_view_model.dart';
class MonitoringView extends StatelessWidget {
const MonitoringView({super.key});
@override
Widget build(BuildContext context) {
return ViewModelBuilder<MonitoringViewModel>.reactive(
viewModelBuilder: () => MonitoringViewModel(),
onViewModelReady: (MonitoringViewModel model) async {
await model.init();
},
builder: (
BuildContext context,
MonitoringViewModel model,
Widget? child,
) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(30),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Notif Banjir",
style: boldTextStyle.copyWith(fontSize: 40),
),
const SizedBox(height: 20),
const Image(
image: AssetImage("assets/logo.png"),
width: 125,
height: 125,
),
const SizedBox(height: 20),
Container(
padding: const EdgeInsets.all(20),
width: double.infinity,
decoration: BoxDecoration(
color: mainColor,
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TheText(
title: 'Status',
text: model.socketIoClient.status,
),
TheText(
title: 'Warning Level',
text: model.socketIoClient.status == "..."
? "No Data"
: model.socketIoClient.warningLevel == 0
? 'Air Belum Mencapai Tahap Warning'
: "Air Mencapai Tahap Warning",
),
TheText(
title: 'Danger Level',
text: model.socketIoClient.status == "..."
? "No Data"
: model.socketIoClient.dangerLevel == 0
? 'Air Belum Mencapai Tahap Danger'
: "Air Mencapai Tahap Danger",
),
TheText(
title: 'Water Height',
text: model.socketIoClient.dangerLevel == 1
? '${model.socketIoClient.waterHeight} m'
: '-',
),
],
),
),
],
),
),
),
);
},
);
}
}
class TheText extends StatelessWidget {
const TheText({
super.key,
required this.title,
required this.text,
});
final String title;
final String text;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(bottom: 10),
child: RichText(
text: TextSpan(
text: '$title : ',
style: boldTextStyle,
children: [
TextSpan(
text: text,
style: regularTextStyle,
),
],
),
),
);
}
}

View File

@ -0,0 +1,13 @@
import '../../../../app/app.logger.dart';
import '../../../../app/core/custom_base_view_model.dart';
class MonitoringViewModel extends CustomBaseViewModel {
final log = getLogger('MonitoringViewModel');
Future<void> init() async {
while (true) {
notifyListeners();
await Future.delayed(const Duration(seconds: 1));
}
}
}

View File

@ -0,0 +1,83 @@
import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:stylish_bottom_bar/stylish_bottom_bar.dart';
import '../../../app/app.router.dart';
import '../../../app/themes/app_colors.dart';
import '../../../app/themes/app_text.dart';
import './nav_bar_view_model.dart';
class NavBarView extends StatelessWidget {
const NavBarView({super.key});
@override
Widget build(BuildContext context) {
return ViewModelBuilder<NavBarViewModel>.reactive(
viewModelBuilder: () => NavBarViewModel(),
onViewModelReady: (NavBarViewModel model) async {
await model.init();
},
builder: (
BuildContext context,
NavBarViewModel model,
Widget? child,
) {
return WillPopScope(
onWillPop: () async {
return false;
},
child: Scaffold(
appBar: AppBar(
backgroundColor: mainColor,
title: Text(
model.bottomNavBarList[model.currentIndex]['name'],
style: boldTextStyle,
),
),
body: ExtendedNavigator(
navigatorKey: StackedService.nestedNavigationKey(3),
router: NavBarViewRouter(),
initialRoute: NavBarViewRoutes.monitoringView,
),
bottomNavigationBar: StylishBottomBar(
items: [
for (var item in model.bottomNavBarList)
BottomBarItem(
icon: Icon(item['icon'],
color: model.currentIndex ==
model.bottomNavBarList.indexOf(item)
? sixthGrey
: backgroundColor),
title: Text(
item['name'],
style: regularTextStyle.copyWith(
color: model.currentIndex ==
model.bottomNavBarList.indexOf(item)
? sixthGrey
: mainGrey,
),
),
backgroundColor: model.currentIndex ==
model.bottomNavBarList.indexOf(item)
? fontColor
: mainGrey,
),
],
currentIndex: model.currentIndex,
hasNotch: true,
backgroundColor: mainColor,
onTap: (value) {
model.handleNavigation(value);
},
option: BubbleBarOptions(
barStyle: BubbleBarStyle.horizontal,
bubbleFillStyle: BubbleFillStyle.fill,
opacity: 0.3),
),
),
);
},
);
}
}

View File

@ -0,0 +1,100 @@
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
import '../../../app/app.locator.dart';
import '../../../app/app.logger.dart';
import '../../../app/app.router.dart';
import '../../../services/my_notification.dart';
import '../../../services/my_socket_io_client.dart';
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
class NavBarViewModel extends IndexTrackingViewModel {
final log = getLogger('NavBarViewModel');
final _navigationService = locator<NavigationService>();
final _socketIoClient = locator<MySocketIoClient>();
final _myNotification = locator<MyNotification>();
final _bottomNavBarList = [
{
'name': 'Real Time',
'icon': Icons.home_outlined,
},
{
'name': 'Log Data',
'icon': Icons.list_alt_outlined,
},
];
List<Map<String, dynamic>> get bottomNavBarList => _bottomNavBarList;
final List<String> _views = [
NavBarViewRoutes.monitoringView,
NavBarViewRoutes.logDataView,
];
Future<void> init() async {
_socketIoClient.on('data', (data) {
// log.i('data : $data');
var waterHeight = data['water_height'];
_socketIoClient.waterHeight = waterHeight is int
? waterHeight.toDouble()
: waterHeight is double
? waterHeight
: double.parse(waterHeight as String);
_socketIoClient.warningLevel = data['warning_level'];
_socketIoClient.dangerLevel = data['danger_level'];
if (_socketIoClient.dangerLevel == 1) {
_socketIoClient.status =
"Bahaya , Peringatan Banjir, Air Melewati Batas";
if (_socketIoClient.notif < 2) {
_myNotification.showNotification(
id: 1,
title: 'Peringatan Banjir',
body: 'Air Melewati Batas',
payload: 'payload',
flutterLocalNotificationsPlugin: flutterLocalNotificationsPlugin,
);
_socketIoClient.notif = 2;
}
} else if (_socketIoClient.warningLevel == 1) {
_socketIoClient.status =
"Peringatan Banjir, Air Dalam Skala 4:5 atau lebih";
if (_socketIoClient.notif == 0) {
_myNotification.showNotification(
id: 2,
title: 'Peringatan Banjir',
body: 'Air Dalam Skala 4:5 atau lebih',
payload: 'payload',
flutterLocalNotificationsPlugin: flutterLocalNotificationsPlugin,
);
_socketIoClient.notif = 1;
}
} else {
_socketIoClient.status = "Normal";
_socketIoClient.notif = 0;
}
notifyListeners();
});
}
void handleNavigation(int index) {
log.d("handleNavigation: $index");
log.d("currentIndex: $currentIndex");
if (currentIndex == index) return;
setIndex(index);
// header = _bottomNavBarList[index]['header'] as String;
_navigationService.navigateTo(
_views[index],
id: 3,
);
}
}

View File

@ -0,0 +1,61 @@
import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import '../../../app/themes/app_text.dart';
import './splash_screen_view_model.dart';
class SplashScreenView extends StatelessWidget {
const SplashScreenView({super.key});
@override
Widget build(BuildContext context) {
return ViewModelBuilder<SplashScreenViewModel>.nonReactive(
viewModelBuilder: () => SplashScreenViewModel(),
onViewModelReady: (SplashScreenViewModel model) async {
await model.init();
},
builder: (
BuildContext context,
SplashScreenViewModel model,
Widget? child,
) {
return Scaffold(
// backgroundColor: mainColor,
body: Column(
children: [
const SizedBox(),
Expanded(
child: Center(
// show the logo.png
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Image(
image: AssetImage("assets/logo.png"),
width: 200,
height: 200,
),
const SizedBox(height: 10),
Text(
"Notif Banjir",
style: boldTextStyle.copyWith(
fontSize: 20,
),
)
],
),
),
),
const Text(
"Made with Flutter and Passion By Kk",
textAlign: TextAlign.center,
style: regularTextStyle,
),
const SizedBox(height: 15),
],
),
);
},
);
}
}

View File

@ -0,0 +1,23 @@
import 'package:flood_app/services/my_notification.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import '../../../app/app.logger.dart';
import '../../../app/app.router.dart';
import '../../../app/core/custom_base_view_model.dart';
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
class SplashScreenViewModel extends CustomBaseViewModel {
final log = getLogger('SplashScreenViewModel');
Future<void> init() async {
await Future.delayed(const Duration(seconds: 2));
// navigate to login page
// ignore: use_build_context_synchronously
MyNotification.initialize(flutterLocalNotificationsPlugin);
socketIoClient.init();
// socketIoClient.connect();
navigationService.replaceWith(Routes.navBarView);
}
}

View File

@ -0,0 +1,34 @@
import 'package:flutter/material.dart';
import '../../app/themes/app_colors.dart';
class MyButton extends StatelessWidget {
const MyButton({
Key? key,
required this.text,
this.onPressed,
}) : super(key: key);
final String text;
final VoidCallback? onPressed;
@override
Widget build(BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: mainColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
onPressed: onPressed,
child: Text(
text,
style: const TextStyle(
color: backgroundColor,
fontSize: 18,
),
),
);
}
}

View File

@ -0,0 +1,91 @@
import 'package:flutter/material.dart';
import '../../app/themes/app_colors.dart';
class MyTextFormField extends StatelessWidget {
const MyTextFormField({
Key? key,
this.labelText,
this.hintText,
this.obscureText,
this.validator,
this.suffixIcon,
this.prefixIcon,
this.focusNode,
this.controller,
this.maxLines = 1,
this.onEditingComplete,
this.readOnly = false,
this.onTap,
this.keyboardType = TextInputType.text,
this.initialValue,
this.enabled = true,
this.maxLength,
}) : super(key: key);
final String? labelText;
final String? hintText;
final bool? obscureText;
final FormFieldValidator<String>? validator;
final Widget? suffixIcon;
final Widget? prefixIcon;
final FocusNode? focusNode;
final TextEditingController? controller;
final int maxLines;
final VoidCallback? onEditingComplete;
final bool readOnly;
final VoidCallback? onTap;
final TextInputType keyboardType;
final String? initialValue;
final bool enabled;
final int? maxLength;
@override
Widget build(BuildContext context) {
return TextFormField(
maxLength: maxLength,
enabled: enabled,
initialValue: initialValue,
onEditingComplete: onEditingComplete,
maxLines: maxLines,
controller: controller,
focusNode: focusNode,
obscureText: obscureText ?? false,
readOnly: readOnly,
onTap: onTap,
keyboardType: keyboardType,
decoration: InputDecoration(
prefixIcon: prefixIcon,
suffixIcon: suffixIcon,
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: mainColor,
),
),
focusedBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: mainColor,
),
),
focusedErrorBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: dangerColor,
),
),
errorBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: dangerColor,
),
),
labelText: labelText,
hintText: hintText,
labelStyle: const TextStyle(color: fontColor),
),
validator: validator,
);
}
}