More space between LabelText and HintText in TextFormField (Flutter) - flutter-layout

I want to increase the distance between labelText and hintText in a TextFormField, and contentPadding: EdgeInsets.fromLTRB(x, x, x, x), doesn't help me at all, it does apply a padding but those elements remain together.
My preview:

You can do this with hintStyle and LabelStyle, set height attribute to what you want
You can see my test result in picture
code snippet
TextFormField(
decoration: const InputDecoration(
icon: Icon(Icons.person),
hintText: 'What do people call you?',
hintStyle: TextStyle(height:7, fontWeight: FontWeight.bold),
labelText: 'Name *',
labelStyle: TextStyle(height:5, fontWeight: FontWeight.bold),
),
full code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
#override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
icon: Icon(Icons.person),
hintText: 'What do people call you?',
hintStyle: TextStyle(height:7, fontWeight: FontWeight.bold),
labelText: 'Name *',
labelStyle: TextStyle(height:5, fontWeight: FontWeight.bold),
),
onSaved: (String value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
validator: (String value) {
return value.contains('#') ? 'Do not use the # char.' : null;
},
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

Provided you are using a OutlineInputBorder, you can use textAlignVertical: TextAlignVertical.bottom, together with
decoration: InputDecoration(
contentPadding: EdgeInsets.only(top: 28),
border: OutlineInputBorder(
)
)
Adjust the contentPadding to your desired height.

I needed a TextField just with a label and no hint. My workaround to create space between was to provide the hint as an empty string. Hope this helps took me like 20 minutes to figure out 🙄
TextField(
decoration: InputDecoration(
hintStyle: TextStyle(
height: 3.0, // sets the distance between label and input
),
hintText: '', // needed to create space between label and input
labelStyle: TextStyle(
color: kWhiteTextColor,
fontSize: 20.0,
),
labelText: 'My first name is',
),
),

TextFormField(
controller: controller.fullNameController,
focusNode: controller.fullNameFocusNode,
enabled: false,
decoration: InputDecoration(
alignLabelWithHint: true,
labelText: fullNameLabel,
hintText: fullNameLabel,
labelStyle: TextStyle(
fontSize: 18,
height: 0.5, // Tweak this 1,2 moves label bottom-> -1,-2 moves label upwards
fontWeight: FontWeight.w500,
),
floatingLabelBehavior: FloatingLabelBehavior.always),
),

Figured it out!
Don't use labelText Parameter, instead, use the label parameter, wrap your text widget with padding widget then specify the amount of padding you want between label and text form field.
Like so
label: Padding( padding: Const EdgeInsets.all(10), child:Text('your label text')

Related

Flutter textformfield cursor moving to front of the text

I am using a textformfield in my application. The cursor keeps moving to the left most side of the text. To solve this issue I added a listener:
final TextEditingController _controller = TextEditingController();
#override
Widget build(BuildContext context) {
_controller.text = widget.localLayoutItem.cx.toInt().toString();
_controller.addListener(() {
_controller.value = _controller.value.copyWith(
text: _controller.text,
selection: TextSelection.fromPosition(TextPosition(
offset: _controller.text.length,
)),
composing: TextRange.empty,
);
});
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
controller: _controller,
decoration: const InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 10),
iconColor: Colors.grey,
),
onChanged: (value) {
FocusedItemModel.of(context).redraw();
widget.localLayoutItem.cx = double.parse(value);
},
),
],
);
}
The textfield value is directly connected to the widgets x-axis value so if the widget moves I need to update the textfield value as well and vice-versa.
Problem:
After adding the listener the cursor is staying on the right most but I lost functionality like using the arrow key to a desired character or deleting all characters.
how to solve this issue?
update the text editing controller like this
TextEditingController questionController = TextEditingController.fromValue(TextEditingValue(
text: somevalue,
selection: TextSelection( baseOffset: somevalue.length,
extentOffset: somevalue.length),
)

Copy values from a spreadsheet to multiple textFields in flutter

EDIT: Let me clarify more the point I am struggling to achieve as it seems not to be clear. I already built a table with TextFields as you see in my code, and I tried the excel package and I tried several other packages, however nothing allows me to COPY the input from an external spreadsheet to my web app directly. The user will eventually have to manually type all the digital sample values one by one or they have to upload an external excel sheet to the site. These samples can be as big as several thousand samples in some cases, the only option is to copy them from their original spreadsheet or text source to the web app.
In my flutter web app, I want to get input from the users: they input time samples of any digital signal into a series of TextFields then the site will plot these samples and do some processing on them etc..
The problem is that digital signals are usually long and users will want to copy them from other text files or spreadsheets rather than manually typing them one by one in the browser.
Below is part of the code I used so far
class InputData extends StatefulWidget {
#override
State<InputData> createState() => _InputDataState();
}
class _InputDataState extends State<InputData> {
#override
Widget build(BuildContext context) {
return Material(
child:Scaffold(
body: Column(
children: <Widget>[
TitleBanner(),
Expanded(
child: Row(
children: [
Container(
height: 40,
width: 100,
child: Text('Sample Interval')),
Container(
height: 40,
width: 100,
child: TextField())
],
),
),
SizedBox(height: 100,),
Expanded(
child: Padding(
padding: const EdgeInsets.fromLTRB(8.0,0,0,0),
child: Row(
children: <Widget>[
Column(
children: <Widget>[
Container(
decoration: BoxDecoration(border: Border.all() ),
height: 20,
width: 100,
child: Text('Samples')),
buildTextField(),
buildTextField(),
buildTextField(),
buildTextField(),
buildTextField(),
buildTextField(),
buildTextField(),
]
),
Column(
children: <Widget>[
Container(
decoration: BoxDecoration(border: Border.all() ),
height: 20,
width: 100,
child: Text('Amplitudes')),
buildTextField(),
buildTextField(),
buildTextField(),
buildTextField(),
buildTextField(),
buildTextField(),
buildTextField(),
]
)
],
),
),
)
],
),
)
);
}
Container buildTextField() {
return Container(
decoration: BoxDecoration(border: Border.all() ),
height: 20,
width: 100,
child: TextField(keyboardType: TextInputType.number,
decoration: kTextFieldDecoration
),);
}
}
This way the user can input the samples and amplitudes manually but they cannot copy them from another source. I tried using the excel package in flutter, but it seems to allow us to use a full excel spreadsheet and export it rather than integrating the cells inside the browser.
I tried also some other packages like sticky_headers but they seem to do good formatting only, without allowing a copy from external spreadsheet.
Is there a way to directly paste the values using this implementation?
If you don't need the pasted values to be editable the simplest solution would be to parse the input from a single TextField. Probably with a package like csv.
If you need a grid of input fields the solution gets more tricky. You need to listen to changes to each TextField, parse the data, and spread multicell input to the rest of the grid.
Each TextField will need a controller to set the text programmatically. They will also need an inputFormatter to make sure the receiving field only contains the content from one cell. You can use the formatter or a separate onChanged listener to parse the input and update the text for other cells in the grid.
I took the time to create a PoC of the grid solution here.
You need to use the Table widget, and inside its children use a TextField. I will give you the links I found and below I will paste the code from one of them:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root
// of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Table',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text("GeeksforGeeks"),
backgroundColor: Colors.green,
),
body: Column(
children:<Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("Table",textScaleFactor: 2,style: TextStyle(fontWeight:FontWeight.bold),),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Table(
// textDirection: TextDirection.rtl,
// defaultVerticalAlignment: TableCellVerticalAlignment.bottom,
// border:TableBorder.all(width: 2.0,color: Colors.red),
children: [
TableRow(
children: [
Text("Education",textScaleFactor: 1.5,),
Text("Institution name",textScaleFactor: 1.5),
Text("University",textScaleFactor: 1.5),
]
),
TableRow(
children: [
Text("B.Tech",textScaleFactor: 1.5),
Text("ABESEC",textScaleFactor: 1.5),
Text("AKTU",textScaleFactor: 1.5),
]
),
TableRow(
children: [
Text("12th",textScaleFactor: 1.5),
Text("Delhi Public School",textScaleFactor: 1.5),
Text("CBSE",textScaleFactor: 1.5),
]
),
TableRow(
children: [
Text("High School",textScaleFactor: 1.5),
Text("SFS",textScaleFactor: 1.5),
Text("ICSE",textScaleFactor: 1.5),
]
),
],
),
),
]
),
);
}
}
Table Widget in Flutter
How to manage textField controller for multiple dataRow in dataTable Flutter
It is also possible to use Excel:
Excel Pub Dev Package
Create an Excel Document in Flutter Youtube Guide
If I helped you, please don't forget to mark my answer as correct (tick).
If I understood you correctly, then you want to insert a default value into the TextField, you can do this with the following code:
Widget buildTextField(String initText) {
return Container(
decoration: BoxDecoration(border: Border.all()),
height: 20,
width: 100,
child: TextField(
controller: TextEditingController(text: initText),
keyboardType: TextInputType.number,
decoration: kTextFieldDecoration
),
);
}

