ShadyBoukhary/flutter_clean_architecture

Controller in PreferredSizeWidget

Closed this issue · 1 comments

Is there a way to access the controller from the AppBar? It is not possible to use ControlledWidgetBuilder as it returns a Widget and AppBar requires a PreferredSizeWidget

Example:

AppBar get appBar => AppBar(
        title: Text("Title"),
        backgroundColor: Colors.white,
);

Valid case:

AppBar get appBar => AppBar(
        title: ControlledWidgetBuilder<Controller>(builder: (context, controller) {
              return Text(controller.title),
        }
       backgroundColor: Colors.white,
);

but if I wanted to get the background color from the controller I would have to:

AppBar get appBar => ControlledWidgetBuilder<Controller>(builder: (context, controller) {
        return AppBar(
                title: Text("Title"),
                backgroundColor: controller.color,
        );
});

and this is invalid because AppBar requires a PreferredSizeWidget

Hi @javier-mora!

Thanks for the notice. You can achieve it in some ways.

  1. You can encapsulate your View inside the controlled widget builder (although this is not very optimal, but it works depending on your necessity)
ControlledWidgetBuilder<Controller>(builder: (context, controller) {
        return Scaffold(
			...
		)
});
  1. You can create your own AppBar widget to be used
class MyAwesomeAppBar extends StatelessWidget implements PreferredSizeWidget {
	...
}

void main() {
	return Scaffold(
		appBar: MyAwesomeAppBar(...)
	);
}
  1. You can define it directly on the view (Obviously, if you do not want it to be Controlled)

ControlledWidgetBuilder should be agnostic about the Widget type.

Thanks for let us know, i'm closing this issue, if you need something more, feel free to re-open