Remove Flutter HTML widget's default padding - flutter

There is a default padding in flutter_html already when trying to parse text.
Below is the difference between using HTML(data: ...) and Text(...) widgets.
Top: HTML Widget, Bottom: Text Widget
How can I remove the horizontal padding?

I think HTML or body is having a default padding and/or margin.
Try adding "body": Style(margin: EdgeInsets.zero, padding: EdgeInsets.zero, in the style parameter.

Html has updated, so I update accepted answer:
"body": Style(
padding: EdgeInsets.zero,
margin: Margins(
bottom: Margin.zero(),
left: Margin.zero(),
top: Margin.zero(),
right: Margin.zero(),
),
),

Add 'body': Style(margin: Margins.zero) inside the style parameter curly brackets.

Related

Container in Bottom Navigation Bar taking whole screen

I want to put a container in the bottom navigation bar for my app but it's taking up the whole screen
bottomNavigationBar: Container(
padding: EdgeInsets.only(
left: Dimensions.sizeWidthPercent(16),
right: Dimensions.sizeWidthPercent(16),
bottom: Dimensions.sizeHeightPercent(30)),
child: Column(
children: const [
TextContainer(text: 'Proceed to request dispatcher')
],
),
)
This is what happens the whole scaffold body goes missing
The issue was here.Column will expand vertically when no size was not declared to his parent vertically. So you need to declare main axis size for the column. Add following mainAxis Size to the column.
mainAxisSize:MainAxisSize.min,
So the full code for above is,
bottomNavigationBar: Container(
padding: EdgeInsets.only(
left: Dimensions.sizeWidthPercent(16),
right: Dimensions.sizeWidthPercent(16),
bottom: Dimensions.sizeHeightPercent(30)),
child: Column(
mainAxisSize:MainAxisSize.min,
children: const [
TextContainer(text: 'Proceed to request dispatcher')
],
),
)
Just add height to the container, it will resolve the issue

textFormField borders with shadows

I'm new to flutter. I just saw this beautiful design and therefore I've been trying to implement something like attached below -
Unfortunately, I've been unable to do so after a lot of tries with InputBorders, Material widget etc.
Can someone please let me know how we can implement such textFormFields with the exact same design ?
You can use neumorphic package, You can achieve a similar effect using this package.
Neumorphic(
margin: EdgeInsets.only(left: 8, right: 8, top: 2, bottom: 4),
style: NeumorphicStyle(
depth: NeumorphicTheme.embossDepth(context),
boxShape: NeumorphicBoxShape.stadium(),
),
padding: EdgeInsets.symmetric(vertical: 14, horizontal: 18),
child: TextField(
onChanged: this.widget.onChanged,
controller: _controller,
decoration: InputDecoration.collapsed(hintText: this.widget.hint),
),
)
If you look at TextField docs on Flutter API, there's the decoration property.
Blurred effect in borders
This property is the class InputDecoration (read its API).
To add a simple border, in the border property (classes Border and for each side of the border the class BorderSide) see the code below.
Border(
top: BorderSide(width: 16.0, color: Colors.lightBlue.shade50),
bottom: BorderSide(width: 16.0, color: Colors.lightBlue.shade900),
),
Border class has a color property, you can use it with Colors.blue[50] to do a blurred effect or a higher number. Read Colors class.
Shadow style
To do these shadows, you can use the properties contentPadding, enabledBorder and border. Look at this answer https://stackoverflow.com/a/54195097/9192954. It uses InputDecoration (the code is from 2019, then borderSide currently doesn't exist, instead use border).
Hope this helps you.

why sizer dealing with padding value as a hight?

I am using sizer library, but i am wondering why padding value is assigned as a height top:5.h instead of weight top:5.w?.
The code in the Sizer documentation is:
Padding(
padding: EdgeInsets.symmetric(vertical: 5.h, horizontal: 3.h),
child: Container(),
);
This looks more logical
Padding(
padding: EdgeInsets.symmetric(vertical: 5.h, horizontal: 3.w),
child: Container(),
);
It creates :
A vertical padding of 5% of the screen height
An horizontal padding of 3% of the screen width
It might be a mistake in the doc, you could suggest an edit on the repo

What's the most efficient way to add spacing with a specific width to Widgets in a Row()?

I use Expanded() to tell the Widgets in my Row to use the available space. I want to have 4px space between elements in my Row. I don't want to have the same space towards elements outside of the Row. Given that Flutter doesn't support negative Padding I therefore can't add Padding in a way that gives the first item no left Padding and the last item no right Padding.
Is there pattern to easily define spacing with a specific width? I want a solution that works with an arbitrary number of Widgets. For 4 Widgets I want something that produces the equivalent of:
Row(children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 8, 0),
child: Expanded(Text("Alice"))),
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: Expanded(Text("Alice"))),
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: Expanded(Text("Alice"))),
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 0, 0),
child: Expanded(Text("Alice")))
]);
cant understand what you want to achieve exactly but try
Use SizedBox widget very easy and simple check it here
https://api.flutter.dev/flutter/widgets/SizedBox-class.html
example
SizedBox(
width: 200.0,
height: 300.0,
)
I usually use this:
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Widget(),
Spacer(),
Widget(),
Widget(),
],
),
Spacer, a widget that takes up space proportional to it's flex
value. Flutter Row Class
mainAxisAlignment property : How the children should be placed along
the main axis. Flutter MainAxisAlignment enum
If you want to determine the size, you can use SizedBox(width: double) instead of Spacer ().
SizedBox: A box with a specified size. Flutter SizedBox class

