flutter - 颤振不能改变变量的值
问题描述
我有这门课:
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()
解决方案
您不应该这样做: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),
),
);
}
}
推荐阅读
- laravel - Laravel 7-Carbon 以 UTC 的形式返回
- r - 如何提取每个日期时间戳的频率
- sql - 使用在另一个查询中返回多个的查询
- python - 库 Keras 和 Tensorflow 的退出代码 132
- android - Android Kotlin Retrofit,等待外挂响应
- c# - OnCollisionExit2D 在退出碰撞时未触发
- javascript - 我怎样才能将这个 Datatables 函数返回到 globale 函数中
- android - 如何使用 Mockito 模拟 Hawk 库?
- javascript - CSS 颜色过渡在 Chrome/Edge 83 中无法按预期工作
- node.js - SMPP 服务器 - 如何获取客户端(ESME)的 IP 地址?