Skip to content

Commit 31d147f

Browse files
blueneogeogitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent 9cebde9 commit 31d147f

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

guide/untitled.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,145 @@
11
# Styling per platform
22

3+
When you write your Flutter app you probably want to target both iOS and Android. Flutter-view styling can make this easier for you, using the standard tools from CSS, Pug and Sass.
4+
5+
## The platform widgets library
6+
7+
First tip is, use the [**platform\_widgets library**](https://pub.dartlang.org/packages/flutter_platform_widgets), and add it as an [default import](configuring-flutter-view.md#imports) in flutter-view.json:
8+
9+
{% code-tabs %}
10+
{% code-tabs-item title="flutter-view.json" %}
11+
```javascript
12+
{
13+
"imports": [
14+
"package:flutter_view_tools/flutter_view_tools.dart",
15+
"package:flutter_platform_widgets/flutter_platform_widgets.dart"
16+
]
17+
}
18+
```
19+
{% endcode-tabs-item %}
20+
{% endcode-tabs %}
21+
22+
Put `flutter-view.json` in the root of your Flutter project.
23+
24+
This will allow you to use widgets that adapt to the platform they are used on, and also provides you with the short [**isCupertino**](https://pub.dartlang.org/documentation/flutter_platform_widgets/latest/flutter_platform_widgets/isCupertino.html) and [**isMaterial**](https://pub.dartlang.org/documentation/flutter_platform_widgets/latest/flutter_platform_widgets/isMaterial.html) properties, that you can use throughout your layouts.
25+
26+
## Layout per platform
27+
28+
A common pattern is a [**slot**](flow-control.md#slot) with two implementations with two [**if**](flow-control.md#if) statements below it, one per platform:
29+
30+
{% code-tabs %}
31+
{% code-tabs-item title="pug" %}
32+
```css
33+
.foo
34+
slot
35+
.ios(if='isCupertino')
36+
...iOS layout here...
37+
.android(if='isMaterial')
38+
...Android layout here...
39+
```
40+
{% endcode-tabs-item %}
41+
42+
{% code-tabs-item title="css" %}
43+
```
44+
.foo
45+
.ios
46+
// ios layout styling here
47+
.android
48+
// android layout styling here
49+
```
50+
{% endcode-tabs-item %}
51+
{% endcode-tabs %}
52+
53+
The above will render different layout depending on the phone OS you run it on. Since we are adding a different class, we can also add different styling through CSS.
54+
55+
## Same layout but different styling per platform
56+
57+
A very common situation is that we have the same basic layout, but want to use different CSS styling per layout. We could do the same as above:
58+
59+
```css
60+
.foo
61+
slot
62+
.ios(if='isCupertino')
63+
.some
64+
.layout
65+
.here
66+
.android(if='isMaterial')
67+
.some
68+
.layout
69+
.here
70+
```
71+
72+
This will work, we can apply different styling for .bar.ios and .bar.android. However we are repeating ourselves in the layout. This can be quite redundant.
73+
74+
Instead, we can let Pug mixins help us. We can make a default.pug in our project that multiple view pugs can import. Then in this pug we can write a mixin:
75+
76+
{% code-tabs %}
77+
{% code-tabs-item title="default.pug" %}
78+
```css
79+
mixin platform-slot
80+
slot
81+
.ios-slot(if='isCupertino')
82+
.ios
83+
block
84+
.android-slot(if='isMaterial')
85+
.android
86+
block
87+
```
88+
{% endcode-tabs-item %}
89+
{% endcode-tabs %}
90+
91+
We can then import this tool into our view and use it like this:
92+
93+
{% code-tabs %}
94+
{% code-tabs-item title="using mixin" %}
95+
```css
96+
include /screens/default.pug
97+
98+
.foo
99+
platform-slot
100+
.some
101+
.layout
102+
.here
103+
```
104+
{% endcode-tabs-item %}
105+
106+
{% code-tabs-item title="generated pug" %}
107+
```css
108+
include /screens/default.pug
109+
110+
.foo
111+
slot
112+
.ios-slot(if='isCupertino')
113+
.ios
114+
.bar
115+
.some
116+
.layout
117+
.here
118+
.android-slot(if='isMaterial')
119+
.android
120+
.bar
121+
.some
122+
.layout
123+
.here
124+
```
125+
{% endcode-tabs-item %}
126+
127+
{% code-tabs-item title="css" %}
128+
```
129+
.foo
130+
.ios
131+
.some
132+
// ios layout styling here
133+
.android
134+
.some
135+
// android layout styling here
136+
```
137+
{% endcode-tabs-item %}
138+
{% endcode-tabs %}
139+
140+
Now we have no repetition in our layout!
141+
142+
However, there is a downside: the generated pug will not know what source code line it came to, and as a result, you will not get the [source reference comments in the generated Dart](configuring-flutter-view.md#showpuglinenumbers). This means that in VSCode, the [flutter-view extension](../get-started/vs-code-support.md#linking-between-pug-and-generated-dart) hotlinking will not work for these lines.
143+
144+
_Note: kudos for Floris van der Grinten for this nifty solution_
145+

0 commit comments

Comments
 (0)