-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Input time in textFields using keyboard (#153)
* add time using in text field using keyboard and readme updated * Add Feature: Input Time in text fields using keayboard and readme updated
- Loading branch information
Showing
5 changed files
with
258 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
lib/app/modules/addOrUpdateAlarm/controllers/input_time_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:get/get.dart'; | ||
import 'package:ultimate_alarm_clock/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart'; | ||
|
||
class InputTimeController extends GetxController { | ||
AddOrUpdateAlarmController addOrUpdateAlarmController = | ||
Get.find<AddOrUpdateAlarmController>(); | ||
final isTimePicker = false.obs; | ||
TextEditingController inputHrsController = TextEditingController(); | ||
TextEditingController inputMinutesController = TextEditingController(); | ||
final selectedDateTime = DateTime.now().obs; | ||
@override | ||
void onInit() { | ||
isTimePicker.value = true; | ||
selectedDateTime.value = addOrUpdateAlarmController.selectedTime.value; | ||
isAM.value = addOrUpdateAlarmController.selectedTime.value.hour < 12; | ||
super.onInit(); | ||
} | ||
|
||
final isAM = true.obs; | ||
changePeriod(String period) { | ||
isAM.value = period == 'AM'; | ||
} | ||
|
||
changeDatePicker() { | ||
isTimePicker.value = !isTimePicker.value; | ||
} | ||
|
||
void setTime() { | ||
int hour = int.parse(inputHrsController.text); | ||
if (hour == 0 && inputHrsController.text.length == 2) { | ||
inputHrsController.text = '12'; | ||
} | ||
|
||
if (isAM.value) { | ||
if (hour == 12) { | ||
hour = hour - 12; | ||
} | ||
} else { | ||
if (hour != 12) { | ||
hour = hour + 12; | ||
} | ||
} | ||
int minute = int.parse(inputMinutesController.text); | ||
final time = TimeOfDay(hour: hour, minute: minute); | ||
DateTime today = DateTime.now(); | ||
DateTime tomorrow = today.add(const Duration(days: 1)); | ||
bool isNextDay = (time.hour == today.hour && time.minute < today.minute) || | ||
(time.hour < today.hour); | ||
bool isNextMonth = isNextDay && (today.day > tomorrow.day); | ||
bool isNextYear = isNextMonth && (today.month > tomorrow.month); | ||
int day = isNextDay ? tomorrow.day : today.day; | ||
int month = isNextMonth ? tomorrow.month : today.month; | ||
int year = isNextYear ? tomorrow.month : today.month; | ||
selectedDateTime.value = DateTime(year, month, day, time.hour, time.minute); | ||
addOrUpdateAlarmController.selectedTime.value = selectedDateTime.value; | ||
} | ||
|
||
@override | ||
void onClose() { | ||
inputHrsController.dispose(); | ||
inputMinutesController.dispose(); | ||
super.onClose(); | ||
} | ||
} | ||
|
||
class LimitRange extends TextInputFormatter { | ||
LimitRange( | ||
this.minRange, | ||
this.maxRange, | ||
) : assert( | ||
minRange < maxRange, | ||
); | ||
|
||
final int minRange; | ||
final int maxRange; | ||
|
||
@override | ||
TextEditingValue formatEditUpdate( | ||
TextEditingValue oldValue, TextEditingValue newValue) { | ||
var value = int.parse(newValue.text); | ||
if (value < minRange) { | ||
return TextEditingValue(text: minRange.toString()); | ||
} else if (value > maxRange) { | ||
return TextEditingValue(text: maxRange.toString()); | ||
} | ||
return newValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.