Related
i want to change my container's border color when the TextField is on focus. Is in Flutter this thing is ilegal? i've tried several way and it color can't be change when TextField is on focus or loss focus.
open it https://i.postimg.cc/767RT4qW/Screenshot-45.png
here's my container and textfield code:
Container(
decoration: BoxDecoration(
border: Border.all(
color: _myFocusNode.hasFocus
? Colors.black38
: Colors.blue,
width: 2,
),
borderRadius: BorderRadius.circular(8),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Padding(
padding: EdgeInsets.symmetric(
horizontal: 10,
),
child: Icon(
Icons.alternate_email_rounded,
color: Colors.black38,
),
),
Flexible(
child: TextField(
focusNode: _myFocusNode,
autofocus: true,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
),
cursorHeight: 24,
cursorColor: Colors.grey,
decoration: const InputDecoration(
border: InputBorder.none,
suffixIcon: SizedBox(
height: 24,
width: 24,
),
hintText: 'Input your email',
hintStyle: TextStyle(
color: Colors.black38,
),
),
),
),
],
),
),
],
),
Try this:
FocusScope(
child: Focus(
onFocusChange: (focus) => print("focus: $focus"),
child: TextField(
decoration: const InputDecoration(labelText: 'City'),
)
)
)
try this
TextFormField(
decoration: InputDecoration(
labelText: "Name",
fillColor: Colors.white,
focusedBorder:OutlineInputBorder(
borderSide: const BorderSide(color: Colors.red, width: 2.0),
borderRadius: BorderRadius.circular(25.0),
),
),
)
I am new to Flutter I have the following activity, when I click in Text Field Keyboard appears and then the warning e.g. BOTTOM OVERFLOWED BY 84 PIXLES also appears how can i be able to resolve this issue? when I tried SingleChildScrollView then empty area("where there is no Widgets") of activity gone white. Is there any Widget that is missing or i made a mistake in my code?
My Activity
here is my code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:marshal/Payment.dart';
import 'bottomnavigationbar.dart';
class Payment2 extends StatefulWidget {
#override
_Payment2State createState() => _Payment2State();
}
class _Payment2State extends State<Payment2> {
#override
Widget build(BuildContext context) {
final PaymentButton = Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Colors.red,
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: () {
Route route = MaterialPageRoute(builder: (context) => Paymentdone());
Navigator.pushReplacement(context, route);
},
child: Text("Payment",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white, fontWeight: FontWeight.w800)),
),
);
return Scaffold(
appBar: AppBar(
title: Text("Payment"),
centerTitle: true,
),
body: Container(
padding: EdgeInsets.all(16),
color: Colors.black,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.all(25),
),
Text(
"ENTER YOUR CARD DETAILS",
style: TextStyle(
fontWeight: FontWeight.w400,
color: Colors.white,
fontSize: 16),
),
Padding(
padding: EdgeInsets.all(5),
),
Card(
color: Color(0xFF1E1E1E),
child: ListTile(
leading: CircleAvatar(),
title: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"MasterCard",
style: TextStyle(fontSize: 16, color: Colors.white),
),
Text(
'90 \u0024',
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.all(12),
),
Card(
color: Color(0xFF1E1E1E),
child: cardnumber(),
),
Row(
children: [
Container(
//height: 60,
width: MediaQuery.of(context).size.width * 0.45,
color: Color(0xFF1E1E1E),
child: TextField(
style: style,
maxLength: 5,
cursorColor: Colors.red,
textAlign: TextAlign.left,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'MM/YY',
hintStyle: TextStyle(fontSize: 16, color: Colors.white),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
),
),
),
Padding(
padding: EdgeInsets.all(1),
),
Container(
// height: 50,
width: MediaQuery.of(context).size.width * 0.44,
color: Color(0xFF1E1E1E),
child: TextField(
style: style,
maxLength: 3,
cursorColor: Colors.red,
textAlign: TextAlign.left,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'CVV',
hintStyle: TextStyle(fontSize: 16, color: Colors.white),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
),
),
),
],
),
Container(
height: 100,
),
PaymentButton,
],
),
),
bottomNavigationBar: BottomNavigation(),
);
}
Widget cardnumber() {
return TextField(
style: style,
maxLength: 16,
cursorColor: Colors.red,
textAlign: TextAlign.left,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'XXXX XXXX XXXX 1234',
hintStyle: TextStyle(fontSize: 16, color: Colors.white),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
),
);
}
TextStyle style =
TextStyle(fontFamily: 'Montserrat', color: Colors.white, fontSize: 16.0);
}
using a SingleChildScrollView is the right way to go.
in order to fix the issue you talked about, delete the color attribute from the container, and move it to the scaffold background color attribute:
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: Text("Payment"),
centerTitle: true,
),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(16),
child: Column(
// the rest of the widgets...
),
),
);
Simply add resizeToAvoidBottomInset: false to your Scaffold
Also you can wrap the child inside SingleChildScrollView
Use SingleChildScrollView in body. To avoid white portion problem wrap it inside a Stack.
Stack(
children: <Widget>[
Container(
padding: EdgeInsets.all(16),
color: Colors.black,
),
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.all(25),
),
Text(
"ENTER YOUR CARD DETAILS",
style: TextStyle(
fontWeight: FontWeight.w400,
color: Colors.white,
fontSize: 16),
),
Padding(
padding: EdgeInsets.all(5),
),
Card(
color: Color(0xFF1E1E1E),
child: ListTile(
leading: CircleAvatar(),
title: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"MasterCard",
style: TextStyle(fontSize: 16, color: Colors.white),
),
Text(
'90 \u0024',
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.all(12),
),
Card(
color: Color(0xFF1E1E1E),
child: cardnumber(),
),
Row(
children: [
Container(
//height: 60,
width: MediaQuery.of(context).size.width * 0.45,
color: Color(0xFF1E1E1E),
child: TextField(
style: style,
maxLength: 5,
cursorColor: Colors.red,
textAlign: TextAlign.left,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'MM/YY',
hintStyle: TextStyle(fontSize: 16, color: Colors.white),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
),
),
),
Padding(
padding: EdgeInsets.all(1),
),
Container(
// height: 50,
width: MediaQuery.of(context).size.width * 0.44,
color: Color(0xFF1E1E1E),
child: TextField(
style: style,
maxLength: 3,
cursorColor: Colors.red,
textAlign: TextAlign.left,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'CVV',
hintStyle: TextStyle(fontSize: 16, color: Colors.white),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
),
),
),
],
),
Container(
height: 100,
),
PaymentButton,
],
),
),
)
],
)
I want to develop the Following design in Flutter, I am using Flutter default colors, the I am facing is that when I use Container having color is black and then want to place some Widgets inside of that container having Lighter color than Black as you can in Following image are not visible how to resolve this?
Here is some Code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:marshal/Payment.dart';
import 'bottomnavigationbar.dart';
class Payment2 extends StatefulWidget {
#override
_Payment2State createState() => _Payment2State();
}
class _Payment2State extends State<Payment2> {
#override
Widget build(BuildContext context) {
final PaymentButton = Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Colors.red,
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: () {
Route route = MaterialPageRoute(builder: (context) => Paymentdone());
Navigator.pushReplacement(context, route);
},
child: Text("Payment",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white, fontWeight: FontWeight.w800)),
),
);
return Scaffold(
appBar: AppBar(
title: Text("Payment"),
centerTitle: true,
),
body: Container(
padding: EdgeInsets.all(12),
color: Colors.black,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"ENTER YOUR CARD DETAILS",
style: TextStyle(
fontWeight: FontWeight.w400,
color: Colors.white,
fontSize: 16),
),
Card(
color: Colors.blueGrey,
child: ListTile(
leading: CircleAvatar(),
title: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"MasterCard",
style: TextStyle(fontSize: 16, color: Colors.white),
),
Text(
'90 \u0024',
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
),
),
),
Card(
color: Colors.blueGrey,
child: emailField(),
),
//
// Row(
// crossAxisAlignment: CrossAxisAlignment.center,
// children: <Widget>[
// Card(
// child: Exp_Date(),
// ),
// Card(
// child: CVV(),
// )
// ],
// ),
TextField(
style: style,
maxLength: 5,
cursorColor: Colors.red,
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'MM/YY',
hintStyle: TextStyle(fontSize: 16, color: Colors.white),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
),
),
// Exp_Date(),
PaymentButton,
],
),
),
bottomNavigationBar: BottomNavigation(),
);
}
Widget emailField() {
return TextFormField(
//obscureText: true,
style: style,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 0, 0, 10.0),
//labelText: "Card Number",
hintText: 'xxxx xxxx 1234',
),
);
}
Widget Exp_Date() {
return TextFormField(
obscureText: true,
style: style,
decoration: InputDecoration(
// contentPadding: EdgeInsets.fromLTRB(20.0, 0, 0, 10.0),
// labelText: "Exp Date",
hintText: "MM/YY",
),
);
}
Widget CVV() {
return TextFormField(
obscureText: true,
style: style,
decoration: InputDecoration(
// contentPadding: EdgeInsets.fromLTRB(20.0, 0, 0, 10.0),
//labelText: "CVV",
hintText: "CVV",
),
);
}
TextStyle style =
TextStyle(fontFamily: 'Montserrat', color: Colors.white, fontSize: 16.0);
}
Note
I don't have any issues with BottomNavigationBar
Use Color(0xFF1E1E1E) instead of Colors.blueGrey.
I need a textField that has a suffixIcon, but after click on that icon I don't need to open keyboard. How I can do it alternatively without suffixIcon?
Container(
child: Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextField(),
IconButton(
icon: Icon(Icons.image),
onPressed: () {
// do something
},
),
],
),
)
Tested and confirmed, exactly what u want.
Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextField(
keyboardType: TextInputType.text,
style: Theme.of(context).textTheme.body1,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
contentPadding: const EdgeInsets.fromLTRB(6, 6, 48, 6), // 48 -> icon width
),
),
IconButton(
icon: Icon(Icons.dialpad, color: const Color(0xfff96800)),
onPressed: () {
FocusScope.of(context).requestFocus(FocusNode());
// Your codes...
},
),
],
),
There is one more possible fix for this issue - taken from here: https://github.com/flutter/flutter/issues/39376 - use the standard TextField with a button as suffixIcon and then, the magic trick:
InputDecoration(labelText: "Email address",
border: OutlineInputBorder(),
suffixIcon: IconButton(
iconSize: 40,
icon: Icon(Icons.fingerprint),
onPressed: () async {
focusNode.unfocus();
focusNode.canRequestFocus = false;
await performBiometricLogin();
focusNode.canRequestFocus = true;
},
),
),
Two things that you have to be aware in this case:
a) declare focusNode in your widget (I'm doing it in the state class of my statefull widget) and then use it for your textfield:
FocusNode focusNode = FocusNode();
and in the TextField use the property called focusNode:
focusNode: focusNode,
b) if you don't do any async operation in the onPressed event handler, then you have to follow exactly the logic from the github issue - enable canRequestFocus after some time:
Future.delayed(Duration(milliseconds: 100), () {
widget.textFieldFocusNode.canRequestFocus = true;
});
Hopefully it will help others as it helped me.
Thanks.
Use readOnly, don't use enabled.
Click and not open the keyboard? If so, just create a class and assign it to focusNode, setting hasFocus to false, like this:
class AlwaysDisabledFocusNode extends FocusNode {
#override
bool get hasFocus => false;
}
new TextField(
focusNode: AlwaysDisabledFocusNode(),
onTap: () {},
keyboardType: TextInputType.text,
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(Icons.apps),
hintText: 'Password'),
style: Theme.of(context).textTheme.body1,
),
With readOnly: true it changes icon color on click
new TextField(readOnly: true,
//focusNode: AlwaysDisabledFocusNode(),
onTap: () {},
keyboardType: TextInputType.text,
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(Icons.apps),
hintText: 'Password'),
style: Theme.of(context).textTheme.body1,
),
I think then you have to put a Row with a TextField and an IconButton, with separate actions.
new Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Expanded(
child: Padding(
child: new TextField(
onTap: () {//action of TextField
},
keyboardType: TextInputType.text,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'Password'),
style: Theme.of(context).textTheme.body1,
),
padding: EdgeInsets.only(left: 40),
)),
IconButton(
icon: Icon(Icons.apps),
onPressed: () {//action of iconbutton
},
)
],
)
I have achieved the same thing
Container(
height: 40,
child: Stack(
children: <Widget>[
TextField(
controller: textEditingController,
keyboardType: TextInputType.text,
decoration: InputDecoration(
prefixIcon: Icon(HelageeIcons.search_icon_of_search_box),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff575656),
),
borderRadius: const BorderRadius.all(
const Radius.circular(50.0),
),
),
hintStyle:
TextStyle(color: Color(0xffADADAC), fontSize: 14),
hintText: "Quick Search",
),
),
Positioned(
right: 0,
child: Container(
height: 40,
width: 40,
child: Container(
child: Material(
shape: CircleBorder(),
color: Colors.transparent,
child: InkWell(
customBorder: CircleBorder(),
onTap: () {},
splashColor: Colors.grey,
child: Icon(Icons.keyboard))),
),
),
),
],
),
),
add suffix icon and suffix icon click on text filed, in my case I have used as below
TextFormField(
textInputAction: TextInputAction.done,
maxLines: 1,
obscureText: _obscureText,
autofocus: false,
focusNode: _passwordFocus,
style: TextStyle(fontSize: 17.0, color: Colors.black),
onFieldSubmitted: (term) {
_passwordFocus.unfocus();
_validateAndSubmit();
},
decoration: InputDecoration(
hintText: HINT_PASSWORD,
hintStyle: TextStyle(fontSize: 17.0, color: Colors.black54),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.black87),
),
errorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
),
disabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.black87),
),
focusedErrorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
),
labelText: HINT_PASSWORD,
labelStyle: TextStyle(fontSize: 17.0, color: Colors.black),
errorStyle: TextStyle(fontSize: 12.0, color: Colors.red),
prefixIcon: Icon(
Icons.lock,
color: themeColor,
),
/// magic is here suffix ixon click
suffixIcon: IconButton(
icon: Icon(
// Based on passwordVisible state choose the icon
_obscureText ? Icons.visibility : Icons.visibility_off,
color: themeColor,
),
onPressed: () {
// Update the state i.e. toogle the state of passwordVisible variable
setState(() {
_obscureText ? _obscureText = false : _obscureText = true;
});
},
),
),
validator: validatePassword,
onSaved: (value) => _password = value,
)
This should work
Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextFormField(
),
FlatButton(
onPressed: () {
_openOrhidePassword();
},
child: Image(
height: 24.0,
width: 24.0,
image: AssetImage('images/Eye.png'),
),
),
],
),
In my case image size was another problem. When I provided the dimension it worked well with other widget.
I am new to Flutter Development & tried certain work around but nothing really helped. I want my text to be center vertically in TextFormField in flutter.
textAlign: TextAlign.start brings my text to left but I want them to be center vertically also.
textAlign: TextAlign.center brings my text to center but I want them to be start from left also.
This is what I am getting,
This is what I want,
Snippet of my code:
#override
Widget build(BuildContext context) {
return new Form(
key: _formKey,
child: new SingleChildScrollView(
child: new Theme(
data: new ThemeData(
primaryColor: const Color(0xFF102739),
accentColor: const Color(0xFF102739),
hintColor: const Color(0xFF102739)),
child: new Column(
children: <Widget>[
new TextFormField(
textAlign: TextAlign.center,
maxLines: 1,
autofocus: true,
keyboardType: TextInputType.emailAddress,
style: new TextStyle(
color: const Color(0xFF0f2638),
fontFamily: 'ProximaNova',
fontStyle: FontStyle.italic,
fontSize: 16.0),
decoration: new InputDecoration(
contentPadding: const EdgeInsets.only(
top: 8.0, bottom: 8.0, left: 10.0, right: 10.0),
hintText: "YOUR E-MAIL",
hintStyle: new TextStyle(
color: const Color(0x703D444E),
fontFamily: 'ProximaNova',
fontStyle: FontStyle.italic,
fontSize: 16.0),
border: new UnderlineInputBorder(
borderSide: new BorderSide(
color: const Color(0xFF0f2638), width: 0.2))),
validator: _validateEmail,
),
],
),
)));
}
There is no vertical align option for that. All you need to do is to set contentPadding for positioning:
TextFormField(
decoration: InputDecoration(
contentPadding: EdgeInsets.zero
)
Screenshot
Use the following property:
TextFormField(
textAlignVertical: TextAlignVertical.center,
//Remaining code.
),
The way I did it using "alignment: Alignment.center," for Container with "InputDecoration.collapsed" & ".copyWith(isDense: true)":
Scaffold(
body: Container(
color: Colors.white,
width: double.infinity,
height: double.infinity,
child: Center(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 16.0),
child: Container(
height: 50.0,
alignment: Alignment.center,
color: Colors.greenAccent,
child: TextFormField(
cursorColor: Colors.red,
decoration: InputDecoration.collapsed(
hintText: 'Search',
).copyWith(isDense: true),
),
),
),
),
),
),
So it worked perfecly for me:
TextFormField(
// setting text alignment
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
// decorating and styling your Text
isCollapsed: true,
contentPadding: EdgeInsets.zero,
),
),
TextFormField(
textAlign: TextAlign.center,
)
You can use decoration attribute like this:
decoration: InputDecoration(
isCollapsed: true,
isDense: true,
contentPadding: EdgeInsets.symmetric(horizontal: 5),
),