Skip to main content

qr_screen_brightness 0.0.2 copy "qr_screen_brightness: ^0.0.2" to clipboard
qr_screen_brightness: ^0.0.2 copied to clipboard

Flutter plugin for controlling the current iOS screen or Android activity window brightness.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:qr_screen_brightness/qr_screen_brightness.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: BrightnessDemoPage());
  }
}

class BrightnessDemoPage extends StatefulWidget {
  const BrightnessDemoPage({super.key});

  @override
  State<BrightnessDemoPage> createState() => _BrightnessDemoPageState();
}

class _BrightnessDemoPageState extends State<BrightnessDemoPage> {
  final bool _isSupported = QrScreenBrightness.isSupported;
  double _sliderValue = 1.0;
  double? _currentBrightness;
  String _lastAction = 'ready';
  bool _isBusy = false;

  @override
  void initState() {
    super.initState();

    if (_isSupported) {
      unawaited(_getBrightness());
    } else {
      _lastAction = 'unsupported platform';
    }
  }

  Future<void> _runAction(String label, Future<void> Function() action) async {
    if (!_isSupported || _isBusy) {
      return;
    }

    setState(() {
      _isBusy = true;
    });

    try {
      await action();
      if (!mounted) {
        return;
      }

      setState(() {
        _lastAction = label;
      });
    } on Object catch (error) {
      if (!mounted) {
        return;
      }

      setState(() {
        _lastAction = 'error: $error';
      });
    } finally {
      if (mounted) {
        setState(() {
          _isBusy = false;
        });
      }
    }
  }

  Future<void> _getBrightness() {
    return _runAction('get brightness', () async {
      final value = await QrScreenBrightness.getBrightness();
      if (!mounted) {
        return;
      }

      setState(() {
        _currentBrightness = value;
        if (value != null) {
          _sliderValue = value;
        }
      });
    });
  }

  Future<void> _setBrightness() {
    final value = _sliderValue;

    return _runAction('set brightness ${_formatValue(value)}', () async {
      await QrScreenBrightness.setBrightness(value);
      if (!mounted) {
        return;
      }

      setState(() {
        _currentBrightness = value;
      });
    });
  }

  Future<void> _setMaxBrightness() {
    return _runAction('set max brightness', () async {
      await QrScreenBrightness.setMaxBrightness();
      if (!mounted) {
        return;
      }

      setState(() {
        _sliderValue = 1.0;
        _currentBrightness = 1.0;
      });
    });
  }

  Future<void> _resetBrightness() {
    return _runAction('reset brightness', () async {
      await QrScreenBrightness.resetBrightness();
      if (!mounted) {
        return;
      }

      setState(() {
        _currentBrightness = null;
      });
    });
  }

  String _formatBrightness(double? value) {
    if (value == null) {
      return 'system/default';
    }

    return _formatValue(value);
  }

  String _formatValue(double value) {
    return value.toStringAsFixed(2);
  }

  @override
  Widget build(BuildContext context) {
    final canUseControls = _isSupported && !_isBusy;

    return Scaffold(
      appBar: AppBar(title: const Text('QR screen brightness')),
      body: SafeArea(
        child: ListView(
          padding: const EdgeInsets.all(16),
          children: [
            Text('platform: ${_isSupported ? 'supported' : 'unsupported'}'),
            const SizedBox(height: 8),
            Text(
              'current brightness: ${_formatBrightness(_currentBrightness)}',
            ),
            const SizedBox(height: 8),
            Text('last action: $_lastAction'),
            const SizedBox(height: 24),
            Container(
              height: 160,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                border: Border.all(color: Colors.black26),
              ),
              child: const Text(
                'QR preview placeholder',
                style: TextStyle(fontWeight: FontWeight.w600),
              ),
            ),
            const SizedBox(height: 24),
            Slider(
              value: _sliderValue,
              min: 0,
              max: 1,
              divisions: 100,
              label: _formatValue(_sliderValue),
              onChanged: canUseControls
                  ? (value) {
                      setState(() {
                        _sliderValue = value;
                      });
                    }
                  : null,
            ),
            const SizedBox(height: 16),
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: [
                FilledButton.icon(
                  onPressed: canUseControls ? _getBrightness : null,
                  icon: const Icon(Icons.refresh),
                  label: const Text('Get'),
                ),
                FilledButton.icon(
                  onPressed: canUseControls ? _setBrightness : null,
                  icon: const Icon(Icons.tune),
                  label: const Text('Set'),
                ),
                FilledButton.icon(
                  onPressed: canUseControls ? _setMaxBrightness : null,
                  icon: const Icon(Icons.wb_sunny),
                  label: const Text('Max'),
                ),
                OutlinedButton.icon(
                  onPressed: canUseControls ? _resetBrightness : null,
                  icon: const Icon(Icons.restart_alt),
                  label: const Text('Reset'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
160
points
417
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter plugin for controlling the current iOS screen or Android activity window brightness.

Repository (GitHub)
View/report issues

Topics

#brightness #qr-code #screen

License

MIT (license)

Dependencies

flutter

More

Packages that depend on qr_screen_brightness

Packages that implement qr_screen_brightness