flutter listview with radio not showing in alertDialog

This is the code.
code:
class ThemeChangerWidget extends StatelessWidget {
final List<String> string = ['Light', 'Dark', 'Amoled'];
#override
Widget build(BuildContext context) {
final stateData = Provider.of<ThemeNotifier>(context);
final ThemeData state = stateData.getTheme();
return Theme(
data: state.copyWith(unselectedWidgetColor: state.accentColor),
child: AlertDialog(
backgroundColor: state.primaryColor,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)),
title: Text('Select Theme', style: state.textTheme.body1),
content: ListView.builder(
shrinkWrap: true,
itemBuilder: (context, index) {
return RadioListTile(
value: index,
groupValue: themes.indexOf(state),
onChanged: (ind) {
onThemeChanged(ind, stateData);
},
title: Text(
string[index],
style: state.textTheme.body2,
),
);
},
itemCount: string.length,
)),
);
}
}'
errors-The following assertion was thrown during performLayout():
RenderShrinkWrappingViewport does not support returning intrinsic dimensions.
some times throw this error instead of above LayoutBuilder does not support returning intrinsic dimensions.
Calculating the intrinsic dimensions would require instantiating every child of the viewport, which defeats the point of viewports being lazy.
If you are merely trying to shrink-wrap the viewport in the main axis direction, you should be able to achieve that effect by just giving the viewport loose constraints, without needing to measure its intrinsic dimensions.
AlertDialog uses an IntrinsicWidth widget that doesn't allow ListView.builder. You have to give a specific width to your ListView Example:
return AlertDialog(
title: Text('Dialog'),
content: SizedBox(
width: double.maxFinite,
child: ListView(
children: <Widget>[
//Your content here
],
),
),
);

