微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从列表中调用时,颤动小部件未更新

如何解决从列表中调用时,颤动小部件未更新

我之前问过一个关于小部件未在此处更新的问题: flutter slider not updating widget variables

我得到了一个很好的答案,它向我解释了有关状态如何工作的更多信息,我进一步进行了实验,现在遇到了一个问题,即使我在 setstate 中更新了状态,列表中的小部件也没有更新。

>

未更新的小部件是 testBoxList 列表中的 TestBoxNumber 小部件添加到列表后。我意识到如果我更改构建器以返回小部件本身而不是从列表中返回它可以工作,我不知道为什么会这样!

再次感谢任何帮助,我希望这也能帮助面临同样问题的人:)

主页代码

class TestPage extends StatefulWidget {
  static const id = "test_page";

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

class _TestPageState extends State<TestPage> {
  List testBoxList = [];
  List testSlideList = [];
  List testParamList = [];

  void updateFunc(ind,newVal) {
    setState(() {
      testParamList[ind] = newVal;
    });
  }

  void addSlider() {
    setState(() {
      double slideValue = 0;
      testParamList.add(slideValue);
      int BoxIndex = testParamList.length - 1;

      testBoxList.add(TestBoxNumber(
        numberdisplay: testParamList,BoxIndex: BoxIndex,));
      testSlideList.add(TestSlider(
        testValue: testParamList,updateFunc: updateFunc,));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onpressed: () {
          addSlider();
        },),body: Padding(
        padding: const EdgeInsets.all(30.0),child: ListView(
          children: [
            Text("Test Page"),// Builder for viewers
            ListView.builder(
              shrinkWrap: true,physics: ClampingScrollPhysics(),itemCount: testBoxList.length,itemBuilder: (BuildContext ctx,int index) {
                return testBoxList[index];
                // return Text(testParamList[index].toString());
                // return TestBoxNumber(
                //     numberdisplay: testParamList,BoxIndex: index);
              },// Builder for sliders
            ListView.builder(
              shrinkWrap: true,itemCount: testSlideList.length,int index) {
                return testSlideList[index];
              },],);
  }
}

TestBoxNumber 小部件

class TestBoxNumber extends StatelessWidget {
  final List numberdisplay;
  final int BoxIndex;

  TestBoxNumber({required this.numberdisplay,required this.BoxIndex});

  Widget build(BuildContext context) {
    return Text(this.numberdisplay[this.BoxIndex].toString());
  }
}

滑块小部件

class TestSlider extends StatefulWidget {
  List testValue;
  dynamic updateFunc;
  int BoxIndex;

  TestSlider({
    required this.testValue,required this.updateFunc,required this.BoxIndex,});

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

class _TestSliderState extends State<TestSlider> {
  // double curValue = widget.testValue;

  @override
  Widget build(BuildContext context) {
    double curValue = widget.testValue[widget.BoxIndex];

    return Slider(
      activeColor: themeData.primaryColorLight,value: curValue,min: 0,max: 100,divisions: 50,label: curValue.round().toString(),onChanged: (double value) {
        setState(() {
          curValue = value;
        });
        widget.updateFunc(widget.BoxIndex,value);
      },);
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。