Files
fcs/lib/pages/widgets/input_time.dart

107 lines
3.6 KiB
Dart
Raw Normal View History

2020-10-12 08:26:27 +06:30
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2020-10-18 02:38:46 +06:30
import 'package:intl/intl.dart';
2020-10-12 08:26:27 +06:30
import 'package:provider/provider.dart';
class InputTime extends StatelessWidget {
2021-09-10 14:25:37 +06:30
final String? labelTextKey;
final IconData? iconData;
final TextEditingController? controller;
final FormFieldValidator<String>? validator;
2020-10-12 08:26:27 +06:30
final int maxLines;
final bool withBorder;
2021-09-10 14:25:37 +06:30
final Color? borderColor;
final TextInputType? textInputType;
2020-10-12 08:26:27 +06:30
final bool autoFocus;
const InputTime(
2021-09-10 14:25:37 +06:30
{Key? key,
2020-10-12 08:26:27 +06:30
this.labelTextKey,
this.iconData,
this.controller,
this.validator,
this.maxLines = 1,
this.withBorder = false,
this.borderColor,
this.autoFocus = false,
this.textInputType})
: super(key: key);
@override
Widget build(BuildContext context) {
var languageModel = Provider.of<LanguageModel>(context);
return Padding(
padding: const EdgeInsets.only(top: 15.0, bottom: 5),
child: TextFormField(
readOnly: true,
onTap: () async {
FocusScope.of(context).unfocus();
var initialDate = TimeOfDay.now();
if (controller != null) {
try {
2020-10-18 02:38:46 +06:30
final format = DateFormat.jm(); //"6:00 AM"
initialDate =
2021-09-10 14:25:37 +06:30
TimeOfDay.fromDateTime(format.parse(controller!.text));
2020-10-18 02:38:46 +06:30
// var values = controller.text.split(":");
// initialDate = TimeOfDay(
// hour: int.parse(values[0]), minute: int.parse(values[1]));
2020-10-12 08:26:27 +06:30
} catch (e) {} // ignore error
}
2021-09-10 14:25:37 +06:30
TimeOfDay? t = await showTimePicker(
2020-10-12 08:26:27 +06:30
initialTime: initialDate,
context: context,
);
if (t != null && controller != null) {
2020-10-18 02:38:46 +06:30
final format = DateFormat.jm(); //"6:00 AM"
final now = new DateTime.now();
final dt =
DateTime(now.year, now.month, now.day, t.hour, t.minute);
2021-09-10 14:25:37 +06:30
controller!.text = "${format.format(dt)}";
2020-10-12 08:26:27 +06:30
}
},
controller: controller,
autofocus: autoFocus,
cursorColor: primaryColor,
style: textStyle,
maxLines: maxLines,
keyboardType: textInputType,
decoration: new InputDecoration(
// hintText: '',
hintStyle: TextStyle(
height: 1.5,
),
labelText: labelTextKey == null
? null
2021-09-10 14:25:37 +06:30
: AppTranslations.of(context)!.text(labelTextKey!),
2020-10-12 08:26:27 +06:30
labelStyle: languageModel.isEng
? newLabelStyle(color: Colors.black54, fontSize: 20)
: newLabelStyleMM(color: Colors.black54, fontSize: 20),
icon: iconData == null
? null
: Icon(
iconData,
color: primaryColor,
),
enabledBorder: withBorder
? OutlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0),
)
: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
focusedBorder: withBorder
? OutlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0),
)
: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
),
validator: validator),
);
}
}