Related
i created a rounded textformfield and validate it, textbox are look like this
here is the code for this textbox
firstly i create a container
import 'package:flutter/material.dart';
class TextFieldContainer extends StatelessWidget {
final Widget child;
const TextFieldContainer({
Key key,
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.symmetric(horizontal: 20, vertical: 5),
width: size.width * 0.8,
decoration: BoxDecoration(
color: Colors.blue[50],
borderRadius: BorderRadius.circular(29),
),
child: child,
);
}
}
and then return this container and creating a textformfield
emailtext(){
return TextFieldContainer(
child: TextFormField(
controller: TextEditingController(text: user.username),
autofillHints: [AutofillHints.email],
onEditingComplete: ()=>TextInput.finishAutofillContext(),
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(Icons.email,color: Colors.blue,),
labelText: 'Username'),
onChanged: (value){
user.username=value;
},
validator: (value) {
if (value.isEmpty) {
return 'Please enter username';
}
return null;
},
),
);
}
same i done for password textbox.
but when i click on login button, error message is displaying like this
i want to show it outside from this blue container.
i used fillcolor, but that is not working too.
please help if anyone know how to do this.
You can use each case separetely in decoration like this:
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.greenAccent, width: 5.0),
borderRadius: BorderRadius.circular(25.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red, width: 5.0),
borderRadius: BorderRadius.circular(25.0),
),
errorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red, width: 5.0),
borderRadius: BorderRadius.circular(25.0),
),
And for background color use fillColor inside decoration
use this code:
Container(
alignment: Alignment.center,
padding: EdgeInsets.fromLTRB(30, 3, 20, 0),
margin: EdgeInsets.only(left: 10, right: 10),
height: 50,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
color: Colors.white,
borderRadius: BorderRadius.circular(10)),
child: TextField(
keyboardType: TextInputType.emailAddress,
cursorColor: Colors.black,
controller: emailController,
textInputAction: TextInputAction.search,
decoration: InputDecoration(
border: InputBorder.none,
suffixIcon: IconButton(
onPressed: () {},
icon: Icon(Icons.email_outlined),
color: Colors.grey,
),
hintText: 'Correo Electrónico',
hintStyle: TextStyle(color: Colors.black),
),
style: TextStyle(color: Colors.black),
),
I have a TextFormField like so:
TextFormField(
cursorColor: Colors.black,
decoration: InputDecoration(
prefixIcon: Icon(Icons.email),
errorMaxLines: 2,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
errorBorder: OutlineInputBorder(
borderSide: BorderSide(
width: 2,
),
),
),
autocorrect: false,
onChanged: (_) {},
validator: (_) {},
),
This TextFormField will fetch an email from an api whenever onChanged() is called, so I decided I want to put a progress/loading indicator inside to indicate that it is fetching data, but I am having a hard time figuring out implementing it.
So is there a way I can insert a CircularProgressIndicator() or a simple progress/loading indicator inside a TextFormField?
Something like this:
TextFormEdit doesn't have the right attribute, that is Suffix widget.
Still, InputDecoration has the Suffix attribute where you can append whatever progress widget you want.
Once here, you can keep it visible or not (and a bunch of other settings) directly in the widget.
TextFormField(
cursorColor: Colors.black,
decoration: InputDecoration(
prefixIcon: Icon(Icons.email),
suffix: isLoading?CircularProgressIndicator():null
errorMaxLines: 2,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
errorBorder: OutlineInputBorder(
borderSide: BorderSide(
width: 2,
),
),
),
autocorrect: false,
onChanged: (_) {},
validator: (_) {},
),
Props to #stefanodecillis, I was also able to achieve it using Stack
Stack(
children: <Widget>[
TextFormField(
cursorColor: Colors.black,
decoration: InputDecoration(
prefixIcon: Icon(Icons.email),
errorMaxLines: 2,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
errorBorder: OutlineInputBorder(
borderSide: BorderSide(
width: 2,
),
),
),
autocorrect: false,
onChanged: (_) {},
validator: (_) {},
),
(state.isSubmitting)
? Positioned(
top: 5,
right: 5,
child: Container(
child: CircularProgressIndicator(),
),
)
: Container(),
],
),
Before loading add this variable to class statefull widget
bool _isLoading = true;
so when your email loaded
setState(){
_isLoading = false;
}
and for add this text field code
TextFormField(
cursorColor: Colors.black,
decoration: InputDecoration(
prefixIcon: Icon(Icons.email),
suffix: isLoading?CircularProgressIndicator():null,
errorMaxLines: 2,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
errorBorder: OutlineInputBorder(
borderSide: BorderSide(
width: 2,
),
),
),
autocorrect: false,
onChanged: (_) {},
validator: (_) {},
),
I am using form and TextFormField to get data from user :
child: TextFormField(
keyboardType: TextInputType.number,
enabled: item.editable,
autofocus: false,
maxLines: 1,
textAlign: TextAlign.right,
controller: TextEditingController()
..text = item.value == null
? ""
: replaceFarsiNumber(
item.value.substring(3, 5)),
validator: (val) {
if (val.length < 2) {
return "دورقمی وارد کنید";
}
return null;
},
onChanged: (_) => _form.currentState.validate(),
decoration: InputDecoration(
labelText: "دقیقه",
labelStyle:
TextStyle(color: color_raisedButton_active),
errorStyle: TextStyle(height: 0),
border: OutlineInputBorder(
borderSide: BorderSide(
color: color_raisedButton_active,
width: 1.0),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: color_raisedButton_active,
width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: color_raisedButton_active,
width: 1.0),
),
errorBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.red, width: 1.0),
),
contentPadding: EdgeInsets.only(
left: 5.0,
top: 0.0,
right: 5.0,
bottom: 0.0),
),
),
I add a validator to count number of word that user added to TextFormField. under some situation i want to show error like this to user:
How can i change the error label position to below of TextFormField
I am trying to make some input field but I can't seem the get the height right. The height of the top one is good but the text is offsetted. I tried to change the height of the textstyle but it did not work. I tried to change the container height but it offsets the text. I tried to change the text height but it doesn't work. What can I do?
Container
(
width: 200,
height: 40,
child: TextFormField
(
keyboardType: TextInputType.phone,
style: Theme.of(context).textTheme.body1,
decoration: InputDecoration
(
hintText: 'Enter your name',
hintStyle: Theme.of(context).textTheme.body1,
border: OutlineInputBorder
(
borderSide: BorderSide
(
color: Theme.of(context).primaryColor,
width: 2.0,
),
),
enabledBorder: OutlineInputBorder
(
borderSide: BorderSide
(
color: Theme.of(context).primaryColor,
width: 2.0,
),
)
),
validator: (value) {
return value.isEmpty ? 'Please enter your name' : null;
},
),
),
SizedBox(height: 10,),
Container
(
width: 200,
child: TextFormField(
keyboardType: TextInputType.phone,
style: Theme.of(context).textTheme.body1,
decoration: InputDecoration(
hintText: 'Enter your phone number',
hintStyle: Theme.of(context).textTheme.body1,
border: OutlineInputBorder
(
borderSide: BorderSide
(
color: Theme.of(context).primaryColor,
width: 2.0,
),
),
enabledBorder: OutlineInputBorder
(
borderSide: BorderSide
(
color: Theme.of(context).primaryColor,
width: 2.0,
),
)
),
validator: (value) {
return value.isEmpty ? 'Please enter your phone number' : null;
},
),
),
The offset appear because you set the height of container smaller minimum height of TextField. You can fix it by increasing the height or reducing contentPadding. Like this
Container
(
alignment: Alignment.bottomRight,
width: 200,
height: 30,
child: TextFormField
(
keyboardType: TextInputType.phone,
style: Theme.of(context).textTheme.body1,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration
(
contentPadding: EdgeInsets.all(5),
hintText: 'Enter your name',
hintStyle: Theme.of(context).textTheme.body1,
border: OutlineInputBorder
(
gapPadding: 0.0,
borderSide: BorderSide
(
color: Theme.of(context).primaryColor,
width: 2.0,
),
),
enabledBorder: OutlineInputBorder
(
gapPadding: 0.0,
borderSide: BorderSide
(
color: Theme.of(context).primaryColor,
width: 2.0,
),
)
),
validator: (value) {
return value.isEmpty ? 'Please enter your name' : null;
},
),
),
SizedBox(height: 10,),
Container
(
width: 200,
child: TextFormField(
keyboardType: TextInputType.phone,
style: Theme.of(context).textTheme.body1,
decoration: InputDecoration(
hintText: 'Enter your phone number',
contentPadding: EdgeInsets.all(5),
hintStyle: Theme.of(context).textTheme.body1,
border: OutlineInputBorder
(
borderSide: BorderSide
(
color: Theme.of(context).primaryColor,
width: 2.0,
),
),
enabledBorder: OutlineInputBorder
(
borderSide: BorderSide
(
color: Theme.of(context).primaryColor,
width: 2.0,
),
)
),
validator: (value) {
return value.isEmpty ? 'Please enter your phone number' : null;
},
),
),
Please try this...
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Container(
margin: EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: TextFormField(
keyboardType: TextInputType.phone,
style: Theme.of(context).textTheme.body1,
decoration: InputDecoration(
hintText: 'Enter your name',
hintStyle: Theme.of(context).textTheme.body1,
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).primaryColor,
width: 2.0,
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).primaryColor,
width: 2.0,
),
)),
validator: (value) {
return value.isEmpty ? 'Please enter your name' : null;
},
),
),
SizedBox(
height: 10,
),
Container(
child: TextFormField(
keyboardType: TextInputType.phone,
style: Theme.of(context).textTheme.body1,
decoration: InputDecoration(
hintText: 'Enter your phone number',
hintStyle: Theme.of(context).textTheme.body1,
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).primaryColor,
width: 2.0,
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).primaryColor,
width: 2.0,
),
),
),
validator: (value) {
return value.isEmpty
? 'Please enter your phone number'
: null;
},
),
),
],
),
),
),
);
Bro wrap with SizeBox like this :😎
SizedBox(
height: 45,
child: TextField(
enabled: enabled,
textCapitalization: textCapitalization,
onEditingComplete: onEditingComplete,
obscureText: obscure,
controller: controller,
keyboardType: keyboardType,
style: AppTheme.normalTextStyle(),
decoration: InputDecoration(
contentPadding: textFieldPadding(),
prefixIcon: perfixIcon,
hintText: hint,
fillColor: Colors.white,
hintStyle: AppTheme.normalTextStyle(),
border: OutlineInputBorder(
borderSide: BorderSide(width: 0.0, color: HexColor("#F1F1F1")),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(width: 1, color: HexColor("#F1F1F1")),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(width: 1, color: HexColor("#F1F1F1")),
),
),
inputFormatters: [LengthLimitingTextInputFormatter(maxLength)],
onTap: onTap,
onSubmitted: onSubmit,
),
);
Material Design for iOS doesn't look good, especially the TextField. So is there any way to create your own ? Or Is ther any way to add some styling to TextField so it will look rounded ?
You can add rounded corners to a TextField within the decoration parameter of it
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
filled: true,
hintStyle: TextStyle(color: Colors.grey[800]),
hintText: "Type in your text",
fillColor: Colors.white70),
)
#Rockvole answer is correct but just in case u don't like the default border around the TextField, you can achieve it by overriding the borderSide attribute of the OutlineInputBorder like this:
TextField(
textAlign: TextAlign.center,
controller: searchCtrl,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Enter a product name eg. pension',
hintStyle: TextStyle(fontSize: 16),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
fillColor: colorSearchBg,
),
),
You can get desired output with the help of Container Have a look..
new Container(
child: new Text (
"Text with rounded edges.",
style: new TextStyle(
color: Colors.blue[500],
fontWeight: FontWeight.w900
)
),
decoration: new BoxDecoration (
borderRadius: new BorderRadius.all(new Radius.circular(10.0)),
color: Colors.black
),
padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0),
),
You can try this code bellow:
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
)
I've collected this solution from several methods and solutions over the internet, it works very well with me, so it could help someone out there:
This solution will control the width and height of the TextField, and other properties
Container(
child: Theme(
data: Theme.of(context).copyWith(splashColor: Colors.transparent),
child: TextField(
autofocus: false,
style: TextStyle(fontSize: 15.0, color: Color(0xFFbdc6cf)),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
hintText: 'Search',
contentPadding:
const EdgeInsets.only(left: 14.0, bottom: 12.0, top: 0.0),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(25.7),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(25.7),
),
),
),
),
decoration: new BoxDecoration (
borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
color: Colors.white ), width: 250, height: 50, margin: new EdgeInsets.fromLTRB(20, 20, 20, 210), padding: new EdgeInsets.fromLTRB(8, 8, 8, 8),
),
You could approach the purpose in a few different ways:
First Approach:
Container(
child: new Text (
"Some Text",
style: TextStyle(
color: Colors.black
)
),
decoration: BoxDecoration (
borderRadius: BorderRadius.all( Radius.circular(15)),
color: Colors.white
),
padding: EdgeInsets.all(16.0),
),
Second Approach:
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
Radius.circular(15.0),
),
),
filled: true,
hintStyle: new TextStyle(color: Colors.grey[600]),
hintText: "Hint text",
fillColor: Colors.white),
)
Third Approach:
TextField(
textAlign: TextAlign.center,
controller: someTextXontroller,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Hint Text',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
fillColor: Colors.blue,
),
),
Try this in TextFormField:
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(20.0),
),
),