I'm going through some refactoring to look for a flutter architecture/coding style that suits my sensibilities. I like many small independent blocks of code. So, I'm trying to subclass the AppBar widget for example. The problem I am having is that in my subclasses I cannot get access to the BuildContext for the ultimate top level widget. In the snippet below I cannot find the "Navigator" to switch pages since I do not have a context to pass to "of(context)".
So, the question: what is the idiomatic pattern I should use to subclass stateful widgets (eg AppBar) when my descendants classes will need access to the build context?
Thanks for your help.
import 'package:flutter/material.dart';
class MyBaseAppBar extends AppBar {
MyBaseAppBar( { actions, title }) : super( actions: actions, title: title);
}
class MyPageAppBar extends MyBaseAppBar {
static var myPageActions = <Widget>[
IconButton(
icon: Icon(Icons.view_agenda),
onPressed: () =>Navigator.of( context ). pushNamed("agenda"))
];
MyPageAppBar() : super(
title : Text("My App Bar"),
actions : myPageActions
);
}
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: MyPageAppBar(),
body: Container() // for example
);
}
}