flutter can't access static variable


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

  _BottomNavBarState createState() => _BottomNavBarState();

class _BottomNavBarState extends State<BottomNavBar> {
    static var isCollapsed = false;
  Widget build(BuildContext context) {
        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> {
  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?


Yes, _BottomNavBarState is private because it starts with underscore_. if you want to make it public, remove _ from name and it will be like

  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

Answered By – Yeasin Sheikh

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *