I have layout issues because the text from the dropdown menu that you choose can't fit inside the TextFormField and I can't seem to fix this issue. I have tried adding the overflow: TextOverflow.ellipsis property but that only changes for the dropdown labels, still when I choose one, they can't fit inside the TextFormField.
When you press the dropdown button, it shows like this:
and with the TextOverflow.elipsis property:
which is fine, but I still have the layout issue because of the chosen text that is now displayed inside the textformfield:
How can I add the same property to the TextFormField, or any sort of solution to this issue?
The code:
Row(
children: [
Expanded(
child: DropdownButtonFormField<String>(
decoration:
kTextFieldDecoration.copyWith(
hintText: 'Legal status',
labelText: 'Legal status',
),
value: _legalStatus,
items: [
'Sole proprietorship',
'Partnerships',
'Limited Liability Company (LLC)',
'Corporation',
'Small Business Corporation (S-Corporation)'
]
.map((label) => DropdownMenuItem(
child: Text(
label.toString(),
),
value: label,
))
.toList(),
onChanged: (value) {
setState(() {
_legalStatus = value;
print(value);
});
},
validator: (thisValue) {
if (thisValue == null) {
return 'Please choose your legal status';
}
return null;
},
),
),
SizedBox(
width: 16.0,
),
Container(
width: 120.0,
child: DropdownButtonFormField<String>(
decoration:
kTextFieldDecoration.copyWith(
hintText: 'Year established',
labelText: 'Year established',
),
value: _yearEstablished,
items: items // a list of numbers that are Strings
.map((label) => DropdownMenuItem(
child:
Text(label.toString()),
value: label,
))
.toList(),
onChanged: (value) {
setState(() {
_yearEstablished = value;
print(value);
});
},
validator: (thisValue) {
if (thisValue == null) {
return 'Please choose your company year of establishment';
}
return null;
},
),
),
],
),
Thanks in advance for your help!
You need to use isExpanded property of DropDownFormField to solve this error.
Row(
children: [
Expanded(
child: DropdownButtonFormField<String>(
isExpanded: true,
decoration:
kTextFieldDecoration.copyWith(
hintText: 'Legal status',
labelText: 'Legal status',
),
value: _legalStatus,
items: [
'Sole proprietorship',
'Partnerships',
'Limited Liability Company (LLC)',
'Corporation',
'Small Business Corporation (S-Corporation)'
]
.map((label) => DropdownMenuItem(
child: Text(
label.toString(),
),
value: label,
))
.toList(),
onChanged: (value) {
setState(() {
_legalStatus = value;
print(value);
});
},
validator: (thisValue) {
if (thisValue == null) {
return 'Please choose your legal status';
}
return null;
},
),
),
SizedBox(
width: 16.0,
),
Container(
width: 120.0,
child: DropdownButtonFormField<String>(
decoration:
kTextFieldDecoration.copyWith(
hintText: 'Year established',
labelText: 'Year established',
),
value: _yearEstablished,
items: items // a list of numbers that are Strings
.map((label) => DropdownMenuItem(
child:
Text(label.toString()),
value: label,
))
.toList(),
onChanged: (value) {
setState(() {
_yearEstablished = value;
print(value);
});
},
validator: (thisValue) {
if (thisValue == null) {
return 'Please choose your company year of establishment';
}
return null;
},
),
),
],
),
You need to use selectedItemBuilder parameter which will control how the selected item will be displayed on the button. Then, TextOverflow.ellipsis will work with you as expected. Here's how to use it:
selectedItemBuilder: (BuildContext context) {
return items.map<Widget>((String item) {
return Text(item, overflow: TextOverflow.ellipsis,);
}).toList();
},
Related
i want to enable and disable a textfield when i click on a radio button in flutter.
so when the user enabales the radiobutton the text field is enable and vise versa.
`
ListTile(
title: const Text('Per Kilometer Policy'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.unchecked,
groupValue: _character,
fillColor: MaterialStateColor.resolveWith(
(states) => Colors.black),
onChanged: (SingingCharacter? isKiloChecked) {
setState(() {
_character = isKiloChecked;
});
},
),
TextFormField(
enabled: _kilometerButtonDisable,
onSaved: (Value) => print(kiloMeter),
decoration: InputDecoration(
hintStyle: TextStyle(
fontFamily: "Proxima Nova",
fontWeight: FontWeight.w300,
),
border: InputBorder.none,
labelStyle: TextStyle(
color: Color(0xffFAFAFA),
),
),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r"[0-9]+|\s"))
],
controller: kiloMeter,
validator: (value) {
if (value != null && value.isEmpty || value != 1000) {
return 'Please enter your Kilometer';
}
return null;
},
),
`
return Column(
children: <Widget>[
Radio(
value: 1,
groupValue: _radioValue,
onChanged: (value) {
setState(() {
_radioValue = value;
});
},
),
TextField(
enabled: _radioValue == 1 ? true : false,
),
],
);
you can do as follows
TextFormField(
enabled: _character==valueToEnable,
...
)
Where in this expression _character==valueToEnable you can replace the value or the values to enable the checkbox (null, list of values, etc)
You can wrap your TextFormField inside AbsorbPointer and handle absorb property like:
bool _disable = false; // set this to false initially
AbsorbPointer(
absorbing: _disable, // true makes TextFormField disabled and vice-versa
child: TextFormField(),
)
for further assistance check out :
checkout this
Please I need help for looping years in this widget DropdownSearch.
I'm using this script, but return with wrong Values.
DropdownSearch(
onChanged: (value) {
controller.tahunC.text = value.toString();
print(controller.tahunC.text);
},
showClearButton: true,
dropdownSearchDecoration: const InputDecoration(
suffixIcon: Icon(Icons.image),
labelText: "Tahun",
hintText: 'Tahun',
),
items: List<String>.generate(
(DateTime.now().year).toInt() - 2021,
(int index) => '${index + 1}').map(
(val) {
return DropdownMenuItem<String>(
value: val,
child: Text(val),
);
},
).toList(),
),
I am new to Flutter and I am creating a form with TextFormfield and DropdownButtonFormField. I am trying to display the error message of the DropdownButtonFormField(Site Crop) to below the text box like how the site name field display its error message.
Should I continue using DropdownButtonFormField or is there a better way to do this?
FormSample
code for dropdown
child: DropdownButtonHideUnderline(
child: DropdownButtonFormField(
decoration: InputDecoration(
errorBorder: InputBorder.none,
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
hint: Text(
'Choose Country',
style: TextStyle(
fontSize: 16,
color: NewSite.black.withOpacity(0.6),
),
),
validator: (value) => value == null
? 'Please fill in' : null,
value: _selectedLocation,
onChanged: (newValue) {
setState(() {
_selectedLocation = newValue;
});
},
items: _locations.map((location) {
return DropdownMenuItem(
child: new Text(location),
value: location,
);
}).toList(),
icon: Icon(Icons.arrow_drop_down, color: ColorTheme.main,),
style: TextStyle(color: NewSite.black),
isExpanded: true,
elevation: 26,
dropdownColor: NewSite.white,
),
),
Try Form Widget,
Wrap this your filed inside Form,
like this
import 'package:flutter/material.dart';
class DemoDropDownCreate extends StatefulWidget {
#override
_DemoDropDownCreate createState() =>
_DemoDropDownCreate();
}
class _DemoDropDownCreate
extends State<DemoDropDownCreate> {
final _formKey = GlobalKey<FormState>();
bool _autovalidate = false;
String selectedCountry;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
key: _formKey,
autovalidate: _autovalidate,
child:
Column(
children: <Widget>[
DropdownButtonFormField<String>(
value: selectedCountry,
hint: Text(
'Select Country',
),
onChanged: (salutation) =>
setState(() => selectedCountry = salutation),
validator: (value) => value.isEmpty ? 'field required' : null,
items:
['India', 'Japan'].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
FlatButton(
child: Text('PROCEED'),
color: Colors.green,
onPressed: () {
if (_formKey.currentState.validate()) {
//form is valid, proceed further
_formKey.currentState.save();//save once fields are valid, onSaved method invoked for every form fields
print("success");
} else {
setState(() {
_autovalidate = true; //enable realtime validation
});
print("Please select all field");
}
},
)
],
),
),
);
}
}
You can indeed use validators for DropdownButtonFormField
DropdownButtonFormField(
validator: (dynamic value) => value.isEmpty ? "An error happened or please pick something" : null,
decoration: InputDecoration(....
you can change value.isEmpty to any condition you want, same as the "An error happened" message.
I've made a dropdown of cities and city list is coming from API. When user clicks on GPS button, it will auto fill the dropdown field. How should I auto fill the city name in dropdown? I tried by using dropdown inside textfield
TextFormField(
controller: city,
decoration:InputDecoration(
border:InputBorder.none,
prefix: DropdownButtonFormField<String>(
decoration: InputDecoration.collapsed(
hintText: 'select',
hintStyle: TextStyle(fontSize: 12,color: Colors.grey.shade600),
),
value:type,
validator: (value) => value == null? 'please select': null,
onChanged: (String newValue) {
setState(() {
type = newValue;
});
},
items: List?.map((item){
return DropdownMenuItem(
onTap: (){
selectedCity=item['city'];
// print(selectedCity);
},
value: item['id'].toString(),
child: Padding(
padding: const EdgeInsets.only(left:15.0,top: 13.0),
child: Text(item['city'],style: TextStyle(fontSize: 12,color: Colors.grey.shade600),),
),
);
})?.toList()??[]
),
),
),
You can use ready-made auto-complete packages.
Some examples:
https://pub.dev/packages/autocomplete_textfield
https://pub.dev/packages/dropdownfield
I'm having the following code statement.
Container(
height: 50,
padding: const EdgeInsets.only(left: 16, right: 16),
child: DropdownButtonFormField(
isExpanded: true,
validator: (value) => value == 0 ? errorMessage : null,
items: items
.map((e) => DropdownMenuItem(
child: Text(e,
overflow: TextOverflow.visible,
style: Style.poppinsRegularTextStyleWith(
fontSize: 14, color: color12)),
value: items.indexOf(e)))
.toList(),
value: selectedIndex,
onChanged: (value) {
setState(() {
selectedIndex = value;
if (onChanged != null) {
onChanged(items[value]);
}
});
},
),
)
After submitting the form the dropdown that has error message becomes unresponsive to tap, what i'm doing wrong?
How should i change the validator or what other fix is there for this
Can you replace the code with it
DropdownButtonFormField(
isExpanded: true,
validator: (value) => value == 0 ? errorMessage : null,
items: items.map((e) {
return DropdownMenuItem(
value: e,
child: Text(e),
);
}).toList(),
onChanged: (value) => setState(() {
if (onChanged != null) {
onChanged(items[value]);
}
})),
I am using DropdownButtonFormField in my code and it works perfect it is like;
DropdownButtonFormField(
value: _currentSugars ?? userData.sugars,
items: sugars.map((sugar) {
return DropdownMenuItem(
value: sugar,
child: Text('$sugar sugars'),
);
}).toList(),
onChanged: (val) => setState(() => _currentSugars = val),
),
The issue was the extra height on the container, making the dropdown and error message, this was causing some overlap and the tappable area wasn't there anymore.
**height: 50, <= Issue was here**
padding: const EdgeInsets.only(left: 16, right: 16),
child: DropdownButtonFormField(
isExpanded: true,
validator: (value) => value == 0 ? errorMessage : null,
items: items
.map((e) => DropdownMenuItem(
child: Text(e,
overflow: TextOverflow.visible,
style: Style.poppinsRegularTextStyleWith(
fontSize: 14, color: color12)),
value: items.indexOf(e)))
.toList(),
value: selectedIndex,
onChanged: (value) {
setState(() {
selectedIndex = value;
if (onChanged != null) {
onChanged(items[value]);
}
});
},
),
)```