From dbb3450c32e243b63dbaf7e924f446f4eead99cc Mon Sep 17 00:00:00 2001 From: mohammad Date: Wed, 11 Sep 2024 16:04:54 +0300 Subject: [PATCH] new HourPicker --- lib/pages/common/hour_picker_dialog.dart | 78 ++++++++----------- .../bloc/visitor_password_bloc.dart | 25 ++++-- lib/utils/style.dart | 24 ++++++ pubspec.lock | 8 ++ pubspec.yaml | 2 +- 5 files changed, 81 insertions(+), 56 deletions(-) diff --git a/lib/pages/common/hour_picker_dialog.dart b/lib/pages/common/hour_picker_dialog.dart index 2c89b710..01a87720 100644 --- a/lib/pages/common/hour_picker_dialog.dart +++ b/lib/pages/common/hour_picker_dialog.dart @@ -1,7 +1,10 @@ + + import 'package:flutter/material.dart'; class HourPickerDialog extends StatefulWidget { final TimeOfDay initialTime; + const HourPickerDialog({super.key, required this.initialTime}); @override @@ -9,70 +12,50 @@ class HourPickerDialog extends StatefulWidget { } class _HourPickerDialogState extends State { - late int _selectedHour; - bool _isPm = false; + late String selectedHour; @override void initState() { super.initState(); - _selectedHour = widget.initialTime.hour > 12 - ? widget.initialTime.hour - 12 - : widget.initialTime.hour; - _isPm = widget.initialTime.period == DayPeriod.pm; + // Initialize the selectedHour with the initial time passed to the dialog + selectedHour = widget.initialTime.hour.toString().padLeft(2, '0') + ':00'; } @override Widget build(BuildContext context) { return AlertDialog( title: const Text('Select Hour'), - content: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - DropdownButton( - value: _selectedHour, - items: List.generate(12, (index) { - int displayHour = index + 1; - return DropdownMenuItem( - value: displayHour, - child: Text(displayHour.toString()), - ); - }), - onChanged: (value) { - setState(() { - _selectedHour = value!; - }); - }, - ), - SizedBox(width: 16.0), - DropdownButton( - value: _isPm, - items: const [ - DropdownMenuItem( - value: false, - child: Text('AM'), - ), - DropdownMenuItem( - value: true, - child: Text('PM'), - ), - ], - onChanged: (value) { - setState(() { - _isPm = value!; - }); - }, - ), - ], + content: DropdownButton( + value: selectedHour, // Show the currently selected hour + items: List.generate(24, (index) { + String hour = index.toString().padLeft(2, '0'); + return DropdownMenuItem( + value: '$hour:00', + child: Text('$hour:00'), + ); + }), + onChanged: (String? newValue) { + if (newValue != null) { + setState(() { + selectedHour = newValue; // Update the selected hour without closing the dialog + }); + } + }, ), actions: [ TextButton( - onPressed: () => Navigator.of(context).pop(null), + onPressed: () => Navigator.of(context).pop(null), // Close the dialog without selection child: const Text('Cancel'), ), TextButton( onPressed: () { - int hour = _isPm ? _selectedHour + 12 : _selectedHour; - Navigator.of(context).pop(TimeOfDay(hour: hour, minute: 0)); + // Close the dialog and return the selected time + Navigator.of(context).pop( + TimeOfDay( + hour: int.parse(selectedHour.split(':')[0]), + minute: 0, + ), + ); }, child: const Text('OK'), ), @@ -86,6 +69,7 @@ Future showHourPicker({ required TimeOfDay initialTime, }) { return showDialog( + barrierDismissible: false, context: context, builder: (context) => HourPickerDialog(initialTime: initialTime), ); diff --git a/lib/pages/visitor_password/bloc/visitor_password_bloc.dart b/lib/pages/visitor_password/bloc/visitor_password_bloc.dart index 02f5ef0a..ae0eda30 100644 --- a/lib/pages/visitor_password/bloc/visitor_password_bloc.dart +++ b/lib/pages/visitor_password/bloc/visitor_password_bloc.dart @@ -2,6 +2,7 @@ import 'dart:math'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:fluttertoast/fluttertoast.dart'; import 'package:intl/intl.dart'; import 'package:syncrow_web/pages/common/custom_dialog.dart'; import 'package:syncrow_web/pages/common/hour_picker_dialog.dart'; @@ -14,6 +15,7 @@ import 'package:syncrow_web/services/access_mang_api.dart'; import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/utils/snack_bar.dart'; +import 'package:syncrow_web/utils/style.dart'; class VisitorPasswordBloc extends Bloc { VisitorPasswordBloc() : super(VisitorPasswordInitial()) { @@ -32,9 +34,9 @@ class VisitorPasswordBloc extends Bloc(selectTimeOfLinePassword); on(changeTime); } + final TextEditingController userNameController = TextEditingController(); final TextEditingController emailController = TextEditingController(); - final TextEditingController deviceNameController = TextEditingController(); final TextEditingController deviceIdController = TextEditingController(); final TextEditingController unitNameController = TextEditingController(); @@ -377,19 +379,26 @@ class VisitorPasswordBloc extends Bloc expirationTimeTimeStamp!) { - CustomSnackBar.displaySnackBar('Effective Time cannot be later than Expiration Time.'); + showCustomToast( + message: "Effective Time cannot be later than Expiration Time.", + gravity: ToastGravity.CENTER, + textColor: Colors.white, + fontSize: 16.0, + ); } else { - effectiveTime = - selectedDateTime.toString().split('.').first; // Remove seconds and milliseconds + effectiveTime = selectedDateTime.toString().split('.').first; effectiveTimeTimeStamp = selectedTimestamp; } } else { if (effectiveTimeTimeStamp != null && selectedTimestamp < effectiveTimeTimeStamp!) { - CustomSnackBar.displaySnackBar( - 'Expiration Time cannot be earlier than Effective Time.'); + showCustomToast( + message: 'Expiration Time cannot be earlier than Effective Time.', + gravity: ToastGravity.CENTER, + textColor: Colors.white, + fontSize: 16.0, + ); } else { - expirationTime = - selectedDateTime.toString().split('.').first; // Remove seconds and milliseconds + expirationTime = selectedDateTime.toString().split('.').first; expirationTimeTimeStamp = selectedTimestamp; } } diff --git a/lib/utils/style.dart b/lib/utils/style.dart index 3f31f9d7..33a850a7 100644 --- a/lib/utils/style.dart +++ b/lib/utils/style.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:fluttertoast/fluttertoast.dart'; import 'color_manager.dart'; InputDecoration? textBoxDecoration({bool suffixIcon = false}) => @@ -41,3 +42,26 @@ BoxDecoration containerDecoration = BoxDecoration( ], color: ColorsManager.boxColor, borderRadius: const BorderRadius.all(Radius.circular(10))); + + +/// A function to display a customizable toast message +void showCustomToast({ + required String message, + ToastGravity gravity = ToastGravity.BOTTOM, + Color textColor = Colors.white, + Toast toastLength = Toast.LENGTH_SHORT, + int timeInSecForIosWeb = 2, + double fontSize = 16.0, +}) { + Fluttertoast.showToast( + msg: message, + webBgColor: "linear-gradient(to right, #dc1c13, #dc1c13)", + webPosition:'right' , + toastLength: toastLength, + webShowClose: true, + gravity: gravity, + timeInSecForIosWeb: timeInSecForIosWeb, + textColor: textColor, + fontSize: fontSize, + ); +} diff --git a/pubspec.lock b/pubspec.lock index 8b9df6d6..e358cdbf 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -232,6 +232,14 @@ packages: description: flutter source: sdk version: "0.0.0" + fluttertoast: + dependency: "direct main" + description: + name: fluttertoast + sha256: "95f349437aeebe524ef7d6c9bde3e6b4772717cf46a0eb6a3ceaddc740b297cc" + url: "https://pub.dev" + source: hosted + version: "8.2.8" get_it: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index d187cf1a..14210eb5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -49,7 +49,7 @@ dependencies: intl: ^0.19.0 dropdown_search: ^5.0.6 flutter_dotenv: ^5.1.0 - + fluttertoast: ^8.2.8 dev_dependencies: flutter_test: