"Invalid constant value" when trying to create a dynamic TextInput widget - flutter

I'm trying to create a new TextInput widget for customizing TextFormField but i can't customize labelText. I need to send in constructor labelText for my TextFormField and show this String.
class TextInput extends StatelessWidget {
final TextEditingController textControler;
final String textLabelHint;
const TextInput({Key? key, required this.textControler,required this.textLabelHint}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: textControler,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: textLabelHint,
),
),
);
}
}
But I have problem:
labelText: textLabelHint, //Invalid constant value.

You ned to remove const from decoration: const InputDecoration(...), since textLabelHint isn't a const value:
class TextInput extends StatelessWidget {
final TextEditingController textControler;
final String textLabelHint;
const TextInput(
{Key? key, required this.textControler, required this.textLabelHint})
: super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: textControler,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: textLabelHint,
),
),
);
}
}

This error is there because textLabelHint is a final class property (not const) that could change based on the constructor value. However, where you try to pass this value is InputDecoration which you have marked as const. Thus, the error indicates that.
To resolve the issue, remove the const keyword before the InputDecoration:
class TextInput extends StatelessWidget {
final TextEditingController textControler;
final String textLabelHint;
const TextInput({Key? key, required this.textControler,required this.textLabelHint}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: textControler,
decoration: InputDecoration( // <-- Notice the removed const
border: OutlineInputBorder(),
labelText: textLabelHint,
),
),
);

Try this:
class TextInput extends StatelessWidget {
final TextEditingController textControler;
final String textLabelHint;
const TextInput(
{Key? key, required this.textControler, required this.textLabelHint})
: super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: textControler,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: textLabelHint,
),
),
);
}
}

Related

How can I make FormBuilderTextField validators reusable?

So I am making a sign up page in Flutter. I'm gonna use this "flutter_form_builder" and "form_builder_validators" packages. I will make the textfields reusable to save time, and everything works, except the validators.
This is my reusable widget:
class MyFormBuilderTextField extends StatelessWidget {
final String name;
final Color inputTextColor;
final String labelText;
final Color labelColor;
final bool filled;
final Color fillColor;
final double borderRadius;
final Color enabledBorderColor;
final Color focusedBorderColor;
final FormFieldValidator<String> validators;
MyFormBuilderTextField({
required this.name,
this.inputTextColor = const Color(0xFFFFFFFF),
required this.labelText,
this.labelColor = Colors.white54,
this.filled = true,
this.fillColor = const Color(0xFF131313),
this.borderRadius = 10.0,
this.enabledBorderColor = Colors.white12,
this.focusedBorderColor = const Color(0xFFF57B3B),
required this.validators,
});
#override
Widget build(BuildContext context) {
return FormBuilderTextField(
name: name,
style: TextStyle(color: inputTextColor),
decoration: InputDecoration(
labelText: labelText,
labelStyle: TextStyle(color: labelColor),
filled: filled,
fillColor: fillColor,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(borderRadius),
borderSide: BorderSide(color: enabledBorderColor),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(borderRadius),
borderSide: BorderSide(color: focusedBorderColor),
),
),
validator:validators,
);
}
}
This is how I tried implementing it:
class MyFormBuilder extends StatefulWidget {
#override
State<MyFormBuilder> createState() => _MyFormBuilderState();
}
class _MyFormBuilderState extends State<MyFormBuilder> {
final _formKey = GlobalKey<FormBuilderState>();
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF070707),
body: _buildContent(),
);
}
Widget _buildContent() {
return SingleChildScrollView(
padding: EdgeInsets.all(24.0),
child: FormBuilder(
key: _formKey,
child: Column(
children: <Widget>[
SizedBox(height: 30.0),
MyFormBuilderTextField(
name: 'password',
labelText: 'Password',
validators: [
FormBuilderValidators.min(8),
FormBuilderValidators.required(),
],
),
],
),
)
);
}
}
I am getting this error:
The argument type 'String? Function(String?)' can't be assigned to the parameter type 'List<String? Function(String?)>'.dartargument_type_not_assignable
How can I make the validators reusable?

How to show a text along textfield data like this in flutter

How to show a constant text in textfield in Flutter along with typed data like in this picture.
You can use String Interpolation. Using $ to access variable in Text Widget.
#override
Widget build(BuildContext context) {
double quantity = 1;
return Row(
children: [
Icon(Icons.production_quantity_limits_outlined),
SizedBox(
width: 20,
),
Text("$quantity (qty)"),
],
);
}
class Example extends StatelessWidget {
const Example({super.key});
#override
Widget build(BuildContext context) {
double quantity = 1;
return TextField(
decoration: InputDecoration(
label: Text("$quantity (qty)"),
border: OutlineInputBorder()
),
);
}
}
you can use like this:
class TextFieldExample extends StatelessWidget {
const TextFieldExample({Key? key}) : super(key: key);
Widget build(BuildContext context) {
TextEditingController? controller;
return Scaffold(
body: Padding(
padding: EdgeInsets.all(20),
child: TextField(
controller: controller,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
),
filled: true,
hintText: '1(qty)',
prefixIcon: Icon(Icons.watch),
),
),
),
);
}
}

How to give focus to DateTimeFormField on Flutter?

I have a form with TextFormFields. I can give focus to next field using Tab key as usual. But, it skips DateTimeFormField. How can I make it show the Date picker instead of skipping it when it is its turn?
Did some search but couldn't find anything near it.
Here is my code samples. I simply use TextFormField's and DateTimeFormField inside a Column. But there is no focus option for DateTimeFormField.
class FormDialog extends ConsumerWidget {
const FormDialog({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context, WidgetRef ref) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(
height: 12,
),
CustomTextField(
labelText: firstName,
textInputAction: TextInputAction.next,
),
const SizedBox(
height: 12,
),
CustomTextField(
labelText: lastName,
textInputAction: TextInputAction.next,
),
const SizedBox(
height: 12,
),
CustomDateField(
labelText: dateOfBirth,
),
const SizedBox(
height: 12,
),
CustomTextField.email(
labelText: parentEmail,
),
],
);
}
}
class CustomDateField extends StatelessWidget {
const CustomDateField({
Key? key,
this.labelText,
this.hintText,
}) : super(key: key);
final String? labelText;
final String? hintText;
#override
Widget build(BuildContext context) {
return DateTimeFormField(
mode: DateTimeFieldPickerMode.date,
dateFormat: DateFormat.yMd(),
decoration: InputDecoration(
labelText: labelText,
hintText: hintText,
border: const OutlineInputBorder(),
helperText: ' ',
),
);
}
}
class CustomTextField extends StatelessWidget {
const CustomTextField({
Key? key,
this.labelText,
this.icon,
this.textInputAction,
}) : super(key: key);
final String? labelText;
final TextFieldIcon? icon;
final TextInputAction? textInputAction;
#override
Widget build(BuildContext context) {
return TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
decoration: InputDecoration(
hintText: labelText,
border: const OutlineInputBorder(),
prefixIcon: icon,
helperText: ' ',
),
textInputAction: textInputAction,
);
}
factory CustomTextField.email({
String? errorMessage,
String? labelText,
}) =>
CustomTextField(
labelText: labelText ?? email,
icon: const TextFieldIcon.email(),
textInputAction: TextInputAction.next,
);
}
According to the package's Github repository, it is built upon FormField, creating and initializing a FocusNode and passing the focus node to the widget might resolve the issue. If not you might need to implement your own form field.

Flutter Int_phone_field how to align text field to the country code selector

I have been trying to align the country code selector with the phone number text field. Please help, the following is the problem and the code. I need everything horizontally centered.
class RoundedPhoneField extends StatelessWidget {
final String hintText;
final IconData icon;
final ValueChanged<String> onChanged;
const RoundedPhoneField({
Key? key,
required this.hintText,
this.icon = Icons.person,
required this.onChanged,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return PhoneFieldContainer(
child: IntlPhoneField(
decoration: InputDecoration(
labelText: hintText,
isCollapsed: true,
floatingLabelBehavior: FloatingLabelBehavior.never,
border: InputBorder.none,
),
initialCountryCode: 'ZM',
onChanged: (phone) {
print(phone.completeNumber);
},
),
);
}
}
class PhoneFieldContainer extends StatelessWidget {
final Widget child;
const PhoneFieldContainer({
Key? key,
required this.child,
}) : super(key: key);
#override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
padding: EdgeInsets.fromLTRB(20, 10, 20, 5,),
width: size.width * 0.8,
decoration: BoxDecoration(
color: kPrimaryLightColor,
borderRadius: BorderRadius.circular(29),
),
child: child,
);
}
}
I have faced the same issue.
This problem is with the plugin itself:
I have made the required changes here and it looks like this now:

TextFormField showing error inside the TextFormField- Flutter

As shown in image, error message disturb TextField.
I am using container a parent widget to make some UI stuff but this happened.
Without validation message it works fin
I want error message below the TextField not inside, kindly help.
As shown in image, error message disturb TextField.
I am using container a parent widget to make some UI stuff but this happened.
Without validation message it works fine.
I want error message below the TextField not inside, kindly help.
import 'package:flutter/material.dart';
import 'package:email_validator/email_validator.dart';
class RoundedTextField extends StatelessWidget {
final String hintText;
final IconData iconData;
final Color hintcolor, iconcolor;
final bool password;
final TextEditingController tcontroller;
final TextInputType tType;
const RoundedTextField(
{Key key,
#required this.hintText,
#required this.iconData,
#required this.hintcolor,
this.iconcolor,
this.password,
this.tcontroller,
this.tType})
: super(key: key);
#override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 20),
width: size.width * 0.8,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(29),
color: Color(0xFFa5b2fc),
),
child: TextFormField(
controller: tcontroller,
obscureText: password,
keyboardType: tType,
validator: (String value) {
bool isValid = EmailValidator.validate(value);
if (!password) {
if (!isValid) {
return "Invalid Email Address";
}
}
},
decoration: InputDecoration(
suffixIcon: password
? (Icon(
Icons.visibility,
color: iconcolor,
))
: null,
border: InputBorder.none,
icon: Icon(
iconData,
color: iconcolor,
),
hintText: hintText,
hintStyle: TextStyle(color: hintcolor),
),
),
);
}
}
Use the decoration within the TexformField. Including Text, borders, Shapes, Colors