-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.dart
189 lines (176 loc) · 6.32 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
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_displaymode/flutter_displaymode.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<DisplayMode> modes = <DisplayMode>[];
DisplayMode? active;
DisplayMode? preferred;
final ValueNotifier<int> page = ValueNotifier<int>(0);
late final PageController controller = PageController()
..addListener(() {
page.value = controller.page!.round();
});
@override
void initState() {
super.initState();
SchedulerBinding.instance.addPostFrameCallback((_) {
fetchAll();
});
}
Future<void> fetchAll() async {
try {
modes = await FlutterDisplayMode.supported;
modes.forEach(print);
/// On OnePlus 7 Pro:
/// #1 1080x2340 @ 60Hz
/// #2 1080x2340 @ 90Hz
/// #3 1440x3120 @ 90Hz
/// #4 1440x3120 @ 60Hz
/// On OnePlus 8 Pro:
/// #1 1080x2376 @ 60Hz
/// #2 1440x3168 @ 120Hz
/// #3 1440x3168 @ 60Hz
/// #4 1080x2376 @ 120Hz
} on PlatformException catch (e) {
print(e);
/// e.code =>
/// noAPI - No API support. Only Marshmallow and above.
/// noActivity - Activity is not available. Probably app is in background
}
preferred = await FlutterDisplayMode.preferred;
active = await FlutterDisplayMode.active;
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: Padding(
padding: const EdgeInsets.only(top: 16, left: 16, right: 16),
child: PageView(
controller: controller,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Text(
'Available modes',
style: Theme.of(context).textTheme.headlineSmall,
),
TextButton.icon(
icon: const Icon(Icons.refresh),
onPressed: () {
fetchAll();
},
label: const Text('Fetch'),
),
],
),
if (modes.isEmpty) const Text('Nothing here'),
Expanded(
child: ListView.builder(
itemCount: modes.length,
itemBuilder: (_, int i) {
final DisplayMode mode = modes[i];
return Row(
children: <Widget>[
Radio<DisplayMode>(
value: mode,
groupValue: preferred,
onChanged: (DisplayMode? newMode) async {
await FlutterDisplayMode.setPreferredMode(
newMode!);
await Future<dynamic>.delayed(
const Duration(milliseconds: 100),
);
await fetchAll();
setState(() {});
},
),
if (mode == DisplayMode.auto)
const Text('Automatic')
else
Text(mode.toString()),
if (mode == active) const Text(' [ACTIVE]'),
],
);
},
),
),
if (modes.isNotEmpty) const Divider(),
if (modes.isNotEmpty)
Row(
children: <Widget>[
ElevatedButton(
onPressed: () async {
await FlutterDisplayMode.setHighRefreshRate();
await Future<dynamic>.delayed(
const Duration(milliseconds: 100),
);
await fetchAll();
setState(() {});
},
child: const Text('Highest Hz'),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: () async {
await FlutterDisplayMode.setLowRefreshRate();
await Future<dynamic>.delayed(
const Duration(milliseconds: 100),
);
await fetchAll();
setState(() {});
},
child: const Text('Lowest Hz'),
),
],
),
const Divider(),
],
),
ListView.builder(
itemBuilder: (BuildContext _, int i) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Index: $i'),
);
},
),
],
),
),
bottomNavigationBar: ValueListenableBuilder<int>(
valueListenable: page,
builder: (_, int page, __) => BottomNavigationBar(
currentIndex: page,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.android),
label: 'Modes',
),
BottomNavigationBarItem(
icon: Icon(Icons.list),
label: 'Lists',
),
],
onTap: (int i) => controller.animateToPage(
i,
duration: kTabScrollDuration,
curve: Curves.easeOutQuart,
),
),
),
),
);
}
}