flutter noob here.

I followed along on a tutorial to create my bottom navigation bar. Now all I want is to have a redirect to a confirmation page after a user submits a new trip to be created in my app. The issue is when I rebuild the page after the button submit, the tab on the bottom nav bar doesn't change. I believe there's a very simple solution here, however I still haven't been able to figure it out.

Here is the custom Scaffold: `

class CupertinoHomeScaffold extends StatelessWidget {
  const CupertinoHomeScaffold({
    Key? key,
    required this.currentTab,
    required this.onSelectedTab,
    required this.widgetBuilders,
  }) : super(key: key);

  final TabItem currentTab;
  final ValueChanged<TabItem> onSelectedTab;
  final Map<TabItem, WidgetBuilder> widgetBuilders;

  @override
  Widget build(BuildContext context) {
    return CupertinoTabScaffold(
  
        tabBar: CupertinoTabBar(
          items: [
            _buildItem(TabItem.home),
            _buildItem(TabItem.search),
            _buildItem(TabItem.addtrip),
            _buildItem(TabItem.mytrips),
            _buildItem(TabItem.profile),
          ],
          onTap: (index) => onSelectedTab(TabItem.values[index]),
        ),
        tabBuilder: (context, index) {
          final item = TabItem.values[index];
          return CupertinoTabView(
            builder: (context) => widgetBuilders[item]!(context),
          );
        });
  }

  BottomNavigationBarItem _buildItem(TabItem tabItem) {
    final itemData = TabItemData.allTabs[tabItem];
    final color = currentTab == tabItem ? const Color(0xFF4DD8E7) : Colors.grey;
    return BottomNavigationBarItem(
      icon: Icon(itemData!.icon, color: color),
      label: itemData.title,
    );
  }
}


`

And here is the page that build the selected TabItem page:

`

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  TabItem _currentTab = TabItem.home;
  


  Map<TabItem, WidgetBuilder> get widgetBuilders {
    return {
      TabItem.home: (_) => TripsHome(),
      TabItem.search: (_) => const SearchPage(),
      TabItem.addtrip: (_) => AddTrip2(),
      TabItem.mytrips: (_) => const MyTrips(),
      TabItem.profile: (_) => const ProfileData(),
    };
  }

  void _select(TabItem tabItem) {
    setState(() => _currentTab = tabItem);
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoHomeScaffold(
      currentTab: _currentTab,
      onSelectedTab: _select,
      widgetBuilders: widgetBuilders,
    );
  }
}

`

I only want to have the TabItem.mytrips to be selected after the submission and then the user will be able to see the trip that they posted. I tried to setState after the submission, but I'm missing something to actually change the selectedTab of the CupertinoHomeScaffold.

0

There are 0 answers