我对颤振不是很了解,但是必须做些什么... 我现在正在创建一个“首选项”菜单,其中一个菜单项应该变成灰色,只要未启用上述一项。我一直在努力寻找解决方案,因为这是基本行为,所以这似乎很奇怪。
一段时间后,我找到了尝试使用Listenables的方法。但是由于某种原因它不起作用,有人可以告诉我为什么吗?较低的条目仅保留初始值。
main.dart
var emailFormat=/^[a-zA-Z0-9_.+]+(?<!^[0-9]*)@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
sync.dart
class SettingList extends StatelessWidget {
final handler = new ValueNotifier(false);
Widget titleSync = Text(
'Entry 1'
);
Widget titleSet = Text(
'Entry 2'
);
Widget subTitleSet = Text(
'Entry 2 Subheading'
);
@override
Widget build(BuildContext context) {
return ListView(children: <Widget>[
SwitchOption(titleSync, handler),
ListOption(titleSet, subTitleSet, handler)
],
);
}
}
syncSettings.dart
import 'package:flutter/material.dart';
class _SwitchOptionState extends State<SwitchOption> {
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<bool>(
valueListenable: widget.handler,
builder: (context, value, child) {
return SwitchListTile(
value: widget._toggle,
title: widget.title,
onChanged: (bool value) {
setState(() {
if(value == true){
widget.handler.value = true;
}else{
widget.handler.value = false;
}
widget._toggle = value;
});
}
);
},
);
}
}
class SwitchOption extends StatefulWidget {
SwitchOption(this.title, this.handler);
ValueNotifier<bool> handler;
bool _toggle = false;
final Widget title;
@override
_SwitchOptionState createState() => _SwitchOptionState();
}
任何帮助表示赞赏!
答案 0 :(得分:0)
因此,我更改了代码,以使切换状态上移,并且不再使用ValueListener。我还重构了一些代码:
main.dart:
import 'package:flutter/material.dart';
import 'package:test_project/sync.dart';
import 'package:test_project/syncSettings.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SettingList(),
);
}
}
class SettingList extends StatefulWidget {
@override
_SettingListState createState() => _SettingListState();
}
class _SettingListState extends State<SettingList> {
bool _toggle = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
SwitchOption(
'Entry 1',
onToggle,
),
ListOption(
'Entry 2',
'Entry 2 Subheading',
_toggle,
),
],
),
);
}
void onToggle(bool value) {
print("Changed");
setState(() {
_toggle = value;
});
}
}
sync.dart
import 'package:flutter/material.dart';
class SwitchOption extends StatefulWidget {
SwitchOption(this._title, this._onChanged);
final String _title;
final Function _onChanged;
@override
_SwitchOptionState createState() => _SwitchOptionState();
}
class _SwitchOptionState extends State<SwitchOption> {
bool _localValue = true;
@override
Widget build(BuildContext context) {
return SwitchListTile(
value: _localValue,
title: Text(widget._title),
onChanged: (value) {
setState(() {
_localValue = value;
});
widget._onChanged(value);
});
}
}
syncSettings.dart:转换为无状态窗口小部件并从顶部窗口小部件传递状态
class ListOption extends StatelessWidget {
ListOption(this._title, this._subtitle, this._isEnabled);
final String _title;
final String _subtitle;
final bool _isEnabled;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(_title),
subtitle: Text(_subtitle),
enabled: _isEnabled,
);
}
}