Home > other >  why is (_selected) in Widged _button undfined
why is (_selected) in Widged _button undfined

Time:07-21

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

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

CodePudding user response:

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

CodePudding user response:

Make sure your _button method is inside the _NavBarState class, otherwise you can't access the global data inside it.

  • Related