Skip to content

[question] Bottom navigation with stacked #72

@RodolfoSilva

Description

@RodolfoSilva

Hi @FilledStacks, do you have any examples of how to use stacked with TabController or DefaultTabController?

Right now I'm implementing this way, but I don't know if this is the better way to do that:

home_view.dart

import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';

import 'package:my_app/ui/views/calendar/calendar_view.dart';
import 'package:my_app/ui/views/customer/customer_view.dart';
import 'package:my_app/ui/views/more/more_view.dart';
import 'package:my_app/ui/views/overview/overview_view.dart';
import 'package:my_app/ui/views/home/home_viewmodel.dart';

class HomeView extends StatelessWidget {
  const HomeView({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final tabs = [
      OverviewTabView(),
      CustomerTabView(),
      CalendarTabView(),
      MoreView(),
    ];
    final initialIndex = 0;
    return DefaultTabController(
      length: tabs.length,
      initialIndex: initialIndex,
      child: ViewModelBuilder<HomeViewModel>.reactive(
        viewModelBuilder: () => HomeViewModel(),
        onModelReady: (model) => model.setCurrentIndex(initialIndex),
        builder: (context, model, child) => Scaffold(
          body: TabBarView(
            children: tabs,
            physics: NeverScrollableScrollPhysics(),
          ),
          bottomNavigationBar: new Theme(
            data: Theme.of(context).copyWith(
              canvasColor: Colors.deepPurple,
              primaryColor: Colors.white,
              textTheme: Theme.of(context).textTheme.copyWith(
                    caption: new TextStyle(
                      color: Colors.white38,
                    ),
                  ),
            ),
            child: BottomNavigationBar(
              showUnselectedLabels: true,
              unselectedFontSize: 12.0,
              unselectedItemColor: Colors.white.withOpacity(0.5),
              onTap: (index) {
                DefaultTabController.of(context).animateTo(index);
                model.setCurrentIndex(index);
              },
              currentIndex: model.currentIndex,
              items: [
                BottomNavigationBarItem(
                  title: Text('Overview'),
                  icon: Icon(Icons.home),
                ),
                BottomNavigationBarItem(
                  title: Text('Customers'),
                  icon: Icon(Icons.group),
                ),
                BottomNavigationBarItem(
                  title: Text('Scheduler'),
                  icon: Icon(Icons.event),
                ),
                BottomNavigationBarItem(
                  title: Text('Settings'),
                  icon: Icon(Icons.more_horiz),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

home_viewmodule.dart

import 'package:stacked/stacked.dart';

class HomeViewModel extends BaseViewModel {
  int _currentIndex = 0;

  int get currentIndex => _currentIndex;

  void setCurrentIndex(int index) {
    _currentIndex = index;
    notifyListeners();
  }
}

Do you have any suggestion?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions