Styling with CSS


One of the most powerful features in flutter-view is that it allows you to use CSS styles to flutter widgets, and to set any property of any flutter widget.

For example, you can start with a simple Container:

    .example Hello world!

This will generate a function that returns a Container with the class name "example", that in turns contains a Text widget with the text "Hello world".

Then you can style this Container by assigning Sass styles to the class:

    width: 500
    height: 300
    color: red
    background-color: #333
    font-size: 20
    font-weight: bold

Flutter-view will process your styles, attaching them to the classes. Properties such as width and height are directly assigned. Some properties are recognized as CSS properties, and generate more code, such as color and font-size. The result is a normal Dart function you can call in your normal Dart code to render the styled view. In this case, a grey box with red bold text saying "Hello world!".

Structuring your files for styling

You can use CSS or Sass to set any property to any class or id in your Pug or HTML file.

To style a Pug file, create a Sass style file with the same name (but different extension) as your Pug file, in the same directory. For example, if you have a startpage.pug, to style it simply add a startpage.sass in the same directory.

Recommendation: create a directory per layout, with the name of your layout. Then inside, create a pug file, sass file and your model and other supporting files.

Example structure:

See the example projects for more ideas for structuring your application.

Applying styles as properties

You can apply extra properties to html elements by adding them through style rules.

To style anything in a flutter-view you:

  1. add classes to the elements in your pug file,

  2. add style rules to these classes in the related sass file

To assign a class in pug, use the Pug .classname syntax. Any div element becomes a Container widget.

For example, given the following Pug:

.message hello!

This will translate into the following HTML:

<div class="message">hello!</div>

You can then use the .message class to assign properties using Sass or CSS:

    width: 200
    height: 200

The resulting Dart will be a combination of your layout and style:

  child: Text( 
  width: 200,
  height: 200,

Using shortcut properties in styles

Flutter-view provides many shortcut properties, that let you style in a CSS-style manner.

Some examples are color, padding, margin and background-image. See the shortcut properties reference for the full list.

As an example, consider the following Pug layout we want to style (taken and converted into flutter-view Pug from the Flutter Card sample):

            icon(as='leading' :value='Icons.album')
            .title(as='title') The Enchanted Nightingale
            .subtitle(as='subtitle') Music by Julie Gable. Lyrics by Sidney Stein.
                    .label Buy tickets
                    .label Listen

We have a layout, and now we can style it.

The "buy tickets" and "listen" FlatButtons we want to have uppercase text. We can use the text-transform shortcut:

        text-transform: uppercase

We want the card to be blue and the column of the card to have mainAxisSize: MainAxisSize.min:. Here we can use the color shortcut, so we can use CSS colors, and the main-axis-size shortcut, which lets us simply use 'min':

    color: blue
        main-axis-size: min

We want to give some padding to the title and subtitle, and give each slightly different colors. Padding and margin are shortcuts that adhere to CSS standards:

            color: white
            padding: 4 6
            color: grey[100]
            padding: 2 6

Here is the end result:

artist-card(flutter-view :on-buy-pressed :on-listen-pressed)    
                icon(as='leading' :value='Icons.album')
                .title(as='title') The Enchanted Nightingale
                .subtitle(as='subtitle') Music by Julie Gable. Lyrics by Sidney Stein.
                        .label Buy tickets
                        .label Listen

As you can see here, Sass is a nice match with Pug, since you can retain the same structure. This makes it easy to find the matching styles to your pug elements.

Setting expressions

You can set an expression in CSS by wrapping the expression in quotes and starting it with a colon.

It is recommended to keep Dart expressions in your Pug files. However, in some cases it can be practical to be able to set an expression as a CSS value, for example if flutter-view does not have special support for it.

Say you want to achieve the following Dart:

    defaultVerticalAlignment: TableCellVerticalAlignment.middle,
    children: [ ... ]

Which you can achieve with the following (a bit contrived) Pug code:


Now let's move the defaultVerticleAlignment property into a Sass file:

    default-verticle-alignment: ':TableCellVerticalAlignment.middle'

Note the colon before TableCellVerticleAlignment.middlein the Sass file.

Using Flutter Themes in styles

Flutter's Material library has theming support. You may want to assign the values of the theme of the current BuildContext. Flutter-view has support for easily assigning ThemeData values to your properties.

To understand how it works, let's first look at how you would use it in pure Dart code. To assign a font size to a style, you may write something like this:

style: TextStyle( 
  fontSize: Theme.of(context).textTheme.title.fontSize,

First we find the Theme of the current BuildContext, then we get a path of properties.

In flutter-view CSS, you might write this the same way:

    font-size: ':Theme.of(context).textTheme.title.fontSize'

Instead, you may write it like this:

    font-size: theme(text-theme/title/font-size)

This still requires the context to be available. If you have no current context, you can use the builder shortcut. The theme properties path has been replaced by dash-cased steps, separated by forward slashes/.

Pro tip: You can define CSS classes that set multiple theme style properties at once and reuse them across your app.

Last updated