caduandrade / tabbed_view

Widget inspired by the classic Desktop-style tab component.
MIT License
49 stars 16 forks source link

Set initial tab / Change tabs programatically #36

Closed fjelljager closed 1 year ago

fjelljager commented 1 year ago

Firstly I would like to say thank you for your fantastic widget!

I have a situation where the initial tab I would like open is not always the first tab. I could not find an "initial tab" parameter anywhere or a way to change the tab programmatically to achieve the same result. If you have any advice on how best to do this it would be much appreciated, thank you.

caduandrade commented 1 year ago

Hi @fjelljager! Thanks.

It's possible to change the selected tab using the controller.selectedIndex ok? So you can change it in initState.

  @override
  void initState() {
    super.initState();

    tabs.add(TabData(
        text: 'Tab 1',
        leading: (context, status) => Icon(Icons.star, size: 16),
        content: Text('Hello')));
    tabs.add(TabData(text: 'Tab 2', content: Text('Hello again')));

    _controller = TabbedViewController(tabs);
    _controller.selectedIndex = 1; // second tab as initial tab
  }

I believe that it's enough for you right?

fjelljager commented 1 year ago

Hi @caduandrade thank you for the quick reply, it's much appreciated!

I just tried that and it worked perfectly, thank you very much.

I think it would be useful to include this in the documentation if you get the time :)