Code formate break code in unnecessary code line - flutter

I am using android studio and always use reformate code with 'dartfarmate' option. but it alwasy breaks the code in very unnecessary code of line which sometime disturb me while coding. what is the better solution
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey),
color: const Color
.fromARGB(
100, 225, 225, 225),
borderRadius:
BorderRadius
.circular(5),
),
margin:
const EdgeInsets.only(
bottom: 5),
padding:
const EdgeInsets.only(
bottom: 8,
top: 8,
left: 8,
right: 8),
child: InputField(
text:
"detail here...",
controller:
controler,
isEditable:
true,
),
),
But I expect the code should look like that
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
color: const Color.fromARGB(100, 225, 225, 225),
borderRadius: BorderRadius.circular(5),
),
margin: const EdgeInsets.only(bottom: 5),
padding:
const EdgeInsets.only(bottom: 8, top: 8, left: 8, right: 8),
child: InputField(
text: "detail here...",
controller: controler,
isEditable: true,
),
),

You can try this :
Go to Preferences -> Editor -> Code Style -> Dart -> Line Length and put and appropriate value.

Related

Negative Content Padding in Container

I have a list with an Container. With a Content Padding of 0 it´s look like this:
but I want the List Tile (the white writing) in the middle of the box.
Here is the part of my Code where the Magic is done:
return Card(
color: Color.fromRGBO(25, 29, 30, 100),
margin: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
child: Container(
height: 70,
padding: EdgeInsets.fromLTRB(15, 20, 20, 15),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.red,
),
child: ListTile(
contentPadding: EdgeInsets.symmetric(vertical: 0),
leading: Checkbox(
side: const BorderSide(
color: Colors.white
),
Thanks for help in advance
use title in your ListTile like this
ListTile(
contentPadding: EdgeInsets.symmetric(vertical: 0),
leading: Checkbox(
side: const BorderSide(
color: Colors.white
),
),
title: Text('Your text'),
)
Nevermind.
I removed the container and changed the code to this:
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
color: Colors.red,
margin: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
child: ListTile(...
It solved the problem with the container and positioned the ListTile in the middle

How to shorten the length of a border?

I need a container that has these borders (those in the corners).
This is my code:
Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(color: Colors.black, width: 8),
left: BorderSide(color: Colors.black, width: 8),
),
),
padding: EdgeInsets.all(15),
height: 265,
width: 265,
margin: EdgeInsets.symmetric(
vertical: size.height * 0.1,
horizontal: size.width * 0.1,
),
child: Placeholder(),
),
But it looks like this:
How can I make the border shorter? Thanks
You can use Stack.
Stack(
children: [
Container(
width: 48,
height: 48,
decoration: BoxDecoration(
border: Border(
top: BorderSide(color: Colors.black, width: 8),
left: BorderSide(color: Colors.black, width: 8),
),
),
),
Positioned(
bottom: 0,
right: 0,
child: Container(
width: 48,
height: 48,
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.black, width: 8),
right: BorderSide(color: Colors.black, width: 8),
),
),
),
),
Container(
padding: EdgeInsets.all(15),
height: 265,
width: 265,
margin: EdgeInsets.symmetric(
vertical: 16,
horizontal: 16,
),
decoration: BoxDecoration(
color: Colors.red,
boxShadow: [
BoxShadow(
color: Colors.black,
offset: Offset(0, 1),
blurRadius: 4,
spreadRadius: 0,
)
],
),
child: Container(),
),
],
)

How to make custom border design in flutter?

I am trying to make layout similar to this image.
Here is my code:
GestureDetector(
child: Container(
width: double.infinity,
margin: EdgeInsets.only(left: 15, right: 15),
height: 100,
decoration: BoxDecoration(
border: Border(
left: BorderSide(color: Colors.blue, width: 10.0),
top: BorderSide(color: Colors.grey[300], width: 2),
right: BorderSide(color: Colors.grey[300], width: 2),
bottom: BorderSide(color: Colors.grey[300], width: 2),
),
borderRadius:
BorderRadius.only(
topRight: Radius.circular(15)
)),
),
onTap: () => {},
)
Not really sure why but borderRadius only works when specifying border with Border.all else it crashes..
Maybe this workaround can get the job done for you..
GestureDetector(
child: Stack(children: <Widget>[
new Container(
margin: EdgeInsets.only(left: 15, right: 15),
width: double.infinity,
height: 100.0,
decoration: new BoxDecoration(
border: new Border.all(
color: Colors.grey[300],
width: 2.0,
style: BorderStyle.solid),
borderRadius: new BorderRadius.horizontal(
right: new Radius.circular(20.0),
),
),
),
Container(
width: 10,
height: 100,
margin: EdgeInsets.only(left: 15),
color: Colors.blue),
]),
)

