I want to access a static variable in a stateful widget in a flutter.
but it does not work.
and someone said that is a private widget and I can't access it.
so how can I access the variable isCollapsed
in the below code:
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 var isCollapsed = false;
@override
Widget build(BuildContext context) {
Scaffold(
body: SlidingUpPanel(
controller: _pc,
panelBuilder: (sc) {
if (isCollapsed == false) _pc.hide();
if (isCollapsed == true) _pc.show();
return Container(
child: Center(child: Text("Panel")),
);
},
body: Text("something");
), }
}
I want to change isCollapsed in another class, when clicked on the icon, isCollapsed changes to true.
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: () {
},
icon: SvgPicture.asset(play),
),
);}}
can anyone help me please how can I do it?
CodePudding user response:
Yes, _BottomNavBarState
is private because it starts with underscore_
. if you want to make it public, remove _
from name and it will be like
@override
BottomNavBarState createState() => BottomNavBarState();
}
class BottomNavBarState extends State<BottomNavBar> {
static var isCollapsed = false;
and use anywhere like BottomNavBarState.isCollapsed
, but I should prefer state-management while the variable has effects on state.
For more about