F
F
fedos8082022-01-10 20:52:18
Dart
fedos808, 2022-01-10 20:52:18

Custom Nav Bar, how to make navigation so that the button that was pressed remains when switching?

import 'package:flutter/material.dart';

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

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

class _buildNavBarItemState extends State<buildNavBarItem> {

  late int _selectedItemIndex;

  @override
  Widget build(BuildContext context) {
    return 
    Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        NavBarItem(UniconsLine.pen, 0, PravoMainScreen()),
        NavBarItem(UniconsLine.home_alt, 1, OrganizationScreen()),
        NavBarItem(UniconsLine.setting, 2, JusticeMainScreen()),
      ],
    );
  }
  Widget NavBarItem(IconData icon, int index, Widget screen) {
    return GestureDetector(
      onTap: () {
        setState(() {
          _selectedItemIndex = index;
        });
     Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => screen)
        );}, // Тут переходит на другой экран, но кнопка возвращается на исходную
      child: Container(
          height: 50.0,
          width: MediaQuery.of(context).size.width / 5.5,
          decoration: BoxDecoration(
            color: index == _selectedItemIndex ? Colors.white: Color(0xFF007FFF),
            borderRadius: BorderRadius.circular(20),
          ),
          child: Icon(icon, size: 44, color: index == _selectedItemIndex ? Color(0xFF007FFF): Colors.white),
        ),
    );
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Madi Pagani, 2022-01-13
@prince_pagani

em in BottomNavigationBar there is such element as selectedItemColor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question