flutter_refresh_rate_control 0.1.0+0
flutter_refresh_rate_control: ^0.1.0+0 copied to clipboard
A Flutter plugin that requests high refresh rate mode on Android, iOS and macOS devices.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_refresh_rate_control/flutter_refresh_rate_control.dart';
import 'fps_counter.dart';
void main() {
runApp(const FlutterRefreshRateControlExampleApp());
}
class FlutterRefreshRateControlExampleApp extends StatefulWidget {
const FlutterRefreshRateControlExampleApp({super.key});
@override
State<FlutterRefreshRateControlExampleApp> createState() =>
_FlutterRefreshRateControlExampleAppState();
}
class _FlutterRefreshRateControlExampleAppState
extends State<FlutterRefreshRateControlExampleApp> {
String _platformVersion = 'Unknown';
Map<String, dynamic> _refreshRateInfo = {};
bool _isHighRefreshRate = false;
bool _exceptionMode = false;
// Duration.zero updates the FPS counter on every frame
Duration _fpsUpdateInterval = const Duration(seconds: 1);
// Without an animation only the updates of the FPS counter are rendered
bool _showAnimation = true;
final _flutterRefreshRateControlPlugin = FlutterRefreshRateControl();
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
String platformVersion;
Map<String, dynamic> refreshRateInfo = {};
try {
platformVersion =
await _flutterRefreshRateControlPlugin.getPlatformVersion() ??
'Unknown platform version';
refreshRateInfo =
await _flutterRefreshRateControlPlugin.getRefreshRateInfo();
} on PlatformException catch (e) {
platformVersion = 'Failed to get platform version: ${e.message}';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
_refreshRateInfo = refreshRateInfo;
});
}
Future<void> _toggleHighRefreshRate() async {
try {
bool success;
if (_isHighRefreshRate) {
success = await _flutterRefreshRateControlPlugin.stopHighRefreshRate();
} else {
success =
await _flutterRefreshRateControlPlugin.requestHighRefreshRate();
}
if (success) {
setState(() {
_isHighRefreshRate = !_isHighRefreshRate;
});
// Refresh the info after changing mode
await _updateRefreshRateInfo();
}
} on PlatformException catch (e) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error: ${e.message}')));
}
}
}
Future<void> _updateRefreshRateInfo() async {
try {
final info = await _flutterRefreshRateControlPlugin.getRefreshRateInfo();
setState(() {
_refreshRateInfo = info;
});
} on PlatformException catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error getting refresh rate info: ${e.message}'),
),
);
}
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Refresh Rate Control')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Platform: $_platformVersion'),
const SizedBox(height: 16),
Text(
'High Refresh Rate: ${_isHighRefreshRate ? "Enabled" : "Disabled"}',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: _isHighRefreshRate ? Colors.green : Colors.red,
),
),
const SizedBox(height: 16),
// The rate Flutter really renders at, whatever was requested
Row(
children: [
if (_showAnimation) ...[
const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 3),
),
const SizedBox(width: 12),
],
FpsCounter(
updateInterval:
_fpsUpdateInterval == Duration.zero
? null
: _fpsUpdateInterval,
style: const TextStyle(
fontSize: 18,
fontFamily: 'monospace',
),
),
],
),
Row(
children: [
const Text('Update FPS: '),
DropdownButton<Duration>(
value: _fpsUpdateInterval,
onChanged: (value) {
setState(() {
_fpsUpdateInterval = value ?? Duration.zero;
});
},
items: const [
DropdownMenuItem(
value: Duration.zero,
child: Text('Every frame'),
),
DropdownMenuItem(
value: Duration(milliseconds: 250),
child: Text('4 times a second'),
),
DropdownMenuItem(
value: Duration(seconds: 1),
child: Text('Once a second'),
),
DropdownMenuItem(
value: Duration(seconds: 5),
child: Text('Every 5 seconds'),
),
],
),
],
),
Row(
children: [
Checkbox(
value: _showAnimation,
onChanged: (value) {
setState(() {
_showAnimation = value ?? false;
});
},
),
const Expanded(
child: Text('Show an animation, so there are frames'),
),
],
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _toggleHighRefreshRate,
child: Text(
_isHighRefreshRate
? 'Stop High Refresh Rate'
: 'Enable High Refresh Rate',
),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _updateRefreshRateInfo,
child: const Text('Refresh Info'),
),
const SizedBox(height: 16),
Row(
children: [
Checkbox(
value: _exceptionMode,
onChanged: (value) {
setState(() {
_exceptionMode = value ?? false;
_flutterRefreshRateControlPlugin
.exceptionOnUnsupportedPlatform = _exceptionMode;
});
},
),
const Expanded(
child: Text('Enable exceptions on unsupported platforms'),
),
],
),
const SizedBox(height: 16),
const Text(
'Refresh Rate Info:',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Expanded(
child: SingleChildScrollView(
child: Text(
_refreshRateInfo.isNotEmpty
? _refreshRateInfo.entries
.map((e) => '${e.key}: ${e.value}')
.join('\n')
: 'No refresh rate info available',
style: const TextStyle(fontFamily: 'monospace'),
),
),
),
],
),
),
),
);
}
}