/
main.dart
223 lines (219 loc) · 7.5 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import 'package:flutter/material.dart';
import 'package:screen_adaptation/screen_adaptation.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return OrientationBuilder(
builder: (BuildContext context, Orientation orientation) => MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
builder: (BuildContext context, Widget child) {
return ScreenAdaptationInit(
size: Size(750, 1334), child: child);
},
));
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final bool _isPortrait =
ScreenAdaptationUtil.getInstance().orientation == Orientation.portrait;
return Scaffold(
appBar: AppBar(
title: Text('screen_adaptation'),
actions: [
TextButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => DomePage())),
child: Text(
'dome',
style: TextStyle(fontSize: 48.s, color: Colors.white),
))
],
),
body: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: EdgeInsets.all(30.w),
child: Text(
'旋转屏幕后UI会自动进行适配',
style: TextStyle(fontSize: 48.s),
),
),
Container(
width: _isPortrait ? 750.w : 1334.w,
height: 100.w,
color: Colors.blueGrey,
alignment: Alignment.center,
child: Text(
'宽度100%',
style: TextStyle(color: Colors.white, fontSize: 35.s),
),
),
Container(
margin: EdgeInsets.only(top: 30.w),
width: _isPortrait ? 345.w : 667.w,
height: _isPortrait ? 667.h : 345.w,
color: Colors.blueGrey,
alignment: Alignment.center,
child: Text(
'宽度50% 高度50%',
style: TextStyle(color: Colors.white, fontSize: 35.s),
),
),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class DomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
padding: EdgeInsets.only(
top: ScreenAdaptationUtil.getInstance().statusBarHeight + 30.w,
left: 30.w,
right: 30.w,
bottom: 30.w),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Wrap(
spacing: 30.w,
runSpacing: 30.w,
children: [
Container(
width: ScreenAdaptationUtil.getInstance().setWidth(100),
height: ScreenAdaptationUtil.getInstance().setWidth(100),
color: Colors.blueGrey,
alignment: Alignment.center,
),
Container(
width: 100.h,
height: 100.w,
color: Colors.blueGrey,
alignment: Alignment.center,
),
Container(
width: 100.w,
height: 100.h,
color: Colors.blueGrey,
alignment: Alignment.center,
),
Container(
width: 100.h,
height: 100.h,
color: Colors.blueGrey,
alignment: Alignment.center,
),
Container(
width: 100.r,
height: 100.r,
color: Colors.blueGrey,
alignment: Alignment.center,
),
],
),
Container(
child: Text('设计宽度:${ScreenAdaptationUtil.getInstance().width}'),
),
Container(
child: Text('设计高度:${ScreenAdaptationUtil.getInstance().height}'),
),
Container(
child: Text(
'屏幕宽度:${ScreenAdaptationUtil.getInstance().screenWidth}'),
),
Container(
child: Text(
'屏幕高度:${ScreenAdaptationUtil.getInstance().screenHeight}'),
),
Container(
child:
Text('屏幕密度:${ScreenAdaptationUtil.getInstance().pixelRatio}'),
),
Container(
child: Text(
'文字缩放比例:${ScreenAdaptationUtil.getInstance().textScaleFactor}'),
),
Container(
child: Text(
'状态栏高度:${ScreenAdaptationUtil.getInstance().statusBarHeight}'),
),
Container(
child: Text(
'底部安全距离:${ScreenAdaptationUtil.getInstance().bottomBarHeight}'),
),
Container(
child: Text(
'是否允许字体根据系统缩放变化:${ScreenAdaptationUtil.getInstance().allowFontScaling}'),
),
Container(
margin: EdgeInsets.only(top: 30.w),
child: Text(
'不会根据系统缩放变化的文字',
style: TextStyle(
fontSize: ScreenAdaptationUtil.getInstance()
.setSp(60, allowFontScaling: false)),
),
),
Container(
margin: EdgeInsets.only(top: 30.w),
child: Text(
'不会根据系统缩放变化的文字',
style: TextStyle(fontSize: 60.sf),
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 30.w),
child: Text(
'因为ScreenAdaptationInit allowFontScaling默认为false 我也不会变化',
style: TextStyle(fontSize: 60.s),
),
),
Container(
child: Text(
'会根据系统缩放变化的文字 缩放了 ${ScreenAdaptationUtil.getInstance().textScaleFactor}',
style: TextStyle(
color: Colors.blueGrey,
fontSize: ScreenAdaptationUtil.getInstance()
.setSp(60, allowFontScaling: true)),
),
),
Container(
child: Text(
'会根据系统缩放变化的文字 缩放了 ${ScreenAdaptationUtil.getInstance().textScaleFactor}',
style: TextStyle(color: Colors.blueGrey, fontSize: 60.st),
),
),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
bottomNavigationBar: TextButton(
onPressed: () => Navigator.maybePop(context),
child: Container(
height: 100.w,
alignment: Alignment.center,
color: Colors.blue,
child: Text(
'返回',
style: TextStyle(color: Colors.white, fontSize: 40.s),
),
),
),
);
}
}