How do i set the position of picture/button/text to be responsive for other device?

I try to set the position of picture/button/text by define the number but when my code run to others device my UI is not the same as runing on my device. This is the picture that show the result on my device and othe devide.
my device
others device
So, I try to use this code to make my code to be responsive.
Container(
child: Positioned(
left: MediaQuery.of(context).size.width*0.4,
top: MediaQuery.of(context).size.height*0.28,
width: 100,
height: 100,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/LG5.png"))),
))),
And for setting the position of button and login TextFiled I use this code
Container(
margin: const EdgeInsets.only(left: 80.0, right: 80.0, top: 300),
child: Column(
children: <Widget>[
TextField(
obscureText: true,
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide:
BorderSide(width: 3, color: Colors.indigo[700]),
),
labelText: 'Password',
),
),
TextField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide:
BorderSide(width: 3, color: Colors.indigo[700]),
),
labelText: 'Username',
),
),
Container(
width: 300,
margin: const EdgeInsets.only(top: 40),
child: RaisedButton(
child: Text('Login',
style: TextStyle(color: Colors.white, fontSize: 20)),
color: Colors.indigo[700],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeScreen()));
}),
)
for the above code I use this code to set position of TextFiled
margin: const EdgeInsets.only(left: 80.0, right: 80.0, top: 300)
for button for login is
margin: const EdgeInsets.only(top: 40)
As you see, i define the number for the position and i try to use this code and assign to the left,right,top of "EdgeInsets.only" but it doesn't work.
MediaQuery.of(context).size.width
MediaQuery.of(context).size.height
So, how can i define the value of left,right,top with "EdgeInsets.only" and no need to fix the number and it will reponsive to other device?
Or, you can suggest me to do another way that more proper than this way because i'm not sure that i do the right way. I just learn Flutter.
Thank you.
Instead of using absolute values like 20, 30 or whatsoever, try to use %. This way your UI will be responsive

How to add border on a container inside a row widget in Flutter?

Container(
// decoration: BoxDecoration(
// border: Border.all(color: Colors.black45),
// borderRadius: BorderRadius.circular(8.0),
// ),
child: Row(
children: <Widget>[
Container(
child: Text("hi"),
margin : EdgeInsets.fromLTRB(20, 8, 8, 16),
width: MediaQuery.of(context).size.width *0.42,
height: 90,
color: Colors.black12,
),
Container(
child: Text("Hi"),
margin: EdgeInsets.fromLTRB(16, 8, 8, 16),
width: MediaQuery.of(context).size.width * 0.42 ,
height: 90,
color: Colors.black12,
)
],
),
),
I can add border using Box decoration on the outer container, but it's throwing me an error when I am trying to do the same on the inner containers. What is the problem and how to solve it?
In order to add border on container inside row widget , we have to use decoration for the inner containers.
Once you will post the error, we can answer you better but i think the below code will be helpful for you.
If you are using decoration then you must not add colour attribute in container directly, it should be in decoration only.
Container(
child: Row(
children: <Widget>[
Container(
child: Text("hi"),
margin: EdgeInsets.fromLTRB(20, 8, 8, 16),
width: MediaQuery.of(context).size.width * 0.42,
height: 90,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
shape: BoxShape.rectangle,
border: Border.all(
color: Colors.blue,
width: 4,
)),
),
Container(
child: Text("Hi"),
margin: EdgeInsets.fromLTRB(16, 8, 8, 16),
width: MediaQuery.of(context).size.width * 0.42,
height: 90,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
shape: BoxShape.rectangle,
border: Border.all(
color: Colors.blue,
width: 4,
)),
)
],
),
),
In container widgets you cannot use the color and decoration at the same time. Remove the color property from the Container and move it into the BoxDecoration widget
This should work:
Container(
child: Row(
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black45),
borderRadius: BorderRadius.circular(8.0),
color: Colors.black12, //add it here
),
child: Text("hi"),
margin : EdgeInsets.fromLTRB(20, 8, 8, 16),
width: MediaQuery.of(context).size.width *0.42,
height: 90,
//color: Colors.black12, //must be removed
),
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black45),
borderRadius: BorderRadius.circular(8.0),
color: Colors.black12, //add it here
),
child: Text("Hi"),
margin: EdgeInsets.fromLTRB(16, 8, 8, 16),
width: MediaQuery.of(context).size.width * 0.42 ,
height: 90,
//color: Colors.black12, // must be removed
)
],
),
),
Consider the humble Divider widget to keep things simple. If you add it to the bottom of a Column in your row it will add a line that will act as a border.
const Divider(height: 1.0,),