For navigation, I built a simple factory class that generates a ListTile that pushes a route to the Navigator:
static Widget simpleNavRow(String text, BuildContext context, String route) {
return Column(
children: <Widget>[
ListTile(
title: Text(text),
onTap: () {
Navigator.pushNamed(context, route);
},
),
Divider(),
],
);
}
However, I soon realized that it would be convenient to support pushing widgets as well (or instantiate from their class if possible). I couldn't figure out how to make the "route" argument accept either a String or a Widget, so I created a class that initializes with one of those two types. This code works, but is there a better way to achieve this?
class NavTo {
String route;
Widget widget;
NavTo.route(this.route);
NavTo.widget(this.widget);
push(BuildContext context) {
if (route != null) {
Navigator.pushNamed(context, route);
}
if (widget != null) {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return widget;
}));
}
}
}
class ListHelper {
static final padding = EdgeInsets.all(12.0);
static Widget simpleNavRow(String text, BuildContext context, NavTo navTo) {
return Column(
children: <Widget>[
ListTile(
title: Text(text),
onTap: () {
navTo.push(context);
},
),
Divider(),
],
);
}
}
// usage:
// ListHelper.simpleNavRow('MyWidget', context, NavTo.widget(MyWidget()))
MaterialApp#onGenerateRoute
? that way you can usepushNamed
and still have the freedom for returning anyRoute
you want – Jerri