꿈꾸는 시스템 디자이너

Flutter Example - Screen Management | Kept On Screen 본문

Tutorial/Flutter with App

Flutter Example - Screen Management | Kept On Screen

독행소년 2019. 9. 27. 12:40

1. Add this to pubspec.yaml file

dependencies:
  screen: ^0.0.5

 

2. Add the following permissions to Android Manifest file

<uses-permission android:name="android.permission.WAKE_LOCK" />

 

3. Source Code

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

ManageScreenDemoState pageState;

class ManageScreenDemo extends StatefulWidget {
  @override
  ManageScreenDemoState createState() {
    pageState = ManageScreenDemoState();
    return pageState;
  }
}

class ManageScreenDemoState extends State<ManageScreenDemo> {
  double _brightness;
  bool _enableKeptOn;

  @override
  void initState() {
    super.initState();
    getBrightness();
    getIsKeptOnScreen();
  }

  void getBrightness() async {
    double value = await Screen.brightness;
    setState(() {
      _brightness = double.parse(value.toStringAsFixed(1));
    });
  }

  void getIsKeptOnScreen() async {
    bool value = await Screen.isKeptOn;
    setState(() {
      _enableKeptOn = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Manage Screen Demo")),
      body: Column(
        children: <Widget>[
          // Notice
          Container(
            margin: const EdgeInsets.all(10),
            padding: const EdgeInsets.all(10),
            alignment: Alignment(0,0),
            height: 50,
            decoration: BoxDecoration(color: Colors.orange),
            child: Text(
              "Do this example on a real phone, not an emulator.",
              style: TextStyle(color: Colors.white),
            ),
          ),
          // Brightness Settings
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text("Brightness:"),
                (_brightness == null)
                    ? CircularProgressIndicator()
                    : Slider(
                        value: _brightness,
                        min: 0,
                        max: 1.0,
                        divisions: 10,
                        onChanged: (newValue) {
                          setState(() {
                            _brightness = newValue;
                          });
                          // set screen's brightness
                          Screen.setBrightness(_brightness);
                        },
                      ),
                Text(_brightness.toString()),
              ],
            ),
          ),
          // Kept-On Settings
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text("Kept on Screen:"),
                Text(_enableKeptOn.toString()),
                (_enableKeptOn == null)
                    ? CircularProgressIndicator()
                    : Switch(
                        value: _enableKeptOn,
                        onChanged: (flag) {
                          Screen.keepOn(flag);
                          getIsKeptOnScreen();
                        },
                      )
              ],
            ),
          )
        ],
      ),
    );
  }
}

 

 

▶ Go to Table of Contents | 강의 목차로 이동


※ This example is also available in the Flutter Code Examples app. | 본 예제는 Flutter Code Examples 앱에서도 제공됩니다.

 

Flutter Code Examples - Google Play 앱

Are you a beginner at Flutter? Check out the various features of Flutter through the demo. Source code for all demos is also provided.

play.google.com

Comments