【问题标题】:Flutter : setState() is not working properly颤振:setState() 无法正常工作
【发布时间】:2019-08-29 18:05:26
【问题描述】:

我正在制作一个新的有状态小部件,它将根据所选选项显示一个列表视图,此处为 ONE 和 TWO。一旦点击 GestureDetector,index 的值就会改变,文本的字体大小和颜色也会改变。但是,pages[index] 的容器不会重建

我不知道出了什么问题,因为列中的一个容器会重建,而另一个没有。

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MatchStatsState();
  }
}

class MatchStatsState extends State<MatchStats>{
  List<Widget> pages = [
    ListView(
      scrollDirection: Axis.horizontal,
      children: <Widget>[
        BattingStatsView(CskBatting),
        BowlingStatsView(cskBowling),
      ],
    ),
    ListView(
      scrollDirection: Axis.horizontal,
      children: <Widget>[
        BattingStatsView(kxipBatting),
        BowlingStatsView(kxipBowling)
      ],
    ),   
  ];
  Color activeColor = Colors.yellow;
  Color inactiveColor = Colors.white;
  num activeFontSize = 20.0;
  num inactiveFontSize = 15.0;
  int index = 0;

  @override
  Widget build(BuildContext context) {


    // TODO: implement build
    return Container(
      height: MediaQuery.of(context).size.height*0.4,
      width: MediaQuery.of(context).size.width*0.95,
      child: Column(
        children: <Widget>[
          Container(
            height: MediaQuery.of(context).size.height*0.05,
            width: MediaQuery.of(context).size.width*0.95,
            child: Row(
              children: <Widget>[
                GestureDetector(
                    onTap: (){
                      setState(() {
                        index = 0;
                      });
                    },
                    child: Container(
                    width: MediaQuery.of(context).size.width*0.45,
                    child: Text("ONE",style: TextStyle(color: index == 0?activeColor:inactiveColor,fontSize: index == 0? activeFontSize: inactiveFontSize)),
                  ),
                ),
                GestureDetector(
                    onTap: (){
                      setState(() {
                        index = 1;
                      });
                    },
                    child: Container(
                    width: MediaQuery.of(context).size.width*0.45,
                    child: Text("TWO",style: TextStyle(color: index == 1?activeColor:inactiveColor, fontSize: index == 1? activeFontSize: inactiveFontSize)),
                  ),
                ),
              ],
            ),
          ),
          Container(
            height: MediaQuery.of(context).size.height*0.35,
            width: MediaQuery.of(context).size.width*0.95,
            child: pages[index]
          ),
        ]
      )
    );
  }
}

我希望列中的第二个容器在索引值更改时重建,我该如何实现?

【问题讨论】:

  • 你在State类中添加了initState()吗?

标签: dart flutter


【解决方案1】:

我认为这个问题的原因是元素树无法识别在小部件树中所做的更改,因此您可以将 Key 添加到包含 pages[index] 的容器中或 你可以这样做:

    Widget getWidget(int index){
  return Container(
    child:pages[index],
  );
}

不要在小部件树中使用 Container,而是使用每次 ui 重新呈现时都会调用的函数。 希望能帮到你

【讨论】:

    【解决方案2】:

    试试这个:

    创建一个返回 List Widget 的方法,如下所示:

    List<Widget> buildPages() {
        return [
          ListView(
            scrollDirection: Axis.horizontal,
            children: <Widget>[
              BattingStatsView(CskBatting),
              BowlingStatsView(cskBowling),
            ],
          ),
          ListView(
            scrollDirection: Axis.horizontal,
            children: <Widget>[
              BattingStatsView(kxipBatting),
              BowlingStatsView(kxipBowling)
            ],
          ),
        ];
      }
    
    Widget getProperWidget(int index) {
        return buildPages()[index];
      }
    

    比你的列容器:

    Container(
                height: MediaQuery.of(context).size.height*0.35,
                width: MediaQuery.of(context).size.width*0.95,
                child: getproperWidget(index)
              ),
    

    记得覆盖 initState。

    【讨论】:

    • 我用这段代码试过了,我对 initState 进行了覆盖,我在 initState 中初始化了索引,但不幸的是我得到了相同的结果。
    • 我已经更新了代码,试试看。您以某种方式强制重建小部件,只有这样容器才能重建自身。似乎在您的代码中小部件没有重建
    • 没错!我怎么能强制重建? getProperWidget() 给出与以前相同的结果
    • 您尝试强制执行的是刷新列表。尝试返回初始代码,将列表保存为属性并在 setState() { pages = List.from(pages) }
    猜你喜欢
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 2020-07-13
    • 2022-01-18
    • 2021-10-01
    • 2021-12-27
    • 2021-03-29
    • 2018-05-12
    相关资源
    最近更新 更多