Files
mih-project/Frontend/patient_manager/lib/components/myMLTextInput.dart

136 lines
3.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:patient_manager/theme/mihTheme.dart';
class MyMLTextField extends StatefulWidget {
final controller;
final String hintText;
final bool editable;
final bool required;
const MyMLTextField({
super.key,
required this.controller,
required this.hintText,
required this.editable,
required this.required,
});
@override
State<MyMLTextField> createState() => _MyMLTextFieldState();
}
class _MyMLTextFieldState extends State<MyMLTextField> {
bool startup = true;
FocusNode _focus = FocusNode();
bool makeEditable() {
if (widget.editable) {
return false;
} else {
return true;
}
}
String? get _errorText {
final text = widget.controller.text;
if (startup) {
return null;
}
if (!widget.required) {
return null;
}
if (text.isEmpty) {
return "${widget.hintText} is required";
}
return null;
}
void _onFocusChange() {
setState(() {
startup = false;
});
}
Widget setRequiredText() {
if (widget.required) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"*",
style: TextStyle(color: MyTheme().errorColor()),
),
const SizedBox(
width: 8.0,
),
Text(widget.hintText,
style: TextStyle(color: MyTheme().secondaryColor())),
],
);
} else {
return Text(widget.hintText,
style: TextStyle(color: MyTheme().secondaryColor()));
}
}
@override
void initState() {
_focus.addListener(_onFocusChange);
super.initState();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: TextField(
style: TextStyle(color: MyTheme().secondaryColor()),
textAlign: TextAlign.start,
textAlignVertical: TextAlignVertical.top,
expands: true,
maxLines: null,
controller: widget.controller,
readOnly: makeEditable(),
obscureText: false,
focusNode: _focus,
onChanged: (_) => setState(() {
startup = false;
}),
decoration: InputDecoration(
label: setRequiredText(),
errorText: _errorText,
errorStyle: TextStyle(
color: MyTheme().errorColor(), fontWeight: FontWeight.bold),
labelStyle: TextStyle(color: MyTheme().secondaryColor()),
alignLabelWithHint: true,
fillColor: MyTheme().primaryColor(),
filled: true,
//hintText: hintText,
//hintStyle: TextStyle(color: Colors.blueGrey[400]),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: MyTheme().secondaryColor(),
width: 2.0,
),
),
focusedErrorBorder: OutlineInputBorder(
borderSide: BorderSide(
color: MyTheme().errorColor(),
width: 2.0,
),
),
errorBorder: OutlineInputBorder(
borderSide: BorderSide(
color: MyTheme().errorColor(),
width: 2.0,
),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: MyTheme().secondaryColor()),
),
),
),
);
}
}