【问题标题】:flutter can't change of value of variable颤振不能改变变量的值
【发布时间】:2021-12-04 23:38:43
【问题描述】:

我有这门课:

class BottomNavBar extends StatefulWidget {
  final int activeTabIndex;

  const BottomNavBar({Key? key, required this.activeTabIndex})
      : super(key: key);

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

class BottomNavBarState extends State<BottomNavBar> {
    static bool isCollapsed = false;
 @override
  Widget build(BuildContext context) {
 Scaffold(
        body: SlidingUpPanel(
          controller: _pc,
          panelBuilder: (sc) {
               setState(() {
          isCollapsed ? _pc.show(): _pc.hide();
        });
            return Container(
              child: Center(child: Text("Panel")),
            );
          },
          body: Text("something");
        ),  }
}

我想改变这个类中变量isCollapsed的值:

class _PlayerPreviewState extends State<PlayerPreview> {
  @override
  Widget build(BuildContext context) {
    final String play = 'assets/icons/play.svg';
    var pageWidth = MediaQuery.of(context).size.width;
    return  Padding(
        padding: EdgeInsets.only(top: pageWidth * .04),
        child: IconButton(
        onPressed: () {
        
             BottomNavBarState.isCollapsed = true;
                                   
         },
          icon: SvgPicture.asset(play),
                            ),
                          );}}

我想在点击 IconButton 时,isCollapsed 改为 true。

当我打印 BottomNavBarState.isCollapsed 时,打印 true 及其变化。 但在 BottomNavBarState 中不会改变。而这部分 -> if (isCollapsed == true) _pc.show(); 不起作用。

我知道我应该与报表管理器一起做,但我不知道他们。 所以有人可以帮帮我吗?如何更改 isCollaps 并运行 _pc.show()

【问题讨论】:

  • 为什么是isCollapsed static
  • 我自己也在学习 Flutter,我觉得你从 panelBuilder 闭包中调用 _pc 上的方法是不对的。相反,我希望您最好使用 _pc(不管它是什么)的工厂方法,如下所示:controller: isCollapsed ? PCThing.hidden() : PCThing.shown(),。这个想法是您为小部件的当前状态返回一个处于静态状态的对象。
  • 正如a comment to your other question 所解释的,您需要调用setState 以触发您的Widget 使用更新后的值进行重建。
  • 我使用静态是因为我想在另一个类中使用它。 @trojanfoe
  • 好的,我应该在代码中哪里使用setState? @jamesdlin

标签: flutter dart variables slideshow flutter-onpressed


【解决方案1】:

你不应该这样做:BottomNavBarState.isCollapsed = true; 因为您是从状态对象外部更改状态,因此您没有引用同一个实例。此外,您不应该将此变量设为静态。

解决方案:

就是在bottomBar的父类中创建一个函数来切换isCollapsed变量,例如:

父小部件

class ParentClass extends StatefulWidget {
  final int activeTabIndex;

  const ParentClass({Key? key, required this.activeTabIndex})
      : super(key: key);

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

class ParentClassState extends State<ParentClass> {
    
  bool isCollapsed = false;

  void setCollapsed(bool collapsed) {
    setState(() {
      isCollapsed = collapsed;
    });
  }

 @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Player(
          setCollapsed: setCollapsed,
        ),
        BottomBar(
          isCollapsed: isCollapsed,
        ),
      ]
    );
  }
}

底栏:

class BottomNavBar extends StatefulWidget {
  final int activeTabIndex;
  final bool isCollapsed;

  const BottomNavBar({Key? key, required this.activeTabIndex, required this.isCollapsed})
      : super(key: key);

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

class BottomNavBarState extends State<BottomNavBar> {
    

 @override
  Widget build(BuildContext context) {
 Scaffold(
        body: SlidingUpPanel(
          controller: _pc,
          panelBuilder: (sc) {
              widget.isCollapsed ? _pc.show(): _pc.hide();
            return Container(
              child: Center(child: Text("Panel")),
            );
          },
          body: Text("something");
        ),  }
}

玩家:

class _PlayerPreviewState extends State<PlayerPreview> {
  @override
  Widget build(BuildContext context) {
    final String play = 'assets/icons/play.svg';
    var pageWidth = MediaQuery.of(context).size.width;
    return  Padding(
        padding: EdgeInsets.only(top: pageWidth * .04),
        child: IconButton(
          onPressed: () {
            widget.setCollapsed(true);
          },
          icon: SvgPicture.asset(play),
        ),
    );
  }
}

【讨论】:

    【解决方案2】:

    您可以使用 setState() 封装代码,应用需要检查更改并重新构建。因此:

    setState() {
      BottomNavBarState.isCollapsed  = true;
    }
    

    我认为最好使用“bool”而不是“var”,因为变量“isCollapsed”的类型很明显。

    【讨论】:

    • 我已经做了但是不行:(
    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多