How To Create Screen Brightness Using Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 28-10-2020

Screen brightness plugin widget .The overall theme brightness.The default TextStyle color for the textTheme is black if the theme is constructed with Brightness.light and white if the theme is constructed with Brightness.dark.

How To Create Screen Brightness Using Flutter Android App

 Screen Brightness
Step 1:  We cannot directly remove the time stamp from   Screen Brightness but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code

dependencies:
  flutter:
    sdk: flutter
  screen: ^0.0.5

Step 2: After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command. 

flutter pub get

Step 3: Open your project’s main.dart file and import material.dart and screen: ^0.0.5 dart package.
import 'package:screen/screen.dart';

Complete Code For Screen Brightness In Flutter
Main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:screen/screen.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  @override
  bool _isKeptOn = false;
  double _brightness = 1.0;

  @override
  initState() {
    super.initState();
    initPlatformState();
  }

  initPlatformState() async {
    bool keptOn = await Screen.isKeptOn;
    double brightness = await Screen.brightness;
    setState((){
      _isKeptOn = keptOn;
      _brightness = brightness;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          backgroundColor: Colors.lightGreen,
            title: new Text('Screen Brightness'
            )),
        body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text("Brightness :",style: TextStyle(fontSize:15, fontWeight: FontWeight.bold),),
                  new Slider(value : _brightness,
                      activeColor: Colors.lightGreen,
                      onChanged : (double b){
                    setState((){_brightness = b;});
                    Screen.setBrightness(b);
                  })
                ]
            )
        ),
    );
  }
}

 

Related Post