home.dart
4.83 KB
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
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class HomePageScaffold extends StatelessWidget {
const HomePageScaffold({Key? key, required this.title}) : super(key: key);
void printScreenInformation() {
print('Device Size:${Size(1.sw, 1.sh)}');
print('Device pixel density:${ScreenUtil().pixelRatio}');
print('Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}dp');
print('Status bar height dp:${ScreenUtil().statusBarHeight}dp');
print('The ratio of actual width to UI design:${ScreenUtil().scaleWidth}');
print(
'The ratio of actual height to UI design:${ScreenUtil().scaleHeight}');
print('System font scaling:${ScreenUtil().textScaleFactor}');
print('0.5 times the screen width:${0.5.sw}dp');
print('0.5 times the screen height:${0.5.sh}dp');
print('Screen orientation:${ScreenUtil().orientation}');
}
@override
Widget build(BuildContext context) {
printScreenInformation();
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
// Using Extensions
Container(
padding: EdgeInsets.all(10.w),
width: 0.5.sw,
height: 200.h,
color: Colors.red,
child: Text(
'My actual width: ${0.5.sw}dp \n\n'
'My actual height: ${200.h}dp',
style: TextStyle(
color: Colors.white,
fontSize: 12.sp,
),
),
),
// Without using Extensions
Container(
padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
width: ScreenUtil().setWidth(180),
height: ScreenUtil().setHeight(200),
color: Colors.blue,
child: Text(
'My design draft width: 180dp\n\n'
'My design draft height: 200dp',
style: TextStyle(
color: Colors.white,
fontSize: ScreenUtil().setSp(12),
),
),
),
],
),
Container(
padding: EdgeInsets.all(10.w),
width: 100.r,
height: 100.r,
color: Colors.green,
child: Text(
'I am a square with a side length of 100',
style: TextStyle(
color: Colors.white,
fontSize: 12.sp,
),
),
),
Text('Device width:${ScreenUtil().screenWidth}dp'),
Text('Device height:${ScreenUtil().screenHeight}dp'),
Text('Device pixel density:${ScreenUtil().pixelRatio}'),
Text('Bottom safe zone distance:${ScreenUtil().bottomBarHeight}dp'),
Text('Status bar height:${ScreenUtil().statusBarHeight}dp'),
Text(
'The ratio of actual width to UI design:${ScreenUtil().scaleWidth}',
textAlign: TextAlign.center,
),
Text(
'The ratio of actual height to UI design:${ScreenUtil().scaleHeight}',
textAlign: TextAlign.center,
),
SizedBox(
height: 10.h,
),
Text('System font scaling factor:${ScreenUtil().textScaleFactor}'),
SizedBox(height: 5),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'16sp, will not change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: 16.sp,
),
textScaleFactor: 1.0,
),
Text(
'16sp,if data is not set in MediaQuery,my font size will change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: 16.sp,
),
),
],
)
],
),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
SystemChrome.setPreferredOrientations([
MediaQuery.of(context).orientation == Orientation.portrait
? DeviceOrientation.landscapeRight
: DeviceOrientation.portraitUp,
]);
// setState(() {});
},
label: const Text('Rotate'),
),
);
}
final String title;
}