flutter - 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,
),
));
}
}
解决方案
推荐阅读
- php - Laravel 路由 URL 问题
- go - Antlr4 Listener 子树检查条件
- java - 启动新活动时Android Studio打开应用程序
- javascript - How does onchange and onkeyup work in javascript?
- wireshark - 如何过滤捕获的包?
- sql - oracle中的自定义排序
- apache-spark - 拆分数据数据类型后的Spark RDD如何在不更改数据类型的情况下拆分
- python - 如何从python中的多个文件中删除扩展名
- r - 在一个级别中置换一列,对 2 列执行测试,并保存 pvalues
- javascript - 如何在自定义元素的插槽内设置 CSS 样式?