OnPressed Set activeColor to a button of a list, and set inactiveColor to others btns - Flutter

I have a list of Chips, and I want them to change color when user click on them.
For instance if I click on the first Chip its color become black, and every other chips are grey. Then if I click on the second Chip its color become black and first Chip color become grey and so on.
I can't find a beautiful/simple way to do this, have you any ideas ?
Thanks a lot
Here is how you can do it:
Widget _myChip(int number, String name) {
return new Padding(
padding: const EdgeInsets.all(8.0),
child: new InkWell(
child: new Chip(
label: new Text(name,
style: new TextStyle(
color: selectedChip == number ? Colors.white : Colors.black
),),
backgroundColor:
selectedChip == number ? Colors.black : Colors.grey),
onTap: () {
setState(() {
selectedChip = number;
});
},
),
);
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Stackoverflow'),
),
body: new Column(
children: <Widget>[
_myChip(1, 'Arnold'),
_myChip(2, 'Sylvester'),
_myChip(3, 'Priscilla'),
_myChip(4, 'Parge'),
_myChip(5, 'Something'),
],
),
);
}
You need to give chips a unique number to identify and use inline if to change the color of the chips.

Flutter landscape orientation layout

How to set AppBar height in landscape mode so that it is not taking up for screen padding?
Is there a usual Flutter landscape layout Widget? Aforementioned problematic layout is as follow:
new Padding(
padding: media.padding,
child: new Scaffold(
key: _scaffoldKey,
body: new Row(
children: <Widget>[
new LimitedBox(
maxWidth: 320.0,
child: new Column(children: [
_buildAppBar(),
new Expanded(
child: new ModuleDrawer(
widget.module.sugar,
topPadding: 0.0,
)),
]),
),
new Expanded(
child: new RecordList(
widget.model,
),
),
],
)));
You could probably use the primary property of a Scaffold, in combination of detecting the orientation through a simple MediaQuery. So on landscape mode, set the primary flag to false to tell the Scaffold to not take status bar height into account when determining the AppBar height.
See the documentation:
Whether this scaffold is being displayed at the top of the screen.
If true then the height of the appBar will be extended by the height of the screen's status bar, i.e. the top padding for MediaQuery.
The default value of this property, like the default value of AppBar.primary, is true.
So in your case, something like this should do:
#override
Widget build(BuildContext context) {
final Orientation orientation = MediaQuery.of(context).orientation;
final bool isLandscape = orientation == Orientation.landscape;
return new Scaffold(
primary: !isLandscape,
appBar: new AppBar(
title: new Text('AppBar title'),
),
body: new Center(
child: new Text('Look at the appbar on landscape!'),
),
);
}

Resources