i have a layout which i am working on but i am very new to flutter what i would like to do is
have a layout as such:
but what i have managed to do so far is this
the code for what i have managed to do is here:
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.all(20.0),
color: Colors.cyan,
child: Row(
children: <Widget>[
Checkbox(
value: false,
onChanged: (bool? value) {
setState(() {
print(value!);
});
}, //onChanged
),
Text("Enable 1"),
new Flexible(
child: new TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'username',
),
), //textfield
), //flexible
new Flexible(
child: new TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'password',
),
), //textfield
), //flexible
],
),
) //Container
), //Expanded
Expanded(
flex: 1,
child: Container(
child: ElevatedButton.icon(
icon: Icon(
Icons.save_alt,
color: Colors.green,
size: 24.0,
),
label: Text('Save'),
onPressed: () {
print('pressed');
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.greenAccent),
),
),
),
)
]
)//end of body,
i have no idea on how to achieve my desired output any help would be appreciated
You should use a Column instead of the Row.
Then a Row for the text and the checkbox to be aligned.
Here's an example:
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.all(20.0),
color: Colors.cyan,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Checkbox(
value: false,
onChanged: (bool value) {
setState(() {
print(value);
});
}, //onChanged
),
Text("Enable 1"),
],
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: new TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'username',
),
),
), //flexible
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: new TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'password',
),
),
), //flexible
],
),
) //Container
), //Expanded
Expanded(
flex: 1,
child: Container(
child: ElevatedButton.icon(
icon: Icon(
Icons.save_alt,
color: Colors.green,
size: 24.0,
),
label: Text('Save'),
onPressed: () {
print('pressed');
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.greenAccent),
),
),
),
)
])
Related
I'm trying to make it so that the body fades behind the TextField, so I decided to add a gradient on top of it, but there seems to be an empty space between the gradient and the card, and it's ruining the effect. I can't figure out where it's coming from or how to handle it.
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 15,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: <Color>[
Colors.white.withOpacity(0),
Theme.of(context).scaffoldBackgroundColor,
]),
),
),
Row(
children: [
Flexible(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
child: Row(
children: [
Flexible(
child: TextField(
onTap: () => myFocusNode.requestFocus(),
decoration: InputDecoration(
hintText: 'Enter task',
contentPadding: EdgeInsets.all(15),
border: InputBorder.none,
floatingLabelBehavior: FloatingLabelBehavior.never,
),
controller: textController,
focusNode: myFocusNode,
onSubmitted: (_) {
submit();
myFocusNode.requestFocus();
textController.clear();
},
),
),
if (!isEpoch(_selectedDate.toString()))
SizedBox(
height: 20,
child: FittedBox(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text(
DateFormat('dd/MM').format(_selectedDate),
),
Text(timeOfDayAsHhMm(_selectedTime)),
],
),
),
),
SizedBox(
width: 10,
),
IconButton(
splashRadius: 20,
padding: EdgeInsets.zero,
constraints: BoxConstraints(),
onPressed: _dateAndTimePicker,
icon: const Icon(Icons.calendar_month),
),
SizedBox(
width: 10,
),
IconButton(
splashRadius: 20,
padding: EdgeInsets.zero,
constraints: BoxConstraints(),
onPressed: () => null,
icon: const Icon(Icons.image),
),
SizedBox(
width: 10,
)
],
),
),
),
CircleAvatar(
child: TextButton(
onPressed: () {
submit();
myFocusNode.requestFocus();
textController.clear();
setState(() {
_selectedDate = null;
_selectedTime = null;
});
},
child: const FittedBox(
child: Text(
'Add',
style: TextStyle(
color: Colors.white,
),
),
),
),
),
SizedBox(
width: 5,
)
],
),
],
);
Is there some automatic padding coming from somewhere? I did use a lot of Flexibles and Boxes.
To the card add margin with zero insets
Card(
margin: EdgeInsets.zero,
)
I am trying to make a login page which looks like this
But whenever I try to add row which has another column iside it for textinput field and text It shows assertion error. I also tried removing sized box before text row still it is showing the same error
My flutter code:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: color,
body: SafeArea(
child: Center(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 20.0,
),
Text('Life Drop',
style: TextStyle(fontSize: 40, color: Colors.white)),
SizedBox(
height: 10.0,
),
Text("your blood can save lives",
style: TextStyle(fontSize: 12, color: Colors.white)),
SizedBox(
height:20.0
),
Row(
children: [
Column(children: [
Text('Login'),
TextFormField(
controller: emailController,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Email',
),
),
SizedBox(width: 20),
],)
],
)
],
),
),
)
)
);
}
edit your row like below code:
Expanded(
child: Row(
children: [
Expanded(
child: Column(
children: [
const Text('Login'),
TextFormField(
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Email',
),
),
const SizedBox(width: 20),
],
)),
],
))
I have a dropDownButton populated with a query from my database here I have the users, when I select an item I want to show all the user info in the TextFields below.
Since some of the information should not be edited by hand, I wanted to make some of the fields as containers, so I can't write in them, but some fields should be a TextField so I can edit them. I'm new to flutter so I still don't understand this at full, and I can't find a way to load the info in the TextFields when I change the dropDownButton.
here is the code below:
class UtilizadorPage extends StatefulWidget {
#override
_UtilizadorPageState createState() => new _UtilizadorPageState();
}
class _UtilizadorPageState extends State<UtilizadorPage> {
Utilizador user = Utilizador();
BDLocal db = BDLocal.instance;
Utilizador? _currentUser;
var txt = TextEditingController();
#override
void initState(){
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Utilizadores",
),
),
body: Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FutureBuilder<List<Utilizador>>(
future: user.getUtilizadores(),
builder: (BuildContext context,
AsyncSnapshot<List<Utilizador>> snapshot){
if (!snapshot.hasData) return CircularProgressIndicator();
return DropdownButton<Utilizador>(
alignment: Alignment.center,
items: snapshot.data?.map((util) => DropdownMenuItem<Utilizador>(
child: Text(util.nome),
value: util,
)).toList(),
onChanged:(Utilizador? value) {
setState(() {
_currentUser = value;
});
},
isExpanded: false,
//value: _currentUser,
hint: Text('Select User'),
);
}),
]
),
SizedBox(height: 20.0),
Row(
children: [
_currentUser != null
? Expanded(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
flex: 1,
child: Container(
child: InputDecorator(
decoration: InputDecoration(
labelText: "Nome",
floatingLabelBehavior:FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
child: Text(_currentUser!.nome)
),
),
),
SizedBox(width: 10.0),
Expanded(
flex: 1,
child: Container(
child: InputDecorator(
decoration: InputDecoration(
labelText: "ID na BDNuvem",
floatingLabelBehavior:FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
child: Text(_currentUser!.id_BDNuvem.toString())
),
),
),
]),
SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
flex: 1,
child: TextFormField(
initialValue: _currentUser!.perfil.toString(),
decoration: InputDecoration(
label: Text("Perfil"),
floatingLabelBehavior:FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
),
),
SizedBox(width: 10.0),
Expanded(
flex: 1,
child: Container(
child: InputDecorator(
decoration: InputDecoration(
labelText: "Numero de Colaborador",
floatingLabelBehavior:FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
child: Text(_currentUser!.numero.toString())
),
),
),
],
),
SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
flex: 1,
child: Container(
child: InputDecorator(
decoration: InputDecoration(
labelText: "Nome do Funcionario",
floatingLabelBehavior:FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
child: Text(_currentUser!.nomeFuncionario)
),
),
),
],
),
SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
flex: 1,
child: Container(
child: InputDecorator(
decoration: InputDecoration(
labelText: "Email",
floatingLabelBehavior:FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
child: Text(_currentUser!.email)
),
),
),
],
),
SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
flex: 1,
child: Container(
child: InputDecorator(
decoration: InputDecoration(
labelText: "Senha",
floatingLabelBehavior:FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
child: Text(_currentUser!.senha)
),
),
),
],
),
SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
flex: 1,
child: Container(
child: InputDecorator(
decoration: InputDecoration(
labelText: "Ultima Atualizacao",
floatingLabelBehavior:FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
child: Text(_currentUser!.ultimaAtualizacao)
),
),
),
SizedBox(width: 10.0),
Expanded(
flex: 1,
child: Container(
child: InputDecorator(
decoration: InputDecoration(
labelText: "Atualizado Por",
floatingLabelBehavior:FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
child: Text(_currentUser!.atualizadoPor)
),
),
),
],
),
],
),
)
: Text("No User selected"),
],
)
],
),
);
}
}
I only have one textField now because I'm still experimenting things.
what I wanted to do is update the value of the text with the info I retrieved from the query for example: _currentUser!.perfil.toString(), which holds a string that represent the type of user
If you've assigned a TextEditingController to Textfield you can simply manipulate value in by
myTextEditingController.text = "new value";
Error showing is
Invalid Matrix
I want my submit button and Cancel button as this is Form submission in flutter
My ScrollWidget is scrolling down but my SUBMIT and CANCEL Raised Buttons are getting invisible or not shown.
the code is as follows:
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 20.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.person_pin),
labelText: 'CompanyName',
hintText: 'Type the company Name',
),
),
),
),
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.phone_android),
labelText: 'ModelName',
hintText: 'Type the Model Name',
),
),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
keyboardType: TextInputType.number,
inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly
],
decoration: InputDecoration(
labelText: 'SerielNumber',
hintText: 'Type the Seriel Number',
prefixIcon: Icon(Icons.view_headline),
),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
color: Color(0xff476cfb),
onPressed: () {
Navigator.of(context).pop();
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: TextField(
maxLines: null,
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.place),
labelText: 'Cancel',
),
),
),
RaisedButton(
color: Color(0xff476cfb),
onPressed: () {
uploadPic(context);
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: TextField(
maxLines: null,
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.place),
labelText: 'Submit',
),
),
),
],
)
],
),
),
);
}
}
The problem is with your RaisedButton's Child. TextField must have bounded width.
you can use Text widget or Container widget as child of RaisedButton and use Row as child of Container to set title and icon like code below:
RaisedButton(
color: Color(0xff476cfb),
onPressed: () {
Navigator.of(context).pop();
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: Container(
child: Row(
children: [
Text('submit'),
Icon(Icons.place),
],
)),
),
I am new in Flutter so maybe my question will be basic for someone.
The problem is that in my app I want to hide TextField when CheckBox is unchecked. Next to a TextField is a button which should be static. My issue is that when I want to hide this TextField, button is shifting to the left side. I think that the best option is to show it to you
Before:
After:
This is the code I am using:
bool secondLayer = false;
void _secondLayer(bool value) => setState(() => secondLayer = value);
#override
Widget build(BuildContext context) {
return new Scaffold(
/*appBar: new AppBar(
title: new Text('Name Here'),
),*/
resizeToAvoidBottomInset: false,
body: SingleChildScrollView(
child: new Container(
padding: new EdgeInsets.all(32.0),
child: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
child: new Row(
children: <Widget>[
Expanded(
child: Column(
children: <Widget>[
new TextField(
decoration: InputDecoration(
labelText: 'First Hidden Layer',
hintText: '25',
),
autocorrect: true,
autofocus: true,
keyboardType: TextInputType.number,
),
],
),
),
Expanded(
child: Column(
children: <Widget>[
new SizedBox(
width: 100,
child: new RaisedButton(onPressed: null, child: new Text('ADD DATA'),)
)
],
),
)
],
),
),
Container(
child: new CheckboxListTile(
value: secondLayer,
onChanged: _secondLayer,
title: new Text(
'Use Second Hidden Layer',
style: new TextStyle(
fontSize: 12
),
),
controlAffinity: ListTileControlAffinity.leading,
),
),
Container(
child: new Row(
children: <Widget>[
Visibility(
visible: secondLayer,
child: new Expanded(
child: Column(
children: <Widget>[
new TextField(
decoration: InputDecoration(
labelText: 'Second Hidden Layer',
hintText: '25',
),
autocorrect: true,
autofocus: true,
keyboardType: TextInputType.number,
),
],
),
),
),
Expanded(
child: Column(
children: <Widget>[
new SizedBox(
width: 100,
child: new RaisedButton(onPressed: null, child: new Text('OPTIONS'),)
)
],
),
)
],
),
),
Container(
child: new TextField(
decoration: InputDecoration(
labelText: 'Epochs',
hintText: '5000',
),
autocorrect: true,
autofocus: true,
keyboardType: TextInputType.number,
),
),
Container(
child: new TextField(
decoration: InputDecoration(
labelText: 'Learning Rate',
hintText: '0.00333',
),
autocorrect: true,
autofocus: true,
keyboardType: TextInputType.number,
),
),
Container(
padding: EdgeInsets.only(top: 20),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width/3,
height: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
border: Border.all(color: Color.fromRGBO(0, 0, 0, 100)),
),
child: new Text('data')
),
Container(
width: MediaQuery.of(context).size.width/3,
height: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
border: Border.all(color: Color.fromRGBO(0, 0, 0, 100)),
),
child: new Text('data')
)
],
)
)
],
),
)
)
)
);
It is because you didn't add any replacement property to your Visibility Widget.
Visibility(
visible: secondLayer,
child: Expanded(
child: Column(
children: <Widget>[
// your text field here
]
),
),
replacement: Expanded(child: Container()), // the widget which will fill the space when hiddden
)
Something like this should work as you replace your TextField with an empty Container inside an Expanded to fill your empty space.
You need to set the maintainSize property of the Visibility widget to true, it is used to decide "whether to maintain space for where the widget would have been." Source
You also need to set maintainAnimation and maintainState to true for maintainSize to work. Source
So your code will go from:
Visibility(
visible: secondLayer,
child: yourChildWidget
),
to:
Visibility(
visible: secondLayer,
maintainSize: true,
maintainAnimation: true,
maintainState: true,
child: yourChildWidget
),