|
| 1 | +# Styling with CSS |
| 2 | + |
| 3 | +## Structuring your files for styling |
| 4 | + |
| 5 | +**You can use CSS or Sass to set any property to any class or id in your Pug or HTML file.** |
| 6 | + |
| 7 | +To do so, create a style file with the same name as your Pug or HTML 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. |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +Example structure: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +See the[ example projects](examples.md) for more ideas for structuring your application. |
| 16 | + |
| 17 | +## Applying styles as properties |
| 18 | + |
| 19 | +**You can apply extra properties to html elements by adding them through style rules.** |
| 20 | + |
| 21 | +For example, given the following Pug: |
| 22 | + |
| 23 | +```c |
| 24 | +.message hello! |
| 25 | +``` |
| 26 | + |
| 27 | +This will translate into the following HTML: |
| 28 | + |
| 29 | +```markup |
| 30 | +<div class="message">hello!</div> |
| 31 | +``` |
| 32 | + |
| 33 | +You can then use the `.message` class to assign properties using Sass or CSS: |
| 34 | + |
| 35 | +```css |
| 36 | +.message |
| 37 | + width: 200 |
| 38 | + height: 200 |
| 39 | +``` |
| 40 | + |
| 41 | +The resulting Dart will be a combination of your layout and style: |
| 42 | + |
| 43 | +```dart |
| 44 | +Container( |
| 45 | + child: Text( |
| 46 | + 'hello!', |
| 47 | + ), |
| 48 | + width: 200, |
| 49 | + height: 200, |
| 50 | +); |
| 51 | +``` |
| 52 | + |
| 53 | +## Setting expressions |
| 54 | + |
| 55 | +**You can set an expression in CSS by wrapping the expression in quotes and starting it with a colon.** |
| 56 | + |
| 57 | +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 value, for example if flutter-view does not have special support for it. |
| 58 | + |
| 59 | +Say you want to achieve the following Dart: |
| 60 | + |
| 61 | +```dart |
| 62 | +Table( |
| 63 | + defaultVerticalAlignment: TableCellVerticalAlignment.middle, |
| 64 | + children: [ ... ] |
| 65 | +) |
| 66 | +``` |
| 67 | + |
| 68 | +Which you can achieve with the following Pug code: |
| 69 | + |
| 70 | +```css |
| 71 | +table(:default-verticle-alignment='TableCellVerticalAlignment.middle') |
| 72 | + ...children... |
| 73 | +``` |
| 74 | + |
| 75 | +Now let's move the `defaultVerticleAlignment` property into a Sass file: |
| 76 | + |
| 77 | +{% tabs %} |
| 78 | +{% tab title="Sass" %} |
| 79 | +```css |
| 80 | +table |
| 81 | + default-verticle-alignment: ':TableCellVerticalAlignment.middle' |
| 82 | +``` |
| 83 | +{% endtab %} |
| 84 | + |
| 85 | +{% tab title="Pug" %} |
| 86 | +```css |
| 87 | +table |
| 88 | + ...children... |
| 89 | +``` |
| 90 | +{% endtab %} |
| 91 | +{% endtabs %} |
| 92 | + |
| 93 | +Note the colon before `TableCellVerticleAlignment.middle`in the pug file. |
| 94 | + |
| 95 | +## Using shortcut properties in styles |
| 96 | + |
| 97 | +**Flutter-view provides many shortcut properties, that let you style in a CSS-style manner.** |
| 98 | + |
| 99 | +Some examples are [color](../reference/css-properties.md#color-color), [padding](../reference/css-properties.md#padding), [margin](../reference/css-properties.md#margin) and [background-image](../reference/css-properties.md#box-shadow-1). |
| 100 | + |
| 101 | +See the [shortcut properties reference](../reference/css-properties.md) for the full list. |
| 102 | + |
| 103 | +As an example, consider the following Pug layout we want to style \(taken and converted into flutter-view Pug from the [Flutter Card sample](https://docs.flutter.io/flutter/material/Card-class.html)\): |
| 104 | + |
| 105 | +```text |
| 106 | +card |
| 107 | + column |
| 108 | + list-tile |
| 109 | + icon(as='leading' :value='Icons.album') |
| 110 | + .title(as='title') The Enchanted Nightingale |
| 111 | + .subtitle(as='subtitle') Music by Julie Gable. Lyrics by Sidney Stein. |
| 112 | + button-theme:bar |
| 113 | + button-bar |
| 114 | + flat-button.tickets(@on-pressed='...') |
| 115 | + .label Buy tickets |
| 116 | + flat-button.listen(@on-pressed='...') |
| 117 | + .label Listen |
| 118 | +``` |
| 119 | + |
| 120 | +We have a layout, and now we can style it. |
| 121 | + |
| 122 | +The "buy tickets" and "listen" buttons we want to have uppercase text: |
| 123 | + |
| 124 | +```css |
| 125 | +flat-button.tickets, flat-button.listen |
| 126 | + .label |
| 127 | + text-transform: uppercase |
| 128 | +``` |
| 129 | + |
| 130 | +We want the card to be blue and the column of the card to have `mainAxisSize: MainAxisSize.min`: |
| 131 | + |
| 132 | +```css |
| 133 | +card |
| 134 | + color: blue |
| 135 | + column |
| 136 | + main-axis-size: min |
| 137 | +``` |
| 138 | + |
| 139 | +We want to give some padding to the title and subtitle, and make the text color grey\[300\]: |
| 140 | + |
| 141 | +```css |
| 142 | +card |
| 143 | + $title-color: grey[300] |
| 144 | + list-tile |
| 145 | + .title |
| 146 | + color: $title-color |
| 147 | + padding: 4 6 |
| 148 | + .subtitle |
| 149 | + color: $title-color |
| 150 | + padding: 2 6 |
| 151 | +``` |
| 152 | + |
| 153 | +Note how we use Sass variables to avoid repetition. |
| 154 | + |
| 155 | +Here is the end result: |
| 156 | + |
| 157 | +{% tabs %} |
| 158 | +{% tab title="artist-card.pug" %} |
| 159 | +```css |
| 160 | +card |
| 161 | + column |
| 162 | + list-tile |
| 163 | + icon(as='leading' :value='Icons.album') |
| 164 | + .title(as='title') The Enchanted Nightingale |
| 165 | + .subtitle(as='subtitle') Music by Julie Gable. Lyrics by Sidney Stein. |
| 166 | + button-theme:bar |
| 167 | + button-bar |
| 168 | + flat-button.tickets(@on-pressed='...') |
| 169 | + .label Buy tickets |
| 170 | + flat-button.listen(@on-pressed='...') |
| 171 | + .label Listen |
| 172 | +``` |
| 173 | +{% endtab %} |
| 174 | + |
| 175 | +{% tab title="artist-card.sass" %} |
| 176 | +```css |
| 177 | +card |
| 178 | + color: blue |
| 179 | + column |
| 180 | + main-axis-size: min |
| 181 | + list-tile |
| 182 | + $title-color: grey[300] |
| 183 | + .title |
| 184 | + color: $title-color |
| 185 | + padding: 4 6 |
| 186 | + .subtitle |
| 187 | + color: $title-color |
| 188 | + padding: 2 6 |
| 189 | + flat-button |
| 190 | + .label |
| 191 | + text-transform: uppercase |
| 192 | +``` |
| 193 | +{% endtab %} |
| 194 | + |
| 195 | +{% tab title="generated artist-card.dart" %} |
| 196 | + |
| 197 | +{% endtab %} |
| 198 | +{% endtabs %} |
| 199 | + |
| 200 | + |
| 201 | + |
0 commit comments