RafaelBarbosatec / cube

Simple State Manager (Focusing on simplicity and rebuilding only the necessary)
MIT License
35 stars 2 forks source link

Is there a way to build widgets based on multiple/all fields changing? #55

Closed Kiwi-KiwiCorp closed 1 year ago

Kiwi-KiwiCorp commented 2 years ago

let's say I had a class and in that class, I had the following fields:

final count = 0.obsValue;
final shouldReset = false.obsValue;

is there a way I could listen to both/all fields for a change? or would I need to nest two builders inside each other currently? I think a good syntax for rebuilding based on any field changing would be:

class View extends CubeWidget<CubeState> {
  const View({Key? key}) : super(key: key);

  @override
  Widget buildView(BuildContext context, CubeState cube) {
    return Column(
      children: [
        const Text('Your count is:'),
        cube.build((CubeState values) => Text(values.count.toString()))
      ],
    );
  }
}

and then the syntax for specifying multiple fields to listen to could be something like this:

class View extends CubeWidget<CubeState> {
  const View({Key? key}) : super(key: key);

  @override
  Widget buildView(BuildContext context, CubeState cube) {
    return Column(
      children: [
        const Text('Your count is:'),
        cube.buildSelected([cube.count, cube.shouldReset], (CubeState values) => Text(values.count.toString()))
      ],
    );
  }
}

you might actually have to specify the fields something like this unless you can figure out a way to do it using the above syntax:

cube.buildSelected([cube.count.select, cube.shouldReset.select], (CubeState values) => Text(values.count.toString()))

I think this addition would make cubes much more versatile.

RafaelBarbosatec commented 2 years ago

Hi @Kiwi-KiwiCorp ! Thanks for this discussion! So, in the moment you need to nest two builders inside each other. I thing your suggestion interesting. I'll be thinking of a solution for this following the line of what we already have in 'Cubes'. But i see no problem in nest two builders inside each other like this:

cube.firstObs.build((value1){
  return Column(
     children:[
          Text('Hello $value1'),
          cube.secondObs.build((value2){
             return Text('My Ocupation is $value2'),
          }),
      ]
  );
});
RafaelBarbosatec commented 2 years ago

If i follow exactly you suggestion. this packager will is equal flutter_bloc. And this is not my idea.

You can use state approach too:

class MyState {
   final int count;
   MyState(this.count);
   MyState.initial({this.count = 0});

   MyState copyWith({int? count}){
      return MyState(count ?? this.count);
    }
}

class MyCube extends Cube{
    final state = MyState.initial().obs;
}
class View extends CubeWidget<MyCube> {
  const View({Key? key}) : super(key: key);

  @override
  Widget buildView(BuildContext context, MyCube cube) {
    return cube.state.build((MyState state){
       return Column(
          children: [
            const Text('Your count is:'),
           Text(state.count.toString()),
          ],
       );
   });

  }
}