0

Do I have to use Tabbar, TabBarView and TabbarController only with Scaffold and AppBar ?

I want each widget that shows some content to have 2-3 tabs to switch a view (so I want tabs inside Column or Container). I don't want use Scaffold for that, because my my app is designed in a way, that I would have to nest Scaffolds what is not recommended by documentation. So I prepared something like this:

@immutable
class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  final List<Tab> _homeScreenTabs = const <Tab>[
    Tab(text: 'Tab1', icon: Icon(Icons.camera)),
    Tab(text: 'Tab2', icon: Icon(Icons.camera)),
    Tab(text: 'Tab3', icon: Icon(Icons.camera)),
  ];

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: _homeScreenTabs.length,
      child: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            TabBar(
              labelColor: Colors.blue,
              tabs: _homeScreenTabs,
            ),
            Expanded(
              child: TabBarView(
                children: _homeScreenTabs.map((Tab tab) {
                  final String label = tab.text?.toLowerCase() ?? "";
                  return Center(
                    child: Text(
                      'This is the home $label',
                      style:
                          const TextStyle(color: Colors.black87, fontSize: 18),
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

It works but question is: is it correct to use TabBar in that way ? Documentation says only about using inside Scaffold.

luigibros
  • 69
  • 5
  • refer my answer [here](https://stackoverflow.com/a/69387027/13997210) for that hope its helpful to you. just remove `Scaffold()` widget in code and other code is same. – Ravindra S. Patil Dec 10 '21 at 05:02
  • @RavindraS.Patil thanks for reply. I see in you answer that you are doing the same what I did. But my question is: if it's correct. e.g. flutter allows for many things like nesting Scaffolds but documentation clearly says that we should this and show potential problems. – luigibros Dec 10 '21 at 09:51

0 Answers0