首页 > 解决方案 > Flutter:使用开关小部件调整亮度

问题描述

预期行为:

问题:

代码:

我的主页.dart

import 'package:flutter/material.dart';
import 'package:switch_flutter_app/myswitch.dart';

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
  int _currentTabIndex = 0;
  
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Navigator(key: _navigatorKey, onGenerateRoute: generateRoute),
        bottomNavigationBar: _bottomNavigationBar(),
      ),
    );
  }

  Widget _bottomNavigationBar() {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          title: Text("Home"),
        ),
        BottomNavigationBarItem(
            icon: Icon(Icons.account_circle), title: Text("Account")),
        BottomNavigationBarItem(
          icon: Icon(Icons.settings),
          title: Text("Settings"),
        )
      ],
      onTap: _onTap,
      currentIndex: _currentTabIndex,
    );
  }

  _onTap(int tabIndex) {
    switch (tabIndex) {
      case 0:
        _navigatorKey.currentState.pushReplacementNamed("Home");
        break;
      case 1:
        _navigatorKey.currentState.pushReplacementNamed("Account");
        break;
      case 2:
        _navigatorKey.currentState.pushReplacementNamed("Settings");
        break;
    }
    setState(() {
      _currentTabIndex = tabIndex;
    });
  }

  Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case "Account":
        return MaterialPageRoute(
            builder: (context) =>
                Container(child: Center(child: Text("Account"))));
      case "Settings":
        return MaterialPageRoute(
            builder: (context) => Container(child: Center(child: MySwitch())));
      default:
        return MaterialPageRoute(
            builder: (context) =>
                Container(child: Center(child: Text("Home"))));
    }
  }
}

MySwitch.dart

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

class MySwitch extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<MySwitch> {
  bool isSwitched = false;
  double _brightness;

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Switch toggle'),
        ),
        body: Center(
          child: Switch(
            value: isSwitched,
            onChanged: (value) {
              setState(() {
                isSwitched = value;
                if (isSwitched) {
                  Wakelock.enable();
                  Screen.setBrightness(0.5);
                } else {
                  Screen.setBrightness(_brightness);
                  Wakelock.disable();
                }
              });
            },
            activeTrackColor: Colors.lightGreenAccent,
            activeColor: Colors.green,
          ),
        ));
  }
}

标签: flutterdart

解决方案


推荐阅读