skip to Main Content

I wanted to make an app to regulate the phone brightness (android) but cannot find anything.

The only thing that I found is the screen plugin but visual studio code doesn’t accept it.

Do you guy maybe have a practical example that I can use?

2

Answers


  1. There is no plugin which IDE doesn’t accept. It seems your flutter sdk or some of the packages you are using is not compatible with that plugin’s latest version. Or maybe plugin is too old.
    Try this one: https://pub.dev/packages/screen_brightness Instead of adding pubspec.yaml by hand, it is preferred adding dependencies with flutter pub add [dependency_name] . Because it automatically adds compatible version with your flutter sdk and dependencies you are already using.

    Login or Signup to reply.
  2. Try use screen_brightness, like this:

    class MyHomePage extends StatefulWidget {
      const MyHomePage({Key? key, required this.title}) : super(key: key);
    
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.red,
          body: Center(
            child: FutureBuilder<double>(
              future: ScreenBrightness().current,
              builder: (context, snapshot) {
                double currentBrightness = 0;
                if (snapshot.hasData) {
                  currentBrightness = snapshot.data!;
                }
    
                return StreamBuilder<double>(
                  stream: ScreenBrightness().onCurrentBrightnessChanged,
                  builder: (context, snapshot) {
                    double changedBrightness = currentBrightness;
                    if (snapshot.hasData) {
                      changedBrightness = snapshot.data!;
                    }
    
                    return Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        FutureBuilder<bool>(
                          future: ScreenBrightness().hasChanged,
                          builder: (context, snapshot) {
                            return Text(
                                'Brightness has changed via plugin: ${snapshot.data}');
                          },
                        ),
                        Text('Current brightness: $changedBrightness'),
                        Slider.adaptive(
                          value: changedBrightness,
                          onChanged: (value) {
                            setBrightness(value);
                          },
                        ),
                      ],
                    );
                  },
                );
              },
            ),
          ),
        );
      }
    
      Future<void> setBrightness(double brightness) async {
        try {
          await ScreenBrightness().setScreenBrightness(brightness);
        } catch (e) {
          debugPrint(e.toString());
          throw 'Failed to set brightness';
        }
      }
    
      Future<double> get currentBrightness async {
        try {
          return await ScreenBrightness().current;
        } catch (e) {
          print(e);
          throw 'Failed to get current brightness';
        }
      }
    }
    

    Remember that this package won’t work on emulator.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search