I'm able to change the outline color of a TextField's color to a solid color by using the following code:
TextField(
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.orange),
),
),
),
However, I couldn't change its color to a gradient since it only accepts color as an input. How would I change its underline color to a linear gradient in Flutter?
Though, it seems that there is not a property to change underlined color to gradient color, this effect can be achieved with Stack widget,
Here is how I tried to do it:
body: Center(
child: Container(
height: 50,
margin: EdgeInsets.all(
10.0,
),
child: Stack(
children: <Widget>[
TextField(
cursorColor: Colors.red,
decoration: InputDecoration(
hintText: " Enter your text here",
contentPadding: EdgeInsets.symmetric(
vertical: 15.0,
horizontal: 15.0,
),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 0.5,
),
borderRadius: BorderRadius.circular(
10.0,
),
),
),
),
Positioned(
bottom: -1,
child: Container(
height: 10,
width: MediaQuery.of(context).size.width - 20,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(10),
bottomRight: Radius.circular(10),
),
gradient: LinearGradient(
colors: [
Colors.red,
Colors.green,
],
),
),
),
),
],
),
),
),
You can modify it according to your UI.
Output:
Second Verion with no borders :
body: Center(
child: Container(
height: 50,
margin: EdgeInsets.all(
10.0,
),
child: Stack(
children: <Widget>[
TextField(
cursorColor: Colors.red,
decoration: InputDecoration(
hintText: " Enter your text here",
contentPadding: EdgeInsets.symmetric(
vertical: 15.0,
horizontal: 15.0,
),
),
),
Positioned(
bottom: 1,
child: Container(
height: 3,
width: MediaQuery.of(context).size.width - 20,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.red,
Colors.green,
],
),
),
),
),
],
),
),
)
Output:
Related
I want to make a flag in circular. I am using "country_calling_code_picker: ^2.0.0" plugin and i got a image that image i want in circular. when I am trying to make in circular then its not becoming.
this is my code.
Row(
children: [
Expanded(
flex: 0,
child: Container(
decoration: BoxDecoration(
color: whiteColor.withOpacity(0.4),
border: Border(
bottom: BorderSide(
color:greyColor,
width: 1.7,
),
)
),
child: InkWell(onTap: (){
_onPressedShowBottomSheet();
},
child: Row(
children: [
Image.asset(
country.flag,
package: countryCodePackageName,
width: 40,
height: 50,
),SizedBox(width: 5,),
Icon(Icons.arrow_drop_down_sharp,size: 30,color: greyColor,)
],
),
),
),
),
SizedBox(
width: 15,
),
Expanded(
flex: 3,
child: Container( decoration: BoxDecoration(
color: whiteColor.withOpacity(0.4),
),
child: TextFormField(
cursorColor: Theme.of(context).cursorColor,
decoration: const InputDecoration(
hintText: "Enter Number",
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color:greyColor,width: 1.7),
), contentPadding: EdgeInsets.only(left: 12,right: 12),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: greyColor,width: 1.7),
),
),
),
),
),
],
),
I want to make like this my flag
But this is becoming like this
You can wrap your image in a ClipRRect.
Row(
children: [
Expanded(
flex: 0,
child: Container(
decoration: BoxDecoration(
color: whiteColor.withOpacity(0.4),
border: Border(
bottom: BorderSide(
color: greyColor,
width: 1.7,
),
)
),
child: InkWell(onTap: () {
_onPressedShowBottomSheet();
},
child: Row(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(100),
Image.asset(
country.flag,
package: countryCodePackageName,
width: 40,
height: 50,
),
),
SizedBox(width: 5,),
Icon(Icons.arrow_drop_down_sharp, size: 30,
color: greyColor,)
],
),
),
),
),
SizedBox(
width: 15,
),
Expanded(
flex: 3,
child: Container(decoration: BoxDecoration(
color: whiteColor.withOpacity(0.4),
),
child: TextFormField(
cursorColor: Theme
.of(context)
.cursorColor,
decoration: const InputDecoration(
hintText: "Enter Number",
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: greyColor, width: 1.7),
), contentPadding: EdgeInsets.only(left: 12, right: 12),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: greyColor, width: 1.7),
),
),
),
),
),
],
),
The second Container which is in the red color is taking all the remaining space. How to avoid that. I want my second container to take the minimum space.
Container(
height: 600,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(60),
),
),
child: Padding(
padding: const EdgeInsets.only(top: 80.0, right: 50, left: 50),
child: Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
child: Column(
children: [
TextFormField(
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: "Email or Phone Number"),
),
SizedBox(
height: 20,
),
TextField(
decoration:
new InputDecoration.collapsed(hintText: 'PassWord'),
)
],
),
),
),
),[![][1]][1]
Remove parent Container height and set Column <mainAxisSize: MainAxisSize.min,>.
It is taking full height because of Container height and the default behavior of Column.
And to improve UI, use padding on Container<padding:x,child Column>
Container(
// height: 600,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(60),
),
),
child: Padding(
padding: const EdgeInsets.only(top: 80.0, right: 50, left: 50),
child: Container(
padding: const EdgeInsets.all(40), //here padding
decoration: const BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: "Email or Phone Number"),
),
const SizedBox(
height: 20,
),
const TextField(
decoration: InputDecoration.collapsed(hintText: 'PassWord'),
),
],
),
),
),
),
Try below code hope its helpful to you. Just remove your container extra/large height
Container(
height: 180,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(
60,
),
),
),
child: Padding(
padding: const EdgeInsets.only(top: 80.0, right: 50, left: 50),
child: Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
child: Column(
children: [
TextFormField(
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: "Email or Phone Number",
),
),
SizedBox(
height: 20,
),
TextField(
decoration: InputDecoration.collapsed(
hintText: 'PassWord',
),
)
],
),
),
),
),
Your result screen->
I have tried wrapping the textField in a row and adding a container to the row but it throws an error and the rest part of the form doesn't render.
And using the prefix property for textField also doesn't work as the container is shown when the textField is pressed but I want it to be seen even when the textField is not active.
this is the required UI for the mobile no. field I'm trying to build
Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Container(
height: 50,
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(
horizontal: 8,
),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8),
bottomLeft: Radius.circular(8),
),
),
child: Text(
"+91",
style: TextStyle(
color: Colors.white,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12,
),
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Mobile No."
),
),
),
)
],
),
)
result:
I am trying to add textfields in a widget on Flutter, and it renders correctly, but when the text is pressed to enter the data, appears the following error:
"Bottom overflowed by 182 pixels"
The error is when the default keyboard appears. I don't know why is it happening since i am wrapping everything in a SingleChildScrollView widget, therefore, should work just fine.
Attached the code:
class TextFieldsState extends State<TextFields> {
#override
Widget build(BuildContext context) {
const colorRed = Color(0xFFF0786E);
return SingleChildScrollView(
child: Container(
margin: EdgeInsets.symmetric(
horizontal: 20.0,
vertical: 20.0,
),
//color: Colors.greenAccent,
height: 500,
child: Stack(
//alignment: Alignment.bottomCenter,
children: <Widget>[
Container (
height: 500,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(22.0),
color: colorRed,
),
child: Container (
height: 500,
margin: EdgeInsets.only(right: 10,),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(22),
),
),
),
Center(
child: Positioned (
child: SizedBox(
height: 136,
//width: size.width,
child: Column(
children: <Widget>[
Container(
height: 30,
width: 160,
child: TextField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: 'Name',
),
textAlign: TextAlign.center,
),
),
SizedBox(height: 20.0),
Container(
height: 30,
width: 160,
child: TextField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: 'Surname',
),
textAlign: TextAlign.center,
),
),
SizedBox(height: 20.0),
Container(
height: 30,
width: 160,
child: TextField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: 'Nationality',
),
textAlign: TextAlign.center,
),
),
],
),
),
),
),
],
),
),
);
}
}
What is the solution, please?
try this please
SingleChildScrollView(
column( // your column
)
)
overflow error is in column and you should add SingleChildScrollView as parent of this column.
Please check the edited below code.
class TextFieldsState extends State<TextFields> {
#override
Widget build(BuildContext context) {
const colorRed = Color(0xFFF0786E);
return Scaffold(
body: SingleChildScrollView(
child: Container(
margin: EdgeInsets.symmetric(
horizontal: 20.0,
vertical: 20.0,
),
//color: Colors.greenAccent,
height: 500,
child: Stack(
//alignment: Alignment.bottomCenter,
children: <Widget>[
Container(
height: 500,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(22.0),
color: colorRed,
),
child: Container(
height: 500,
margin: EdgeInsets.only(
right: 10,
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(22),
),
),
),
Center(
child: SizedBox(
height: 136,
//width: size.width,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
height: 30,
width: 160,
child: TextField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: 'Name',
),
textAlign: TextAlign.center,
),
),
SizedBox(height: 20.0),
Container(
height: 30,
width: 160,
child: TextField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: 'Surname',
),
textAlign: TextAlign.center,
),
),
SizedBox(height: 20.0),
Container(
height: 30,
width: 160,
child: TextField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: 'Nationality',
),
textAlign: TextAlign.center,
),
),
],
),
),
),
),
],
),
),
),
);
}
}
I ended up wrapping everything with a Expanded widget and the column with the Text fields I wrapped it with a SingleChilsScrollView and it worked perfectly.
I want to style this kind of TextField.
The main problem that I'm facing is to design the US$ part in leading the TextField. This is my code so far.
/// My Place Bid Text Field
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(4),
),
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
SizedBox(
child: Container(
//width: mySize.width * 0.2,
//height: 50,
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4),
bottomLeft: Radius.circular(4),
),
color: Colors.grey[200]),
child: Text(
"RS \u20B9",
style: TextStyle(fontSize: 24),
),
),
),
Expanded(
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
labelText: "Place Bid",
labelStyle: TextStyle(fontSize: 24)),
),
),
],
),
),
Which gives me output
I know this question is not that complicated, but I'm still relatively new to flutter so your help would be much appreciated.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sample"),
),
body: Padding(
padding: EdgeInsets.all(10),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey[500],
),
borderRadius: BorderRadius.all(Radius.circular(8))),
width: double.infinity,
height: 50,
child: Row(
children: <Widget>[
Container(
padding: EdgeInsets.all(10),
color: Colors.grey.shade300,
height: 50,
child: Align(
alignment: Alignment.center,
child: Text(
"RS \u20B9",
style: TextStyle(
fontSize: 24, fontWeight: FontWeight.bold),
),
)),
SizedBox(
width: 10,
),
Expanded(
child: Container(
height: 50,
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Enter Bid",
labelStyle: TextStyle(fontSize: 24)),
)))
],
),
),
));
}
Output
You can use the prefixIcon to add a widget at the beginning of the textfield:
Container(
child: Row(
children: [
Expanded(
child: TextField(
decoration: InputDecoration(
prefixIcon: Padding(
padding: const EdgeInsetsDirectional.only(start: 2.0),
child: Container(
padding: const EdgeInsets.all(13),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4),
bottomLeft: Radius.circular(4),
),
color: Colors.grey[200]),
child: Text(
"RS \u20B9",
style: TextStyle(fontSize: 24),
),
),
),
border: const OutlineInputBorder(),
labelText: 'Hint Text',
),
),
),
],
),
)
example:
https://dartpad.dev/a89aff20d67e2cb31da6a07ba7c17910
Based on your solution.
Notice the "IntrinsicHeight" widget. It makes all the children inside the child the same size (depending on the biggest one). The "Center" widget inside the prefix makes the Container go max height + center the text inside of it. Just make sure not to use IntrinsicHeight too much, since the docs state that it's a bit resource heavy.
Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
border: Border.all(color: Colors.grey[200]),
),
child: IntrinsicHeight(
child: Row(
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(horizontal: 10.0),
decoration: BoxDecoration(
color: Colors.grey[100],
border: Border(
right: BorderSide(color: Colors.grey[200]),
),
),
child: Center(
child: Text(
'RS \u20B9',
style: TextStyle(fontSize: 24),
),
),
),
Expanded(
child: TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
border: InputBorder.none,
labelText: "Place Bid",
),
),
),
],
),
),
),
The second code is almost the same, except there's no label after typing the text inside the TextField.
Container(
margin: EdgeInsets.symmetric(horizontal: 20.0),
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
border: Border.all(color: Colors.grey[200]),
),
child: IntrinsicHeight(
child: Row(
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(horizontal: 10.0),
decoration: BoxDecoration(
color: Colors.grey[100],
border: Border(
right: BorderSide(color: Colors.grey[200]),
),
),
child: Center(
child: Text(
'RS \u20B9',
style: TextStyle(fontSize: 24),
),
),
),
Expanded(
child: TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(
horizontal: 20.0,
vertical: 20.0,
),
border: InputBorder.none,
hintText: "Place Bid",
),
),
),
],
),
),
),
Container(
decoration: BoxDecoration(
),
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
SizedBox(
child: Container(
//width: mySize.width * 0.2,
//height: 50,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
border:Border.all(color:Colors.grey),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4),
bottomLeft:Radius.circular(4),
),
color: Colors.grey[200]),
child: Text(
"RS \u20B9",
style: TextStyle(fontSize: 24),
),
),
),
Expanded(
child: TextField(
decoration: InputDecoration(
border: new OutlineInputBorder(
borderRadius: BorderRadius.only(
topRight: Radius.circular(4),
bottomRight:
Radius.circular(4),
),
),
isDense: true,
labelText: "Place Bid",
labelStyle:
TextStyle(fontSize:24)),
),
),
],
),
),