首页 > 解决方案 > 颤振不能改变变量的值

问题描述

我有这门课:

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。

当我 print BottomNavBarState.isCollapsed, print true 和它的变化。但在 BottomNavBarState 中不会改变。而这部分->if (isCollapsed == true) _pc.show();不起作用。

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

标签: flutterdartvariablesslideshowflutter-onpressed

解决方案


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

解决方案:

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

父小部件

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),
        ),
    );
  }
}

推荐阅读