layout | title | permalink |
---|---|---|
page |
Flutter for Web (HTML/CSS) Developers |
/web-analogs/ |
Contents:
- TOC Placeholder {:toc}
The examples assume:
-
The HTML document starts with a modern HTML DOCTYPE, and the CSS box model for all HTML elements is set to
border-box
, for consistency with the Flutter model. {% prettify css %}{ box-sizing: border-box; } {% endprettify %}
-
In Flutter, the default styling of the "Lorem ipsum" text is defined by the
bold24Roboto
variable as follows, to keep the syntax simple: {% prettify dart %} TextStyle bold24Roboto = new TextStyle( color: Colors.white, fontSize: 24.0, fontWeight: FontWeight.w900, ); {% endprettify %}
The following examples show how to perform the most common UI layout tasks.
Font style, size, and other text attributes that CSS handles with the font and color properties are individual properties of a TextStyle child of a Text widget.
In both HTML and Flutter, child elements or widgets are anchored at the top left, by default.
.greybox { background-color: #e0e0e0; /* grey 300 */ width: 320px; height: 240px; [[highlight]] font: 900 24px Georgia;[[/highlight]] } {% endprettify %}
In Flutter, you set the background color using a
Container’s
decoration
property.
The CSS examples use the hex color equivalents to the Material color palette.
.greybox { [[highlight]] background-color: #e0e0e0; [[/highlight]] /* grey 300 */ width: 320px; height: 240px; font: 900 24px Roboto; } {% endprettify %}
A Center widget centers its child both horizontally and vertically.
To accomplish a similar effect in CSS, the parent element uses either a flex or table-cell display behavior. The examples on this page show the flex behavior.
.greybox { background-color: #e0e0e0; /* grey 300 */ width: 320px; height: 240px; font: 900 24px Roboto; [[highlight]] display: flex; align-items: center; justify-content: center; [[/highlight]] } {% endprettify %}
To specify the width of a
Container
widget, its width
property. This is a fixed width, unlike the
CSS max-width property that adjusts the container width up to a maximum value.
To mimic that effect in Flutter, use the constraints
property of the
Container. Create a new
BoxConstraints
widget with a minWidth
or maxWidth
.
For nested Containers, if the parent’s width is less than the child’s width, the child Container sizes itself to match the parent.
.greybox { background-color: #e0e0e0; /* grey 300 / [[highlight]] width: 320px; [[/highlight]] height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redbox { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; [[highlight]] width: 100%; max-width: 240px; [[/highlight]] } {% endprettify %}
The following examples show how to perform more complex operations on widget position, size, and background.
By default, widgets are positioned relative to their parent.
To specify an absolute position for a widget as x-y coordinates, nest it in a Positioned widget that is, in turn, nested in a Stack widget.
.greybox { background-color: #e0e0e0; /* grey 300 / width: 320px; height: 240px; font: 900 24px Roboto; [[highlight]] position: relative; [[/highlight]] } .redbox { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; [[highlight]] position: absolute; top: 24px; left: 24px; [[/highlight]] } {% endprettify %}
To rotate a widget, nest it in a
Transform
widget. Use the Transform widget’s alignment
and origin
properties to
specify the transform origin (fulcrum) in relative and absolute terms,
respectively.
For a simple 2D rotation, the widget is rotated on the Z axis using radians. (degrees × π / 180)
.greybox { background-color: #e0e0e0; /* grey 300 / width: 320px; height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redbox { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; [[highlight]] transform: rotate(15deg); [[/highlight]] } {% endprettify %}
To scale a widget up or down, nest it in a
Transform
widget. Use the Transform widget’s alignment
and origin
properties to specify the transform origin (fulcrum) in relative or
absolute terms, respectively.
For a simple scaling operation along the x-axis, create a new Matrix4 identity object and use its scale() method to specify the scaling factor.
When you scale a parent widget, its child widgets are scaled accordingly.
.greybox { background-color: #e0e0e0; /* grey 300 / width: 320px; height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redbox { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; [[highlight]] transform: scale(1.5); [[/highlight]] } {% endprettify %}
To apply a linear gradient to a widget's background, nest it in a
Container
widget. Then use the Container widget’s decoration
property to create a
BoxDecoration
object, and use BoxDecoration's gradient
property to transform the
background
fill.
The gradient “angle” is based on the Alignment (x, y) values:
- If the beginning and ending x values are equal, the gradient is vertical (0° | 180°).
- If the beginning and ending y values are equal, the gradient is horizontal (90° | 270°).
.greybox { background-color: #e0e0e0; /* grey 300 */ width: 320px; height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redbox { padding: 16px; color: #ffffff; [[highlight]] background: linear-gradient(180deg, #ef5350, rgba(0, 0, 0, 0) 80%); [[/highlight]] } {% endprettify %}
.greybox { background-color: #e0e0e0; /* grey 300 */ width: 320px; height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redbox { padding: 16px; color: #ffffff; [[highlight]] background: linear-gradient(90deg, #ef5350, rgba(0, 0, 0, 0) 80%); [[/highlight]] } {% endprettify %}
The following examples show how to make and customize shapes.
To round the corners of a rectangular shape, use the borderRadius
property
of a
BoxDecoration
object. Create a new
BorderRadius
object that specifies the radii for rounding each corner.
.greybox { background-color: #e0e0e0; /* gray 300 / width: 320px; height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redbox { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; [[highlight]] border-radius: 8px; [[/highlight]] } {% endprettify %}
In CSS you can specify shadow offset and blur in shorthand, using the box-shadow property. This example shows two box shadows, with properties:
xOffset: 0px, yOffset: 2px, blur: 4px, color: black @80% alpha
xOffset: 0px, yOffset: 06x, blur: 20px, color: black @50% alpha
In Flutter, each property and value is specified separately.
Use the boxShadow
property of BoxDecoration to create a list of
BoxShadow
widgets. You can define one or multiple BoxShadow widgets, which can be stacked
to customize the shadow depth, color, etc.
.greybox { background-color: #e0e0e0; /* grey 300 / width: 320px; height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redbox { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; [[highlight]] box-shadow: 0 2px 4px rgba(0, 0, 0, 0.8), 0 6px 20px rgba(0, 0, 0, 0.5);[[/highlight]] } {% endprettify %}
Making a circle in CSS requires a workaround of applying a border-radius of 50% to all four sides of a rectangle, though there are basic shapes.
While this approach is supported with the borderRadius
property of
BoxDecoration,
Flutter provides a shape
property with
BoxShape enum
for this purpose.
.greybox { background-color: #e0e0e0; /* gray 300 / width: 320px; height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redcircle { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; [[highlight]] text-align: center; width: 160px; height: 160px; border-radius: 50%; [[/highlight]] } {% endprettify %}
The following examples show how to specify fonts and other text attributes. They also show how to transform text strings, customize spacing, and create excerpts.
In CSS you specify the amount of white space between each letter or word by giving a length value for the letter-spacing and word-spacing properties, respectively. The amount of space can be in px, pt, cm, em, etc.
In Flutter, you specify white space as logical pixels
(negative values are allowed)
for the letterSpacing
and wordSpacing
properties of a
TextStyle
child of a Text widget.
.greybox { background-color: #e0e0e0; /* grey 300 / width: 320px; height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redbox { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; [[highlight]] letter-spacing: 4px; [[/highlight]] } {% endprettify %}
A Text widget lets
you display text with the same formatting characteristics. To
display text that uses multiple styles (in this example, a single word with
emphasis), use a
RichText
widget instead. Its text
property can specify one or more
TextSpan widgets
that can be individually styled.
In the following example, "Lorem" is in a TextSpan widget with the default (inherited) text styling, and "ipsum" is in a separate TextSpan with custom styling.
.greybox { background-color: #e0e0e0; /* grey 300 / width: 320px; height: 240px; [[highlight]] font: 900 24px Roboto; [[/highlight]] display: flex; align-items: center; justify-content: center; } .redbox { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; } [[highlight]] .redbox em { font: 300 48px Roboto; font-style: italic; } [[/highlight]] {% endprettify %}
An excerpt displays the initial line(s) of text in a paragraph, and handles the overflow text, often using an ellipsis. In HTML/CSS an excerpt can be no longer than one line. Truncating after multiple lines requires some JavaScript code.
In Flutter, use the maxLines
property of a
Text
widget to specify the number of lines to include in the excerpt, and the
overflow
property for handling overflow text.
.greybox { background-color: #e0e0e0; /* grey 300 / width: 320px; height: 240px; font: 900 24px Roboto; display: flex; align-items: center; justify-content: center; } .redbox { background-color: #ef5350; / red 400 */ padding: 16px; color: #ffffff; [[highlight]] overflow: hidden; text-overflow: ellipsis; white-space: nowrap; [[/highlight]] } {% endprettify %}