[英]Why Flutter statefull widget didn't change the state of another statefull widget when I call it inside its widget
I have created two Stateful Widget
classes name Editor{}
that contains the Employee card UI and the RPMSlider()
that controls the Radius of Items in a card.我创建了两个名为
Editor{}
的Stateful Widget
类,其中包含员工卡 UI 和控制卡中项目半径的RPMSlider()
。
I call the RPMSlider()
in Editor Widget
which looks like this in the below image,我在
Editor Widget
中调用RPMSlider()
,如下图所示,
My question is when I Adjust the slider it perfectly works and shows the value above it.我的问题是,当我调整 slider 时,它可以完美运行并显示高于它的值。
But it cannot make a change in the meantime in a Card UI.但它不能同时在卡片 UI 中进行更改。 Until I click a
button
or gesturedetector
having SetState();
直到我单击具有
SetState();
的button
或gesturedetector
检测器;
Slider function works fine when it is created inside
Editor()
but in a separate Stateful widget it cannot make changes in CardSlider function 在
Editor()
中创建时工作正常,但在单独的有状态小部件中它不能在 Card 中进行更改
This is an Editor这是一个编辑器
class Editor extends StatefulWidget {
@override
_EditorState createState() => _EditorState();
}
class _EditorState extends State<Editor> {
double size = 1;
var width, height;
@override
Widget build(BuildContext context) {
width = MediaQuery.of(context).size.width;
height = MediaQuery.of(context).size.height;
// print(width* 0.5733);
return SafeArea(
child: Container(
width: MediaQuery.of(context).size.width,
child: Row(
children: [
//TODO: Left Side
Container(
width: width * 0.300,
color: Color(0xffdbe5e7),
child: Column(
children: [
//Permanent Area
],
),
),
Container(
height: height,
padding: EdgeInsets.only(top: 10),
width: width * 0.400,
decoration: BoxDecoration(
color: Color(0xffdbe5e7),
),
//TODO: Card Area
child: FlipCard(
key: cardKey,
flipOnTouch: false,
///EDITABLE CARD FRONT
front: Container(),
///EDITABLE CARD Back
back: Container(),
),
),
Container(
width: width * 0.300,
color: Color(0xffdbe5e7),
child: Column(
children: [
//TODO: Radius , Padding , Margins
RPMSlider(),
],
),
),
],
),
),
);
}
}
This is RPMSlider这是 RPMSlider
class RPMSlider extends StatefulWidget {
@override
_RPMSliderState createState() => _RPMSliderState();
}
class _RPMSliderState extends State<RPMSlider> {
@override
Widget build(BuildContext context) {
return radius(caseId: widgetIdCarrier,);
}
radius({required String caseId,label='label',position='topLeft'}) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: Column(
children: [
Row(
children: [
Text(
label.toUpperCase(),
style: TextStyle(fontSize: 10, color: Colors.black54),
),
Spacer(),
Text(
radiusValue.round().toString(),
style: TextStyle(fontSize: 10, color: Colors.black54),
),
],
),
SizedBox(
height: 4,
),
NeumorphicSlider(
min: 0.0,
max: 30,
sliderHeight: 30,
thumb: NeumorphicIcon(
Icons.circle,
size: 20,
),
height: 1,
style: SliderStyle(
borderRadius: BorderRadius.circular(3),
lightSource: LightSource.bottomRight),
value:radiusValue
onChanged: (sts) {
setState(() {
radiusValue=sts;
});
},
),
],
),
);
}
}
You are right, the slider cannot change another widget.你是对的,slider 不能改变另一个小部件。 It can only change it self.
它只能自己改变。 It can also provide callbacks when it has changed.
它还可以在更改时提供回调。 Other widgets already do that, like the
NeumorphicSlider
that you use has an onChanged
callback that it calls when something has changed so you can make adjustments outside the widget.其他小部件已经这样做了,例如您使用的
NeumorphicSlider
有一个onChanged
回调,它会在某些内容发生更改时调用它,以便您可以在小部件之外进行调整。
So give your widget an onChanged
callback, too:所以也给你的小部件一个
onChanged
回调:
class RPMSlider extends StatefulWidget {
final ValueChanged<int>? onChanged;
RPMSlider({this.onChanged});
Now in your state class, whenever you get notified that the value has changed, you notify others that is has changed:现在在您的 state class 中,每当您收到值已更改的通知时,您都会通知其他人已更改:
onChanged: (sts) {
setState(() {
radiusValue = sts;
});
// the new part:
final callBack = widget.onChanged
if(callBack != null) {
callBack(sts);
}
Now in your EditorState, you can use it like this:现在在您的 EditorState 中,您可以像这样使用它:
RPMSlider(onChanged: (value) {
setState(() {
your_state_variable = value;
// don't know what your_state_variable is,
// you need to pick the one you need for this
});
})
So now, the setState
method is called inside the correct state class, that can actually change both widgets, the slider and the other one.所以现在,在正确的 state class 中调用了
setState
方法,这实际上可以更改两个小部件,即 slider 和另一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.