I need of a Box where insert and store some informations that comes as user input, I'm using the 'TextField' so I changed the 'maxLines' option in my 'TextField' from default value (1) to (2) and when I want to insert some text here I can't leave the box because the 'Done' button changed to 'return' blocking me! The keyboard is blocked too.
I'm doing this because I would like a Box with some text inside (like a description) that I can move where I want and change all its properties.
My issue is that the textfield writes all in one line, I need of a newline at the border of the textfield.
Do you know how to handle the TextField better?
This is a part of the code :
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.white,
body : Container(
decoration: BoxDecoration(
shape: BoxShape.rectangle,
image: DecorationImage(
image: _image == null
? AssetImage('assets/images/io.png')
: FileImage(_image), // here add your image file path,
alignment: Alignment.topCenter,
),
),
child: GestureDetector(
child:Container(
alignment: Alignment.center,
child : ConstrainedBox(
constraints: BoxConstraints(
maxHeight: 400,
maxWidth: 400,
),
child: TextField(
minLines: 1,
maxLines: 2,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
const Radius.circular(48.0)
),
),
labelText: 'Descrizione',
labelStyle: TextStyle(
backgroundColor: Colors.white,
),
),
),
),
),
)
)
);
P.S.: I'm studying this 'GestureDetector', it should be useful for this purpose I guess.
In the TextField go into InputDecoration and add a Widget to the suffix property . The suffix property is a Widget that shows at the end of the Textfield.
FocusNode node = FocusNode();
TextField(
minLines: 1,
maxLines: 2,
focusNode: node,
decoration: InputDecoration(
suffix: RaisedButton(
onPressed: () {
node.unfocus();
},
child: Text("Done"),
),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(const Radius.circular(48.0)),
),
labelText: 'Descrizione',
labelStyle: TextStyle(
backgroundColor: Colors.white,
),
),
),
I nested my TextField inside a Container with width 10.0, but the textfield is still taking up the whole width of the device.
Widget _reviewBody() {
return SliverToBoxAdapter(
child: Container(
width: 10.0,
padding: EdgeInsets.only(bottom: 30.0),
child: TextField(
onChanged: (text) {
setState(() {
reviewBody = text;
});
},
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
hintText: 'Add your thoughts here',
hintStyle: TextStyle(color: Burnt.hintTextColor),
enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Burnt.lightGrey)),
focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: Burnt.primaryLight, width: 1.0)),
),
textAlign: TextAlign.center,
),
),
);
}
How do I set the TextField width?
Edit: Turns out SliverToBoxAdapter does magic, so I've used media query to set a padding on my container. Maybe I'm not supposed to use SliverToBoxAdatper, if someone can explain the right thing to do that would be great thanks.
Edit2: Actually I came up with an even better idea, I just put my Container inside a Row and made the Row center my 100.0 wide Container
I tried something like this
Container(
margin: EdgeInsets.only(top: 30, left: 30, right: 30),
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
TextField(
decoration: InputDecoration(
hintText: "Inserisci la tua email",
hintStyle: TextStyle(fontSize: 20),
labelText: "Email",
labelStyle: TextStyle(
color: Utils.blackColor, fontSize: 20),
border: UnderlineInputBorder(
borderSide: BorderSide(color: Utils.greyColor)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
style: BorderStyle.solid))),
controller: nameController,
style: TextStyle(fontSize: 23)),
Padding(padding: EdgeInsets.only(top: 20)),
],
),
),
pu textfield and a container in a column as a child, then wrap children with flexible widget give them flex property
I want to change the color of the prefixIcon to same as the focusedInputBorder color. I have tried with ThemeData in very root of the app but no luck!
The color of the prefixIcon is controlled via the primaryColor of your theme.
You can adjust it only for the TextField by wrapping it inside of a Theme:
Theme(data: Theme.of(context).copyWith(primaryColor: Color(..)), child: TextField(..))
I am new to flutter. But when I tried as below it changes the preFixIcon color: (do not mind the indentation. It is not well indented)
TextField(
obscureText: false,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.bold
),
decoration: new InputDecoration(
prefixIcon: Icon(Icons.vpn_key,
color: Colors.white,),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all( Radius.circular(20)),
borderSide: BorderSide( width: 1, color: Colors.white),),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide( width: 1, color: Colors.white),),
hintText: 'Password',
hintStyle: TextStyle(color: Colors.white,fontSize: 15)
),
)
Below is the expected output I got:
List<FocusNode> _focusNodes = [
FocusNode(),
FocusNode(),
];
#override
void initState() {
_focusNodes.forEach((node){
node.addListener(() {
setState(() {});
});
});
super.initState();
}
TextFormField(
focusNode: _focusNodes[0],
decoration: InputDecoration(
prefixIcon: Icon(
Icons.alternate_email,
color: _focusNodes[0].hasFocus ? Colors.green : Colors.grey,
),
hintText: "Email"),
),
Flutter 3.3.2
can do like this.
Icon(
...
color: FocusScope.of(context).isFirstFocus ? colorA : colorB
...
)
Doing UI for a flutter app at uni, I just want the text typed into the TextFormField to be white. It seems unnecessarily difficult. I've tried googling etc but can't see an obvious answer.
new Theme(
// this colors the underline
data: theme.copyWith(
primaryColor: Colors.white,
hintColor: Colors.transparent,
),
child: new Padding(
padding: const EdgeInsets.fromLTRB(32.0, 40.0, 32.0, 4.0),
child: TextFormField(
key: Key('username'),
keyboardType: TextInputType.text,
controller: usernameController,
decoration: InputDecoration(
fillColor: Colors.black.withOpacity(0.6),
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(8.0),
),
borderSide: new BorderSide(
color: Colors.transparent,
width: 1.0,
),
),
labelText: 'Username',
labelStyle:
new TextStyle(color: Colors.white, fontSize: 16.0)),
style:
TextStyle(fontSize: 20.0, color: textTheme.button.color),
validator: validateUserName,
onSaved: (val) => this.loginFields._username = val),
),
),
This will do:
TextFormField(
style: TextStyle(color: Colors.white),
)
For anyone trying to do this from a material app's theme: ThemeData property, the color can be changed using the subtitle1 text theme style.
MaterialApp(
...
theme: ThemeData(
...
textTheme: const TextTheme(
...
subtitle1: const TextStyle(
color: Colors.red, // <-- TextFormField input color
),
),
),
)
Puedes usar style: TextStyle
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
TextFormField(
TextFormField(
controller: field,
style: TextStyle(fontSize: 18, color: Colors.red),
decoration: const InputDecoration(
contentPadding: const EdgeInsets.only(
left: 15,
top: 8,
right: 15,
bottom: 0
),
hintText: 'name',
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
)
]
)
)
]
)
)
You can use style inside TextFormField.
Example :
TextFormField(
style: const TextStyle(color: Colors.white),
),
You can use this to change everything
TextFormField(
//controller: _phoneController,
cursorColor: Colors.black,
keyboardType: TextInputType.text,
style: TextStyle(
color: Colors.black
),
decoration: new InputDecoration(
hintStyle: TextStyle(
color: Colors.white
),
border: InputBorder.none,
//contentPadding:
//EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
hintText: "New Playlist"),
),
Padding(
padding: EdgeInsets.all(10),
child: TextFormField(
cursorColor: Color(0XFFFFCC00)//Cursor color change
style: TextStyle(
color: Color(0XFFFFCC00),
decorationColor: Color(0XFFFFCC00),//Font color change
backgroundColor: Color(0XFFFFCC00),//TextFormField title background color change
),
),
You are changing input text color in this line TextStyle(fontSize: 20.0, color: textTheme.button.color), so in order to set in to white just use Colors.white constant instead of textTheme.button.color.
More about text style here.
Overview: The textFormField style is set to a TextStyle defined as MediumStyle. The style affects the characters being displayed in the textbox. Whereas, the labelStyle affect the font displays of the inputdecoration.
TextFormField(
style: MediumStyle,
keyboardType: TextInputType.emailAddress,
focusNode: _employeeEmailFocus,
decoration: InputDecoration(labelText: "Employee Email", labelStyle:MediumBoldStyle),
validator: (val)=> null,
onSaved:(value)=> this._employeeEmail=value,
onFieldSubmitted: (term){
_fieldFocusChange(context,_employeeEmailFocus,_passwordFocus);
},
),
decoration: const InputDecoration(
labelStyle: TextStyle(color: Colors.white),
),
If you want to use Global Change then use this:
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
textTheme: Theme.of(context).textTheme.copyWith(
subtitle1: const TextStyle(color: Colors.green),
)),
home: const MyHomePage(),
);
Otherwise you can do this:
TextFormField(
style: TextStyle(color: Colors.green),
)
If you are trying to change the textColor using the ThemeData.
Now TextField is using titleMedium.
So,
ThemeData(
...
textTheme: const TextTheme(
...
titleMedium: const TextStyle(
color: Colors.red, // <-- TextFormField input color
),
),
),
Add a inputdecoration class for textformfield
this is i think so
decoration: InputDecoration(
prefixStyle: new TextStyle(
),
I am trying to change color of the border of my TextField using a BorderSide, but it does not work.
This is my code below.
new TextField(
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(color: Colors.teal)
),
hintText: 'Tell us about yourself',
helperText: 'Keep it short, this is just a demo.',
labelText: 'Life story',
prefixIcon: const Icon(Icons.person, color: Colors.green,),
prefixText: ' ',
suffixText: 'USD',
suffixStyle: const TextStyle(color: Colors.green)),
)
)
Screenshot of the result is shown below.
The new way to do it is to use enabledBorder like this:
new TextField(
decoration: new InputDecoration(
enabledBorder: const OutlineInputBorder(
borderSide: const BorderSide(color: Colors.grey, width: 0.0),
),
focusedBorder: ...
border: ...
),
)
That is not changing due to the default theme set to the screen.
So just change them for the widget you are drawing by wrapping your TextField with new ThemeData()
child: new Theme(
data: new ThemeData(
primaryColor: Colors.redAccent,
primaryColorDark: Colors.red,
),
child: new TextField(
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(color: Colors.teal)),
hintText: 'Tell us about yourself',
helperText: 'Keep it short, this is just a demo.',
labelText: 'Life story',
prefixIcon: const Icon(
Icons.person,
color: Colors.green,
),
prefixText: ' ',
suffixText: 'USD',
suffixStyle: const TextStyle(color: Colors.green)),
),
));
Well, I really don't know why the color assigned to border does not work. But you can control the border color using other border properties of the textfield. They are:
disabledBorder: Is activated when enabled is set to false
enabledBorder: Is activated when enabled is set to true (by default enabled property of TextField is true)
errorBorder: Is activated when there is some error (i.e. a failed validate)
focusedBorder: Is activated when we click/focus on the TextField.
focusedErrorBorder: Is activated when there is error and we are currently focused on that TextField.
A code snippet is given below:
TextField(
enabled: false, // to trigger disabledBorder
decoration: InputDecoration(
filled: true,
fillColor: Color(0xFFF2F2F2),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(width: 1,color: Colors.red),
),
disabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(width: 1,color: Colors.orange),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(width: 1,color: Colors.green),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(width: 1,)
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(width: 1,color: Colors.black)
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(width: 1,color: Colors.yellowAccent)
),
hintText: "HintText",
hintStyle: TextStyle(fontSize: 16,color: Color(0xFFB3B1B1)),
errorText: snapshot.error,
),
controller: _passwordController,
onChanged: _authenticationFormBloc.onPasswordChanged,
obscureText: false,
),
disabledBorder:
enabledBorder:
focusedBorder:
errorBorder:
errorFocusedBorder:
The code in which you change the color of the primaryColor andprimaryColorDark does not change the color inicial of the border, only after tap the color stay black
The attribute that must be changed is hintColor
BorderSide should not be used for this, you need to change Theme.
To make the red color default to put the theme in MaterialApp(theme: ...) and to change the theme of a specific widget, such as changing the default red color to the yellow color of the widget, surrounds the widget with:
new Theme(
data: new ThemeData(
hintColor: Colors.yellow
),
child: ...
)
Below is the code and gif:
Note that if we define the primaryColor color as black, by tapping the widget it is selected with the color black
But to change the label and text inside the widget, we need to set the theme to InputDecorationTheme
The widget that starts with the yellow color has its own theme and the widget that starts with the red color has the default theme defined with the function buildTheme()
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
ThemeData buildTheme() {
final ThemeData base = ThemeData();
return base.copyWith(
hintColor: Colors.red,
primaryColor: Colors.black,
inputDecorationTheme: InputDecorationTheme(
hintStyle: TextStyle(
color: Colors.blue,
),
labelStyle: TextStyle(
color: Colors.green,
),
),
);
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
theme: buildTheme(),
home: new HomePage(),
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
String xp = '0';
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(),
body: new Container(
padding: new EdgeInsets.only(top: 16.0),
child: new ListView(
children: <Widget>[
new InkWell(
onTap: () {},
child: new Theme(
data: new ThemeData(
hintColor: Colors.yellow
),
child: new TextField(
decoration: new InputDecoration(
border: new OutlineInputBorder(),
hintText: 'Tell us about yourself',
helperText: 'Keep it short, this is just a demo.',
labelText: 'Life story',
prefixIcon: const Icon(Icons.person, color: Colors.green,),
prefixText: ' ',
suffixText: 'USD',
suffixStyle: const TextStyle(color: Colors.green)),
)
)
),
new InkWell(
onTap: () {},
child: new TextField(
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(color: Colors.teal)
),
hintText: 'Tell us about yourself',
helperText: 'Keep it short, this is just a demo.',
labelText: 'Life story',
prefixIcon: const Icon(Icons.person, color: Colors.green,),
prefixText: ' ',
suffixText: 'USD',
suffixStyle: const TextStyle(color: Colors.green)),
)
)
],
),
)
);
}
}
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(color: Colors.red)
),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4.0)),
borderSide: BorderSide(width: 1.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blueGrey),
),
errorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.redAccent),
),
focusedErrorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.orangeAccent),
),
disabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
contentPadding: EdgeInsets.all(10.0),
hintText: 'Tell us about yourself',
helperText: 'Keep it short, this is just a demo.',
labelText: 'Life story',
prefixIcon: const Icon(
Icons.person,
color: Colors.green,
),
prefixText: ' ',
suffixText: 'USD',
suffixStyle: const TextStyle(color: Colors.green),
),
),
The best and most effective solution is just adding theme in your main class and add input decoration like these.
theme: ThemeData(
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.pink)
)
),
)
Inside your lib file
Create a folder called colors.
Inside the colors folder create a dart file and name it color.
Paste this code inside it
const MaterialColor primaryOrange = MaterialColor(
_orangePrimaryValue,
<int, Color>{
50: Color(0xFFFF9480),
100: Color(0xFFFF9480),
200: Color(0xFFFF9480),
300: Color(0xFFFF9480),
400: Color(0xFFFF9480),
500: Color(0xFFFF9480),
600: Color(0xFFFF9480),
700: Color(0xFFFF9480),
800: Color(0xFFFF9480),
900: Color(0xFFFF9480),
},
);
const int _orangePrimaryValue = 0xFFFF9480;
Go to your main.dart file and place this code in your theme
theme:ThemeData(
primarySwatch: primaryOrange,
),
Import the color folder in your main.dart like this import 'colors/colors.dart';
border: OutlineInputBorder(borderSide: BorderSide(color: CustomColors.primaryColor),),
We have tried custom search box with the pasted snippet. This code will useful for all kind of TextFiled decoration in Flutter. Hope this snippet will helpful for others.
Container(
margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
child: new Theme(
data: new ThemeData(
hintColor: Colors.white,
primaryColor: Colors.white,
primaryColorDark: Colors.white,
),
child:Padding(
padding: EdgeInsets.all(10.0),
child: TextField(
style: TextStyle(color: Colors.white),
onChanged: (value) {
filterSearchResults(value);
},
controller: editingController,
decoration: InputDecoration(
labelText: "Search",
hintText: "Search",
prefixIcon: Icon(Icons.search,color: Colors.white,),
enabled: true,
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.all(Radius.circular(25.0))),
border: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white, width: 0.0),
borderRadius: BorderRadius.all(Radius.circular(25.0)))),
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 40),
child: TextField(
cursorColor: Color.fromRGBO(25, 118, 218, 1),
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide:
new BorderSide(color: Color.fromRGBO(25, 118, 218, 1)),
),
focusedBorder: new OutlineInputBorder(
borderSide:
new BorderSide(color: Color.fromRGBO(25, 118, 218, 1)),
),
labelText: "Edit Phone",
labelStyle: TextStyle(
color: Colors.grey,
),
prefixIcon: const Icon(
Icons.phone_outlined,
color: Color.fromRGBO(25, 118, 218, 1),
),
),
),
),
Thank me later :)
You can use this code for bottom sheets as well as for normal text fields :
class TextFieldForDropDown extends StatelessWidget {
final String title;
final String hintText;
final TextEditingController textEditingController;
bool isPassword;
final Function onTap;
final bool enable;
TextFieldForDropDown({this.title, this.hintText, this.textEditingController, this.isPassword = false, this.onTap, this.enable});
#override
Widget build(BuildContext context) {
var titleTextStyle = TextStyle(
color: Color(0xff9098C8),
fontSize: 12,
fontWeight: FontWeight.w400,
fontFamily: "Muli",
);
var textFieldTextStyle = TextStyle(
color: Colors.white,
fontSize: 14,
fontWeight: FontWeight.w400,
fontFamily: "Muli",
);
var borderSides = OutlineInputBorder(borderSide: new BorderSide(color: Color(0xff38406B)));
var borderSides1 = OutlineInputBorder(borderSide: new BorderSide(color: Color(0xffdae4ff)));
return InkWell(
onTap: onTap,
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(this.title, style: titleTextStyle),
SizedBox(height: 8),
TextFormField(
enabled: enable,
// onTap: onTap,
obscureText: isPassword,
style: textFieldTextStyle,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
hintText: this.hintText,
hintStyle: titleTextStyle,
border: textEditingController.text != "" ? borderSides1 :borderSides,
enabledBorder: textEditingController.text != "" ? borderSides1 :borderSides,
disabledBorder: textEditingController.text != "" ? borderSides1 :borderSides,
focusedBorder: OutlineInputBorder(borderSide: new BorderSide(color: Color(0xffdae4ff))),
),
controller: textEditingController,
)
],
),
),
);
}
}
and use like this :
TextFieldForDropDown(
title: 'Phone Number*',
hintText: '+123-22-223-00',
textEditingController: viewModel.phoneController,
),