0

I have my base code working for cubit to update a simple counter in a class that is in a row on my app listview.

I want to update the counter of total items using bottomNavigationBar and a totalbagel field which is in a different class that my cubit. I wrapped my navbar text field totalbabgel in a block see below.

My issue is i do not know how to call in the cubit field in my bottom nav bar variable.

What is the correct format to reference a state variable from my cubit class?? All the online examples show only cubit state within same class text update. I need update state in 2 widgets.

text

     bottomNavigationBar: BottomAppBar(
            child: Row(
              children: [
                //IconButton(icon: Icon(Icons.menu), onPressed: () {}),
                Spacer(),
                Container(
                  height: 55.0,
                  width: 1.0,
                ),
                //TODO get bakerdoz and etotal on footer working need to pass data between main and bagelcounter
    
              BlocBuilder<CounterCubit, CounterCubitState>(
                        key: UniqueKey(),
                        builder: (context,state)
                         => Text("Baker's Dozen: " +  $totalbagels  + "    Singles:  $singles",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 20.0,
                        fontWeight: FontWeight.w500)),
    
              ),


my test cubit  increment code:

 

       CounterCubit()
          : super(CounterCubitInitial(dozcount: 0, singcount: 0, totalbagels: 0));
    
      void increment() {
        int  holdcart = (state.totalbagels + 1);
        holdtotal = ((state.totalbagels + 1) ~/ 13);
        holdsingle = ((state.totalbagels + 1) % 13);
        print("+holdsingle: " + holdsingle.toStringAsFixed(2));
        print("+holdtotal: " + holdtotal.toStringAsFixed(2));
        print("+holdcart: " + holdcart.toStringAsFixed(2));
        CartTotal(holdcart);
    
        emit(CounterCubitIncreased(
          totalbagels: (state.totalbagels + 1),
          // dozcount: ((state.totalbagels +1) ~/ 13),
          singcount: ((state.totalbagels +1) % 13),
    
          // singcount: ((state.totalbagels +1) % 13),
        ));
      }

1 Answers1

0

You can handle with states. If you defined states for cubit, you can use state in your BlocBuilder

  key: UniqueKey(),
  builder: (context,state) {
    if(state is CounterCubitInitial) {
        return Text();
    } else if (state is CounterCubitIncreased) {
        return Text("Baker's Dozen: " +  state.totalbagels  + "    Singles:  ${state.singles}",
                  style: TextStyle(
                  color: Colors.white,
                  fontSize: 20.0,
                  fontWeight: FontWeight.w500));
    }
  }           
);
Hasan Karli
  • 101
  • 1
  • 3