Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why is (_selected) in Widged _button undfined

in the Widget _button it says:

Undefined name ‘_selected’.
Try correcting the name to one that is defined, or defining the name.dartundefined_identifier

But I defined at in Widget section please help
or say me what do i wrong
it is the same with _setState

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class NavBar extends StatefulWidget {
  const NavBar({Key? key}) : super(key: key);

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

class _NavBarState extends State<NavBar> {
  final _palette = AppTheme.palette;
  int _selected = 0;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(8),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(15),
        color: _palette.primaryColor,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _button(
            index: 0,
            icon: Icons.home,
            selectedIndex: _selected,
          ),
          _button(
            index: 1,
            icon: Icons.favorite_border_outlined,
            selectedIndex: _selected,
          ),
     
        ],
      ),
    );
  }
}


on the same page is the Widget _button

Widget _button({
  required int index,
  required IconData icon,
  VoidCallback? onPressed,
  int selectedIndex: 0,
}) {
  bool isSelected = selectedIndex == index;
  return Material(
    color: isSelected ? AppTheme.palette.buttonOverlay : Colors.transparent,
    borderRadius: BorderRadius.circular(13),
    clipBehavior: Clip.antiAlias,
    child: IconButton(
      visualDensity: VisualDensity.compact,
      icon: Icon(
        icon,
        color: isSelected
            ? AppTheme.palette.secondaryColor
            : AppTheme.palette.buttonOverlay,
      ),
      onPressed: () {
        _selected = index;
        onPressed?.call();
        setState(() {});
      },
    ),
  );
}

>Solution :

…on the same page is the Widget _button

You need to make sure that _button is within your NavBar class. It should look like this:

class NavBar extends StatefulWidget {
  const NavBar({Key? key}) : super(key: key);

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

class _NavBarState extends State<NavBar> {
  final _palette = AppTheme.palette;
  int _selected = 0;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(8),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(15),
        color: _palette.primaryColor,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _button(
            index: 0,
            icon: Icons.home,
            selectedIndex: _selected,
          ),
          _button(
            index: 1,
            icon: Icons.favorite_border_outlined,
            selectedIndex: _selected,
          ),
        ],
      ),
    );
  }

  Widget _button({
    required int index,
    required IconData icon,
    VoidCallback? onPressed,
    int selectedIndex: 0,
  }) {
    bool isSelected = selectedIndex == index;
    return Material(
      color: isSelected ? AppTheme.palette.buttonOverlay : Colors.transparent,
      borderRadius: BorderRadius.circular(13),
      clipBehavior: Clip.antiAlias,
      child: IconButton(
        visualDensity: VisualDensity.compact,
        icon: Icon(
          icon,
          color: isSelected
              ? AppTheme.palette.secondaryColor
              : AppTheme.palette.buttonOverlay,
        ),
        onPressed: () {
          _selected = index;
          onPressed?.call();
          setState(() {});
        },
      ),
    );
  }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading