Flutter Custom Widget Property Transfer - dart

I want to create a custom widget for TextField widget and I want to transfer all properties to my custom widget. I have used in React Native ...props syntax.
My custom Widget:
return Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.grey,
width: 1,
),
borderRadius: BorderRadius.all(Radius.circular(5))),
margin: margin,
padding: EdgeInsets.symmetric(horizontal: 10),
child: TextField(
decoration: InputDecoration(
labelStyle: TextStyle(color: Colors.black),
labelText: label,
border: InputBorder.none,
counterText: ""),
textDirection: TextDirection.ltr,
//I want to put here custom widget properties
),
);
I want to use custom widget like:
Input(
label: "Şifre",
margin: EdgeInsets.only(bottom: marginBottom),
obscureText: true, //**
textInputAction: TextInputAction.send, //**
controller: passwordController, //**
)
I want to transfer commented ** properties to custom widget commented line

You are using a TextField but look for a TextFormField instead. It gathers all the properties you are asking for.
TextFormField(
decoration: InputDecoration(
labelStyle: TextStyle(color: Colors.black),
labelText: label,
border: InputBorder.none,
counterText: ""),
obscureText: true,
textInputAction: TextInputAction.send,
controller: passwordController,
);

Related

Can't exit from a TextField flutter

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,
),
),
),

How to set TextField width?

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

How to set text in one line for flutter

I have next flutter/dart code for make in one line 4 text field:
Container(
margin: EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
width: 100.0,
child: TextField(
maxLength: 2,
onChanged: (value) {
card.expMonth = int.parse(value);
},
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'month'),
)),
Text("/",
style: TextStyle(fontSize: 36),
textAlign: TextAlign.center),
Container(
width: 100.0,
child: TextField(
maxLength: 2,
onChanged: (value) {
card.expYear = int.parse(value);
},
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'year'),
)),
Container(
padding: EdgeInsets.only(left: 80.0),
width: 180.0,
child: TextField(
maxLength: 3,
onChanged: (value) {
card.cvc = int.parse(value);
},
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'cvc'),
)),
],
),
)
and he see this:
How make all text fields in one line. In android app i simple use guideline or other simple paths. And here i smash with flutter reality where different textfields have different visibility with the same, at first glance, the parameters
UPD.: I make crutch solution:
Padding(
padding: EdgeInsets.only(bottom: 20),
child: Text("/",
style: TextStyle(fontSize: 36),
textAlign: TextAlign.center),
),
but it's stupid how i think
This is how I would do it:
Row(children: <Widget>[
Expanded(
child: TextField(
inputFormatters: [LengthLimitingTextInputFormatter(2)],
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'month'),
)),
Expanded(
child: Text("/",
style: TextStyle(fontSize: 36),
textAlign: TextAlign.center)),
Expanded(
child: TextField(
inputFormatters: [LengthLimitingTextInputFormatter(2)],
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'year'),
)),
Expanded(
child: TextField(
inputFormatters: [LengthLimitingTextInputFormatter(3)],
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'cvc'),
)),
])
Which produces this:
I used inputFormatters: [LengthLimitingTextInputFormatter(2)] instead of maxLength so that the user is still only able to enter in a limited length but you don't get the counter below the TextField as it looks neater.

How to align Hint Text into centre in Text Field?

i am trying to add alignment to the hint-text inside Text-field.but it is not working. How to bring it to centre??
How to write text in curved shape???
Container(
child: new TextFormField(
controller: _pass,
textAlign: TextAlign.center,
decoration: new InputDecoration.collapsed(
hintText: " PASSWORD", ), )),
use textAlign: TextAlign.center, inside TextFormField or textfield
here is the code
The Output Like This
My problem was the auto content padding:
TextField(
textAlign: TextAlign.center,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(0),
hintText: "hola mundo",
),
),
Happy coding.
The textHint will have the same Alignment as the input text. So you can try the code below to achieve what you're looking for:
TextFormField(
keyboardType: TextInputType.number,
maxLength: 5,
textAlign: TextAlign.center,
autofocus: true,
initialValue: '',
decoration: InputDecoration(
hintText: 'some hint',
counterText: "",
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5.0)),
),
);
Note: specific case, but might help others who tried everything else from this thread.
I had a design from which I copied all the paddings and alignments, but I somehow couldn't center the hint text, it was always a few pixels closer to the top edge.
The only thing that worked was finding out that there was a specific text height set in the design. After I applied that, everything aligned perfectly.
InputDecoration(
hintStyle: TextStyle(
fontSize: _smallFontSize, // or whatever
height: 1.4, // <----- this was the key
),
// other properties...
);
you can add textAlign: TextAlign.center to your code
here is my code and its works:
new Padding(
padding: new EdgeInsets.all(30.0),
child:
new TextField(
textAlign: TextAlign.center,
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(color: Colors.teal)),
hintText: 'REF: 2',
),
),
),
all of the answers work for the fixed-sized text fields but in my case, I m using a dynamically sized container that parents my text field
AnimatedContainer(
width: (!widget.scrollController.hasClients ||
widget.scrollController.positions.length > 1)
? MediaQuery.of(context).size.width.toDouble()
: max(
MediaQuery.of(context).size.width -
widget.scrollController.offset.roundToDouble(),
(MediaQuery.of(context).size.width - 80.w).toDouble()),
duration: const Duration(milliseconds: 150),
constraints: BoxConstraints(maxHeight: 40.h),
margin: EdgeInsets.zero,
child: TextField(
controller: controller,
expands: true,
minLines: null,
maxLines: null,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
filled: true,
fillColor: MyAppColors.primaryColor1,
border: UnderlineInputBorder(
borderRadius:
BorderRadius.circular(15.w)),
prefixIcon: Icon(
Icons.search,
size: 20.w,
color: Theme.of(context).accentColor,
),
hintText: 'Songs, albums or artists',
contentPadding: EdgeInsets.zero,
hintStyle:
Theme.of(context).textTheme.subtitle2!.copyWith(
color: Colors.white.withOpacity(0.8),
fontSize: 18.sp,
)),
),
);
the solution that worked for me is that I set expand parameter to true and set these parameters:
minLines: null,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.center
the min and max line must be set to null in order to expand.
that is the only solution I found cuz in another way my hint text gain extra padding even I set the content padding to zero my hint text is not centered in other screen sizes.
You can use following attribute of TextFormField:
textAlign: TextAlign.center
This used to work earlier but now it doesn't. There's a recently opened issue in flutter repo which you can follow
Edit
Now works as expected.
textAlign: TextAlign.center will move your focus too, i fix it with some empty spaces in hint text
hintText: ' this is centred text)',

How to make the background of TextField rectangular box shape?

I tried making Container widget with rectangle shape in TextField. It doesn't show my text inside container. Rather the box shape comes above TextField.
Here's my code:
new Container(
height: 10.0,
width: 20.0,
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
border: new Border.all(
color: Colors.black,
width: 1.0,
),
),
child: new TextField(
textAlign: TextAlign.center,
decoration: new InputDecoration(
hintText: '1',
border: InputBorder.none,
),
),
),
Just remove the height and width property of the container.
Example:
new Container(
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
border: new Border.all(
color: Colors.black,
width: 1.0,
),
),
child: new TextField(
textAlign: TextAlign.center,
decoration: new InputDecoration(
hintText: '1',
border: InputBorder.none,
),
),
)
or else just specify the border property of InputDecoration like
new TextField(
textAlign: TextAlign.center,
decoration: new InputDecoration(
hintText: '1',
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(0.0),
),
borderSide: new BorderSide(
color: Colors.black,
width: 1.0,
),
),
),
)
Hope that helps
TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey, width: 2.0),
),
hintText: 'Email',
prefixIcon: Icon(Icons.mail_outline),
),
),
Output:
TextField(
decoration: InputDecoration(
filled: true, // <- this is required.
border: const OutlineInputBorder(
borderRadius: kStadiumBorderRadius,
borderSide: BorderSide.none,
),
),
);

Resources