Flutter: Как задать разные действие при нажатии на BottomNavigationBarItem

Я создал BottomNavigationBar и мне надо чтобы при нажатии на боковые кнопки менялись страницы, а при нажатии на среднюю - появлялся диалог.

Вот код main.dart

class _MyHomePageState extends State<MyHomePage> {
  int _selectedIndex = 0;

  List<Widget> _widgetOptions = <Widget>[
    Home(),
    Add(),
    Calendar()
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _widgetOptions.elementAt(_selectedIndex),
      bottomNavigationBar: Container(
        height: 60,
        margin: EdgeInsets.only(left: 20, right: 20, bottom: 20),
        decoration: BoxDecoration(
          color: navbarColor,
          borderRadius: BorderRadius.circular(20),
          boxShadow: [
            BoxShadow(
              color: Colors.black38,
              blurRadius: 10,
              spreadRadius: 5
            )
          ]
        ),
        child: 
        ClipRRect(
          borderRadius: BorderRadius.circular(20),
          child: BottomNavigationBar(
            items: [
              BottomNavigationBarItem(
                icon: ImageIcon(
                  AssetImage('assets/icons/Home.png')),
                title: Text('Home')),
              BottomNavigationBarItem(
                icon: ImageIcon(
                  AssetImage('assets/icons/Add.png')),
                title: Text('Add')),
              BottomNavigationBarItem(
                icon: ImageIcon(
                  AssetImage('assets/icons/Calendar.png')),
                title: Text('Calendar')),
            ],
            currentIndex: _selectedIndex,
            selectedItemColor: mainColor,
            unselectedItemColor: iconColor,
            onTap: _onItemTapped,
            iconSize: 27,
            showSelectedLabels: false,
            showUnselectedLabels: false,
          )
        ),
      )
    );
  }
}

В home.dart у меня находится class Home() и void _add

Вместо Add() в main.dart надо поставить _add из home.dart


Ответы (0 шт):