Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TabBar sample #549

Closed
prasant10050 opened this issue Oct 3, 2019 · 11 comments
Closed

TabBar sample #549

prasant10050 opened this issue Oct 3, 2019 · 11 comments
Assignees
Labels
question Further information is requested
Projects

Comments

@prasant10050
Copy link

How can we do handle state management with tabBar in flutter?Please upload a simple basic example where there are two tabs one tab showing seven days and seconds tab is showing 1 to 30 date's value,
it's urgent for my production app

@felangel
Copy link
Owner

felangel commented Oct 4, 2019

Hi @prasant10050 👋
Thanks for opening an issue!

Can you please provide some more information/example of what you're trying to build? I'm not sure why you need a bloc for a TabBar. You can have a bloc manage the state of each tab and wrap the tab in BlocProvider like so:

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

void main() {
  runApp(TabBarSample());
}

class TabBarSample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: [
                Tab(icon: Icon(Icons.directions_car)),
                Tab(icon: Icon(Icons.directions_transit)),
              ],
            ),
            title: Text('Tabs Demo'),
          ),
          body: TabBarView(
            children: [
              BlocProvider(
                builder: (context) => BlocA(),
                child: TabA(),
              ),
              BlocProvider(
                builder: (context) => BlocB(),
                child: TabB(),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Hope that helps 👍

@felangel felangel closed this as completed Oct 4, 2019
@felangel felangel self-assigned this Oct 4, 2019
@felangel felangel added the question Further information is requested label Oct 4, 2019
@felangel felangel added this to Done in bloc Oct 4, 2019
@prasant10050
Copy link
Author

prasant10050 commented Oct 4, 2019 via email

@prasant10050
Copy link
Author

prasant10050 commented Oct 4, 2019 via email

@felangel
Copy link
Owner

felangel commented Oct 4, 2019

@prasant10050 can you please share the complete code which isn’t working for you?

@prasant10050
Copy link
Author

prasant10050 commented Oct 4, 2019 via email

@felangel
Copy link
Owner

felangel commented Oct 4, 2019

@prasant10050 can you create a simple example? I don't need to see the company code, I just want to see a sample app which illustrates the same problem you're facing. Thanks!

@prasant10050
Copy link
Author

Sure!

@prasant10050
Copy link
Author

prasant10050 commented Oct 4, 2019

The first screen has two tabs, and each tab has a list of data.The list of data has two parts, the first header and the second its child inside that header,There is an add and delete button in the header section, a dialog box opens upon pressing the add button,The dialog box has two or three testfields and a submit button, after pressing the submit button, the data of the text field becomes the child of the header.You can also take a text field.But the issue is that the child is created, but the screen does not show immediate reflection, when I come from the second tab to the first tab, the result is visible.
In the same way, if you press the delete button from the header section, the child does not see any effect,
But if I work in a single screen, the effect of add and delete operation is seen, but the same screen not effect in tab bar view.I will thank you very much if you give me the solution for my issue

@felangel
Copy link
Owner

felangel commented Oct 4, 2019

@prasant10050 thanks for the explanation but can you please create a sample flutter app that reproduces the issue and share the link with me? It's very hard to help without that.

@gutisalex
Copy link

I might have a case it would be convenient but I am not sure if I need a cubit/bloc for that.. I have a TabBar and some views. Inside of a view somewhere in the depth of the widget tree I have a button and want to change the currentIndex of the tabBar. Either I would use a cubit to save the currentIndex or I could probably use a GlobalKey for that?! But what if I want to change tab styles according to the currentIndex:

borderRadius: BorderRadius.only(
                topLeft: Radius.circular(_tabController.index == 0 ? 9 : 0),
                topRight: Radius.circular(_tabController.index == 3 ? 9 : 0),
              ),

What do you think?

@xErik
Copy link

xErik commented Feb 27, 2022

The example above helped me figuring out using TabBarView and Bloc.

It should be noted that the child-widget accesses the returned state-value like this:

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

  @override
  Widget build(BuildContext context) {
    var state = context.read<BlocA>().state;
    var payload = state.your_payload;
  }
}

Also, the construction of the BlocProvider has to be updated:

BlocBuilder<BlocA, StateA>(
  builder: (context, state) => TabA(),
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
bloc
  
Done
Development

No branches or pull requests

5 participants
@xErik @felangel @prasant10050 @gutisalex and others