Flutter 来自相关小部件类的颤振更改状态
让我们假设一个类“SpecialButton”及其状态类“SpecialButtonState”Flutter 来自相关小部件类的颤振更改状态,flutter,flutter-state,Flutter,Flutter State,让我们假设一个类“SpecialButton”及其状态类“SpecialButtonState” class SpecialButton扩展StatefulWidget{ bool active=false; 特殊按钮({Key}):超级(Key:Key); @凌驾 SpecialButtonState createState()=>SpecialButtonState(); } 类SpecialButtonState扩展状态{ @凌驾 void initState(){ super.initS
class SpecialButton扩展StatefulWidget{
bool active=false;
特殊按钮({Key}):超级(Key:Key);
@凌驾
SpecialButtonState createState()=>SpecialButtonState();
}
类SpecialButtonState扩展状态{
@凌驾
void initState(){
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回容器(
装饰:
盒子装饰(颜色:this.widget.active?颜色1:颜色2),
子项:空);
}
}
在父窗口小部件中,我管理两个按钮。因此,我想给他们分配一个状态。我尝试的解决方案是在SpecialButton类中引入一个标记“active”,我可以从父窗口小部件轻松地将其设置为true或false。然后我可以在state类的build函数中使用它来为按钮着色。不幸的是,这并不完全有效,因为它不会立即更新按钮(它需要某种状态更新,例如通过将鼠标悬停在元素上)
我的第二个想法是引入这个标志作为SpecialButtonState类的支撑状态
class SpecialButton extends StatefulWidget {
SpecialButton({Key key}) : super(key: key);
@override
SpecialButtonState createState() => SpecialButtonState();
}
class SpecialButtonState extends State<SpecialButton> {
bool active;
@override
void initState() {
super.initState();
this.active = false;
}
activate() {
this.setState(() {
active = true;
});
}
deactivate() {
this.setState(() {
active = false;
});
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: this.active ? COLOR_1 : COLOR_2),
child: null);
}
}
class SpecialButton扩展StatefulWidget{
特殊按钮({Key}):超级(Key:Key);
@凌驾
SpecialButtonState createState()=>SpecialButtonState();
}
类SpecialButtonState扩展状态{
布尔活跃;
@凌驾
void initState(){
super.initState();
this.active=false;
}
激活(){
此.setState(){
主动=真;
});
}
停用(){
此.setState(){
主动=假;
});
}
@凌驾
小部件构建(构建上下文){
返回容器(
装饰:框装饰(颜色:this.active?颜色1:颜色2),
子项:空);
}
}
据我所知,这将是处理颤振的正确方法,但似乎我无法从SpecialButton类或包含小部件的父类访问函数“激活”或“停用”
所以我的问题是:我如何(直接或间接地通过函数)从相应的StatefulWidget类或包含它的父窗口小部件修改状态
在Stack Overflow上已经有一些类似的问题,我可以在这里找到一些提示,对于我发现有误导性的行为,使用或不使用全局键。另外,由于颤振的快速发展,它们可能已经过时了,所以我再次就这个确切的用例提出这个(类似的)问题
编辑:我忘了提到,这是至关重要的,这个标志将在创建后更改,因此它将在其活动时间内更改多次。这需要重新绘制小部件。没有必要为
特殊按钮使用有状态小部件。您可以使用无状态小部件和键处理active
标志。示例代码:
class SomeParent extends StatefulWidget {
const SomeParent({Key key}) : super(key: key);
@override
State<SomeParent> createState() => SomeParentState();
}
class SomeParentState extends State<SomeParent> {
bool _button1IsActive = false;
bool _button2IsActive = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
SpecialButton(
key: UniqueKey(),
active: _button1IsActive,
),
SizedBox(height: 8),
SpecialButton(
key: UniqueKey(),
active: _button2IsActive,
),
SizedBox(height: 16),
TextButton(
child: Text('Toggle button 1'),
onPressed: () {
setState(() {
_button1IsActive = !_button1IsActive;
});
},
),
SizedBox(height: 8),
TextButton(
child: Text('Toggle button 2'),
onPressed: () {
setState(() {
_button2IsActive = !_button2IsActive;
});
},
),
],
),
);
}
}
class SpecialButton extends StatelessWidget {
final bool active;
const SpecialButton({Key key, this.active = false}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
height: 40,
width: 40,
decoration: BoxDecoration(color: active ? Colors.red : Colors.blue),
);
}
}
类SomeParent扩展StatefulWidget{
const SomeParent({Key}):super(Key:Key);
@凌驾
State createState()=>SomeParentState();
}
类SomeParentState扩展状态{
bool _按钮1isactive=false;
bool_按钮2isactive=false;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
儿童:[
特殊按钮(
键:UniqueKey(),
活动:\ u按钮1处于活动状态,
),
尺寸箱(高度:8),
特殊按钮(
键:UniqueKey(),
活动:_按钮2无效,
),
尺寸箱(高度:16),
文本按钮(
子项:文本(“切换按钮1”),
已按下:(){
设置状态(){
_按钮1激活=!\u按钮1激活;
});
},
),
尺寸箱(高度:8),
文本按钮(
子项:文本(“切换按钮2”),
已按下:(){
设置状态(){
_button2IsActive=!\u button2IsActive;
});
},
),
],
),
);
}
}
类SpecialButton扩展了无状态小部件{
最终bool激活;
const SpecialButton({Key-Key,this.active=false}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回容器(
身高:40,
宽度:40,
装饰:框装饰(颜色:活动?颜色。红色:颜色。蓝色),
);
}
}
SomeParent
就是我的幻想,举个例子。不知道你的父母是什么。
钥匙在这里很重要。它们告诉窗口小部件树何时应该重建具有相同类型的特定窗口小部件(例如SpecialButton
)
请尝试这种方法,它应该会起作用。没有必要为特殊按钮使用有状态小部件。您可以使用无状态小部件和键处理active
标志。示例代码:
class SomeParent extends StatefulWidget {
const SomeParent({Key key}) : super(key: key);
@override
State<SomeParent> createState() => SomeParentState();
}
class SomeParentState extends State<SomeParent> {
bool _button1IsActive = false;
bool _button2IsActive = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
SpecialButton(
key: UniqueKey(),
active: _button1IsActive,
),
SizedBox(height: 8),
SpecialButton(
key: UniqueKey(),
active: _button2IsActive,
),
SizedBox(height: 16),
TextButton(
child: Text('Toggle button 1'),
onPressed: () {
setState(() {
_button1IsActive = !_button1IsActive;
});
},
),
SizedBox(height: 8),
TextButton(
child: Text('Toggle button 2'),
onPressed: () {
setState(() {
_button2IsActive = !_button2IsActive;
});
},
),
],
),
);
}
}
class SpecialButton extends StatelessWidget {
final bool active;
const SpecialButton({Key key, this.active = false}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
height: 40,
width: 40,
decoration: BoxDecoration(color: active ? Colors.red : Colors.blue),
);
}
}
类SomeParent扩展StatefulWidget{
const SomeParent({Key}):super(Key:Key);
@凌驾
State createState()=>SomeParentState();
}
类SomeParentState扩展状态{
bool _按钮1isactive=false;
bool_按钮2isactive=false;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
儿童:[
特殊按钮(
键:UniqueKey(),
活动:\ u按钮1处于活动状态,
),
尺寸箱(高度:8),
特殊按钮(
键:UniqueKey(),
活动:_按钮2无效,
),
尺寸箱(高度:16),
文本按钮(
子项:文本(“切换按钮1”),
已按下:(){
设置状态(){
_按钮1激活=!\u按钮1激活;
});
},
),
尺寸箱(高度:8),
文本按钮(
子项:文本(“切换按钮2”),
已按下:(){
塞斯特
import 'package:flutter/material.dart';
class Parent extends StatefulWidget {
@override
_ParentState createState() => _ParentState();
}
class _ParentState extends State<Parent> {
bool isEnabled = false;
@override
Widget build(BuildContext context) {
return Column(
children: [
StateLessButton1(isEnabled: isEnabled),
StateLessButton1(isEnabled: !isEnabled),
FloatingActionButton(onPressed: (){
setState(() {
isEnabled = !isEnabled;
});
})
],
);
}
}