README_PT.md 11.6 KB
Newer Older
T
Thalles Santos 已提交
1 2 3 4 5 6 7 8

# flutter_screenutil
[![pub package](https://img.shields.io/pub/v/flutter_screenutil.svg)](https://pub.dartlang.org/packages/flutter_screenutil)

**Um plugin Flutter para adaptação de tamanho de tela e fontes. Deixe sua UI exibir um layout aceitável em diferentes tamanhos de tela!**

*Nota*: Este plugin ainda está em desenvolvimento e algumas APIs podem ainda não estar disponíveis.

L
LiZhuoyuan 已提交
9
[中文文档](https://github.com/OpenFlutter/flutter_screenutil/blob/master/README_CN.md)  
L
LiZhuoyuan 已提交
10

T
Thalles Santos 已提交
11 12 13 14 15 16 17 18 19 20
[README of English](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/README.md)

github: https://github.com/OpenFlutter/flutter_screenutil

[Histórico de atualizações](/CHANGELOG.md)

## Como usar:

### Adicionando a dependência:
Por favor, verifique a última versão antes da instalação.
L
2.0.0  
LiZhuoyuan 已提交
21
Se houver algum problema com a nova versão, use a versão anterior
T
Thalles Santos 已提交
22 23 24 25
```
dependencies:
  flutter:
    sdk: flutter
L
lizhuoyuan 已提交
26
  # add flutter_screenutil
L
2.0.0  
LiZhuoyuan 已提交
27
  flutter_screenutil: ^2.0.0
T
Thalles Santos 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
```

### Adicione o seguinte import em seu código Dart:
```
import 'package:flutter_screenutil/flutter_screenutil.dart';
```

### Propriedades
   
|Propriedade|Tipo|Valor Padrão|Descrição|
|:---|:---|:---|:---| 
|width|double|1080px|A largura do dispositivo no protótipo de design, em px|
|height|double|1920px|A altura do dispositivo no protótipo de design, em px|
|allowFontScaling|bool|false|Defina caso o tamanho da fonte seja dimensionado de acordo com a opção "tamanho de fonte" na acessibilidade do sistema|

### Inicialize e defina o tamanho de ajuste e tamanho da fonte para dimensionar de acordo com a opção "tamanho de fonte" na acessibilidade do sistema
Por favor, defina a largura e altura do protótipo de design antes de usar (em pixels).
Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou seja, no arquivo de entrada, defina apenas uma vez) para garantir que o tamanho de ajuste seja o mesmo antes de cada uso:

```dart

//Preencha o tamanho da tela do dispositivo no protótipo de design

//Valor padrão: width : 1080px , height:1920px , allowFontScaling:false
L
2.0.0  
LiZhuoyuan 已提交
52 53
ScreenUtil.init(context);        //flutter_screenuitl < 1.2
ScreenUtil.init();               //flutter_screenuitl >= 1.2
T
Thalles Santos 已提交
54 55

//Se o design é baseado no iPhone6 ​​(iPhone6 ​​750*1334)
L
2.0.0  
LiZhuoyuan 已提交
56 57
ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2
ScreenUtil.init(width: 750, height: 1334);          //flutter_screenuitl >= 1.2
T
Thalles Santos 已提交
58 59

//Se você quer definir que o tamanho da fonte seja ajustado de acordo com a opção "tamanho da fonte" na acessibilidade do sistema
L
2.0.0  
LiZhuoyuan 已提交
60 61
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true);    //flutter_screenuitl < 1.2
ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true);             //flutter_screenuitl >= 1.2
T
Thalles Santos 已提交
62 63 64 65 66

```

### Uso:

L
lizhuoyuan 已提交
67
### API
L
lizhuoyuan 已提交
68 69

```dart
L
lizhuoyuan 已提交
70
    ScreenUtil().setWidth(540)  (sdk>=2.6 : 540.w) //Adapted to screen width
L
lizhuoyuan 已提交
71
    ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //Adapted to screen height
L
lizhuoyuan 已提交
72
    ScreenUtil().setSp(24)      (sdk>=2.6 : 24.sp)  //Adapter font
L
lizhuoyuan 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
    ScreenUtil().setSp(24, allowFontScalingSelf: true)  (sdk>=2.6 : 24.ssp) //Adapter font(fonts will scale to respect Text Size accessibility settings)

    ScreenUtil.pixelRatio       //Device pixel density
    ScreenUtil.screenWidth      //Device width
    ScreenUtil.screenHeight     //Device height
    ScreenUtil.bottomBarHeight  //Bottom safe zone distance, suitable for buttons with full screen
    ScreenUtil.statusBarHeight  //Status bar height , Notch will be higher Unit px
    ScreenUtil.textScaleFactor  //System font scaling factor

    ScreenUtil().scaleWidth //Ratio of actual width dp to design draft px
    ScreenUtil().scaleHeight //Ratio of actual height dp to design draft px
```

T
Thalles Santos 已提交
86 87 88 89
#### Adaptar o tamanho da tela:

Informe o tamanho em pixels do protótipo de design:

L
1.0.0  
lizhuoyuan 已提交
90
Adaptado à largura da tela: `ScreenUtil().setWidth(540)`,
T
Thalles Santos 已提交
91

L
1.0.0  
lizhuoyuan 已提交
92
Adaptado à altura da tela: `ScreenUtil().setHeight(200)`,
T
Thalles Santos 已提交
93

L
lizhuoyuan 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
If your dart sdk>=2.6, you can use extension functions:

example:
instead of :
```
Container(
width: ScreenUtil().setWidth(50),
height:ScreenUtil().setHeight(200),
)
```
you can use it like this:
```
Container(
width: 50.w,
height:200.h
)
```

T
Thalles Santos 已提交
112 113 114 115 116 117 118 119 120 121
**Nota** 

Altura também é adaptada de acordo com o setWidth para garantir que não tenha deformação (quando quiser um quadrado)

O método setHeight é a principal forma de adaptar a altura, se quiser controlar a altura e a realidade de uma tela na UiUsed quando a mesma for exibida.

```dart
//Exemplo:
//Retângulo
Container(
L
1.0.0  
lizhuoyuan 已提交
122 123
           width: ScreenUtil().setWidth(375),
           height: ScreenUtil().setHeight(200),
T
Thalles Santos 已提交
124 125 126 127 128
           ...
            ),
            
////Se quiser exibir um quadrado:
Container(
L
1.0.0  
lizhuoyuan 已提交
129 130
           width: ScreenUtil().setWidth(300),
           height: ScreenUtil().setWidth(300),
T
Thalles Santos 已提交
131 132 133 134 135 136 137 138
            ),
            
```

#### Adaptar fonte:
``` dart
//Tamanho da fonte informado, em pixels, a fonte não irá dimensionar respeitando a opção "Tamanho de Fonte" nas configurações de acessibilidade
//(allowFontScaling quando iniciar o ScreenUtil)
L
1.0.0  
lizhuoyuan 已提交
139
ScreenUtil().setSp(28)    
T
Thalles Santos 已提交
140 141 142
     
//Tamanho da fonte informado,em pixels,a fonte irá dimensionar respeitando a opção "Tamanho de Fonte" nas configurações de acessibilidade
//(Se em algum lugar não seguir a configuração global da propriedade allowFontScaling)
L
1.0.0  
lizhuoyuan 已提交
143
ScreenUtil().setSp(24, allowFontScalingSelf: true)
T
Thalles Santos 已提交
144 145 146 147 148 149 150 151 152 153

//Exemplo:

Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                    'Minha fonte tem 24px no protótipo de design e não irá mudar com o sistema.',
                    style: TextStyle(
                      color: Colors.black,
L
1.0.0  
lizhuoyuan 已提交
154
                      fontSize: ScreenUtil().setSp(24),
T
Thalles Santos 已提交
155 156 157 158
                    )),
                Text(
                    'Minha fonte tem 24px no protótipo de design e poderá mudar com o sistema.',
                    style: TextStyle(
L
1.0.0  
lizhuoyuan 已提交
159 160 161
                        color: Colors.black,
                        fontSize: ScreenUtil()
                            .setSp(24, allowFontScalingSelf: true))),
T
Thalles Santos 已提交
162 163 164 165 166 167 168 169 170 171 172
              ],
            )
```

#### Outras APIs relacionadas:
```dart
    ScreenUtil.pixelRatio       //Densidade de pixels do dispositivo
    ScreenUtil.screenWidth      //Largura da tela do dispositivo
    ScreenUtil.screenHeight     //Altura da tela do dispositivo
    ScreenUtil.bottomBarHeight  //Distância segura do rodapé, adequada para botões em tela cheia
    ScreenUtil.statusBarHeight  //Altura da status bar em pixels, Notch será maior
L
lizhuoyuan 已提交
173
    ScreenUtil.textScaleFactor //Fator de escala da fonte do sistema
T
Thalles Santos 已提交
174

L
1.0.0  
lizhuoyuan 已提交
175 176
    ScreenUtil().scaleWidth //Razão entre a largura atual e a largura do protótipo de design em pixels
    ScreenUtil().scaleHeight //Razão entre a altura atual e a altura do protótipo de design em pixels
T
Thalles Santos 已提交
177 178 179 180 181 182 183 184 185 186 187 188

```

```dart
//import
import 'package:flutter_screenutil/flutter_screenutil.dart';

...

  @override
  Widget build(BuildContext context) {
    ///Define o tamanho de ajuste (preenche o tamanho da tela do dispositivo no design). Se o design é baseado no tamanho do iPhone6 (iPhone6 ​​750*1334)
L
2.0.0  
LiZhuoyuan 已提交
189
    ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
T
Thalles Santos 已提交
190 191 192 193 194 195 196 197 198 199
    
    print('Largura do dispositivo:${ScreenUtil.screenWidth}'); //Largura do dispositivo
    print('Altura do dispositivo:${ScreenUtil.screenHeight}'); //Altura do dispositivo
    print(
        'Densidade de pixels do dispositivo:${ScreenUtil.pixelRatio}'); //Densidade de pixels do dispositivo
    print(
        'Distância segura do rodapé:${ScreenUtil.bottomBarHeight}'); //Distância segura do rodapé, adequada para botões em tela cheia
    print(
        'Altura da status bar:${ScreenUtil.statusBarHeight}px'); //Altura da status bar em pixels, Notch será maior
    print(
L
1.0.0  
lizhuoyuan 已提交
200
        'Razão entre a largura atual e a largura do protótipo de design em pixels:${ScreenUtil().scaleWidth}'); 
T
Thalles Santos 已提交
201
    print(
L
1.0.0  
lizhuoyuan 已提交
202
        'Razão entre a altura atual e a altura do protótipo de design em pixels:${ScreenUtil().scaleHeight}'); 
T
Thalles Santos 已提交
203
    print(
L
1.0.0  
lizhuoyuan 已提交
204
        'Razão da fonte e largura para o tamanho do design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}');
T
Thalles Santos 已提交
205
    print(
L
1.0.0  
lizhuoyuan 已提交
206
        'Razão da fonte e altura para o tamanho do design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}');
T
Thalles Santos 已提交
207
        
L
lizhuoyuan 已提交
208 209 210
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
T
Thalles Santos 已提交
211
      ),
L
lizhuoyuan 已提交
212
      body: SingleChildScrollView(
T
Thalles Santos 已提交
213 214 215 216 217 218
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
L
1.0.0  
lizhuoyuan 已提交
219 220 221
                  padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
T
Thalles Santos 已提交
222 223
                  color: Colors.red,
                  child: Text(
L
1.0.0  
lizhuoyuan 已提交
224
                    'Minha largura:${ScreenUtil().setWidth(375)}dp',
T
Thalles Santos 已提交
225 226
                    style: TextStyle(
                        color: Colors.white,
L
1.0.0  
lizhuoyuan 已提交
227
                        fontSize: ScreenUtil().setSp(12)),
T
Thalles Santos 已提交
228 229 230
                  ),
                ),
                Container(
L
1.0.0  
lizhuoyuan 已提交
231 232 233
                  padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
T
Thalles Santos 已提交
234
                  color: Colors.blue,
L
1.0.0  
lizhuoyuan 已提交
235
                  child: Text('Minha largura:${ScreenUtil().setWidth(375)}dp',
T
Thalles Santos 已提交
236 237
                      style: TextStyle(
                          color: Colors.white,
L
1.0.0  
lizhuoyuan 已提交
238
                          fontSize: ScreenUtil().setSp(12))),
T
Thalles Santos 已提交
239 240 241
                ),
              ],
            ),
L
2.0.0  
LiZhuoyuan 已提交
242 243
            Text('Largura do dispositivo:${ScreenUtil.screenWidth}dp'),
            Text('Altura do dispositivo:${ScreenUtil.screenHeight}dp'),
T
Thalles Santos 已提交
244
            Text('Densidade de pixels do dispositivo:${ScreenUtil.pixelRatio}'),
L
2.0.0  
LiZhuoyuan 已提交
245 246
            Text('Distância segura do rodapé:${ScreenUtil.bottomBarHeight}dp'),
            Text('Altura da status bar:${ScreenUtil.statusBarHeight}dp'),
T
Thalles Santos 已提交
247
            Text(
L
1.0.0  
lizhuoyuan 已提交
248
              'Razão entre a largura atual e a largura do protótipo de design em pixels:${ScreenUtil().scaleWidth}',
T
Thalles Santos 已提交
249 250 251
              textAlign: TextAlign.center,
            ),
            Text(
L
1.0.0  
lizhuoyuan 已提交
252
              'Razão entre a altura atual e a altura do protótipo de design em pixels:${ScreenUtil().scaleHeight}',
T
Thalles Santos 已提交
253 254 255
              textAlign: TextAlign.center,
            ),
            Text(
L
1.0.0  
lizhuoyuan 已提交
256
              'Razão da fonte e largura para o tamanho do design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}',
T
Thalles Santos 已提交
257 258 259
              textAlign: TextAlign.center,
            ),
            Text(
L
1.0.0  
lizhuoyuan 已提交
260
              'Razão da fonte e altura para o tamanho do design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}',
T
Thalles Santos 已提交
261 262 263
              textAlign: TextAlign.center,
            ),
            SizedBox(
L
1.0.0  
lizhuoyuan 已提交
264
              height: ScreenUtil().setHeight(100),
T
Thalles Santos 已提交
265
            ),
L
lizhuoyuan 已提交
266
            Text('Fator de escala da fonte do sistema:${ScreenUtil.textScaleFactor}'),
T
Thalles Santos 已提交
267 268 269 270 271 272 273
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                    'Minha fonte tem 24px no protótipo de design e não irá mudar com o sistema.',
                    style: TextStyle(
                      color: Colors.black,
L
1.0.0  
lizhuoyuan 已提交
274
                      fontSize: ScreenUtil().setSp(24),
T
Thalles Santos 已提交
275 276 277 278 279
                    )),
                Text(
                    'Minha fonte tem 24px no protótipo de design e poderá mudar com o sistema.',
                    style: TextStyle(
                      color: Colors.black,
L
1.0.0  
lizhuoyuan 已提交
280
                      fontSize: ScreenUtil().setSp(24, allowFontScalingSelf: true),
T
Thalles Santos 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
                    )),
              ],
            )
          ],
        ),
      ),
    );
  }
```

### Exemplo:

[Demonstração](/example/lib/main_zh.dart)
 
Efeito:

![效果](demo_en.png)
L
1.0.0  
lizhuoyuan 已提交
298
![tablet effect](demo_tablet_en.png)