How do I add margins to my widget ? Understanding the effect of EdgeInsets

I am trying to wrap my head around ui placement in Flutter. So I currently have something that looks like this
I would like to add a little space b/w search Textfield and the button.
This is what the controlling part of my code looks like. I am trying to style my textFieldSearchBox so it has a little margin on the right, I tried trying to increase the Edge insets but it seems to increase the size of the TextField I don't know why? I know I could adding a padding element after TextField but I wanted to know what my other options are.Why does increasing the EdgeInsets in the decoration of textFieldSearchBox increase the size of the textbox? My ideal situation would be to add margin around all the borders of this textbox (LTRB).
Any suggestions?
TextField textFieldSearchBox = new TextField(
keyboardType: TextInputType.text,
controller: filterController,
autofocus: false,
decoration: new InputDecoration(
contentPadding: new EdgeInsets.fromLTRB(20.0, 10.0, 100.0, 10.0),
border:
new OutlineInputBorder(borderRadius: BorderRadius.only()),
),
);
var optionRow = new Row(
children: <Widget>[
new Expanded(child:textFieldSearchBox),
searchButton,
new IconButton(
icon: new Icon(Icons.filter),
onPressed: (){print("Called....");},
),
],
);
return new Scaffold(
appBar: new AppBar(
title: new Text("Title goes here.."),
backgroundColor: Colors.lightBlueAccent,
),
body: new Container(
child:new Column(
children: <Widget>[
optionRow,
],
),
),
);
How to add margin to a widget
In Flutter we generally talk about adding Padding around a widget rather than margin. The Container widget does have a margin parameter, but even this just wraps it child (and any decoration that the child has) with a Padding widget internally.
So if you have something like this
and you want to add some space around the widget like this
then you just wrap the widget with Padding. This is easy to do if you put your cursor on the widget name and press Alt+Enter (or Option+Return on a Mac) in Android Studio. Then choose Add padding from the menu.
which gives you something like this
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"text",
style: TextStyle(fontSize: 20.0),
),
);
Meaning of EdgeInsets
When you are setting padding you can't directly use an integer or double. You have to specify the space (number of logical pixels) using the EdgeInsets. You can set all of the sides at once (as we saw in the example above), or you can set them individually like this:
Widget myWidget() {
return Padding(
padding: const EdgeInsets.only(
left: 40,
top: 20,
right: 40,
bottom: 20,
),
child: Text("text"),
);
}
Since in this example left and right are the same and top and bottom are the same, we can simplify EdgeInsets to
padding: const EdgeInsets.symmetric(
horizontal: 40,
vertical: 20,
),
Using a Container to set padding and margin
An alternate method is to wrap your widget in a Container. The Container widget has both a padding and a margin property. (This would only be necessary, though, if you were also adding a decoration like background color or a border.)
Widget myWidget() {
return Container(
margin: EdgeInsets.all(30),
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.yellow,
border: Border.all(color: Colors.black),
),
child: Text(
"Flutter",
style: TextStyle(
fontSize: 50.0
),
),
);
}
Why does increasing the EdgeInsets in the decoration of textFieldSearchBox increase the size of the textbox ?
Because that padding is used for the internal padding. The one you see between the borders and the actual text.
My ideal situation would be to add margin around all the borders of this textbox (LTRB). Any suggestions ?
Wrap your TextField into a Padding. That is the ideal way to achieve the desired layout in flutter.
final textFieldSearchBox = new Padding(
padding: const EdgeInsets.only(right: 8.0),
child: new TextField(
keyboardType: TextInputType.text,
controller: filterController,
autofocus: false,
decoration: new InputDecoration(
contentPadding: new EdgeInsets.fromLTRB(20.0, 10.0, 100.0, 10.0),
border: new OutlineInputBorder(borderRadius: BorderRadius.only()),
),
),
);
You can put the component inside a padding, like this
var optionRow = new Row(
children: <Widget>[
new Expanded(child:textFieldSearchBox),
new Padding(padding: new EdgeInsets.all(20.0),child:button,),
new IconButton(
icon: new Icon(Icons.filter),
onPressed: (){print("Called....");},
),
],
);
Since the layout of your widgets is Row, why do not you add the mainAxisAlignment property to it like this:
mainAxisAlignment : MainAxisAlignment.spaceAround
Or
mainAxisAlignment : MainAxisAlignment.spaceBetween
Simply use
EdgeInsets.fromLTRB
For example:
padding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
You can also use SizedBox if you want to avoid using Container
There are some widgets which allow that using a parameter, but in most cases You can wrap the Widget in a Padding widget like so.
From this
YourWidget(
....
)
To This
Padding(
padding: EdgeInsets.all(value),
YourWidget(
....
)
)
Where "all" can be replaced with the variant of your choice
In Flutter we generally talk about adding Padding around a widget rather than margin. The Container widget does have a margin parameter, but even this just wraps it child (and any decoration that the child has) with a Padding widget internally.strong text