Skip to content

Commit

Permalink
add initial route assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshj01 committed Jun 15, 2022
1 parent 480335a commit b1cc2d7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
3 changes: 3 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

linter:
rules:
8 changes: 4 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void main() {

class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
List<Color> colors = [mediumPurple, Colors.orange, Colors.teal];
final List<Color> colors = [mediumPurple, Colors.orange, Colors.teal];

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -42,7 +42,7 @@ class HomePage extends StatelessWidget {
DestinationRouter(
navbarItem:
NavbarItem(Icons.home, 'Home', backgroundColor: colors[0]),
destination: [
destinations: [
Destination(
route: '/',
widget: const HomeFeeds(),
Expand All @@ -58,7 +58,7 @@ class HomePage extends StatelessWidget {
DestinationRouter(
navbarItem: NavbarItem(Icons.shopping_basket, 'Products',
backgroundColor: colors[1]),
destination: [
destinations: [
Destination(
route: '/',
widget: const ProductList(),
Expand All @@ -78,7 +78,7 @@ class HomePage extends StatelessWidget {
DestinationRouter(
navbarItem:
NavbarItem(Icons.person, 'Me', backgroundColor: colors[2]),
destination: [
destinations: [
Destination(
route: '/',
widget: const UserProfile(),
Expand Down
41 changes: 33 additions & 8 deletions lib/src/navbar_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Destination {
}

class DestinationRouter {
final List<Destination> destination;
final List<Destination> destinations;

/// Route to load when the app is started
/// for the current destination, defaults to '/'
Expand All @@ -22,9 +22,23 @@ class DestinationRouter {
final NavbarItem navbarItem;

DestinationRouter(
{required this.destination,
{required this.destinations,
required this.navbarItem,
this.initialRoute = '/'});
this.initialRoute = '/'})
: assert(_isRoutePresent(initialRoute, destinations),
'Initial route must be present in List of Destinations');
}

/// helper class for assert
bool _isRoutePresent(String route, List<Destination> destinations) {
bool isPresent = false;
for (Destination destination in destinations) {
if (destination.route == route) {
isPresent = true;
return isPresent;
}
}
return isPresent;
}

class NavbarDecoration {
Expand Down Expand Up @@ -152,7 +166,18 @@ class _NavbarRouterState extends State<NavbarRouter>

@override
void didUpdateWidget(covariant NavbarRouter oldWidget) {
// TODO: implement didUpdateWidget
/// update animation
if (widget.destinationAnimationCurve !=
oldWidget.destinationAnimationCurve ||
widget.destinationAnimationDuration !=
oldWidget.destinationAnimationDuration) {
_controller.duration =
Duration(milliseconds: widget.destinationAnimationDuration);
fadeAnimation = Tween<double>(begin: 0.4, end: 1.0).animate(
CurvedAnimation(
parent: _controller, curve: widget.destinationAnimationCurve),
);
}
super.didUpdateWidget(oldWidget);
}

Expand Down Expand Up @@ -182,15 +207,15 @@ class _NavbarRouterState extends State<NavbarRouter>
widget.destinations[i].initialRoute,
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder? builder = widget.errorBuilder;
final nestedLength =
widget.destinations[i].destination.length;
final nestedLength = widget
.destinations[i].destinations.length;
for (int j = 0; j < nestedLength; j++) {
if (widget.destinations[i].destination[j]
if (widget.destinations[i].destinations[j]
.route ==
settings.name) {
builder = (BuildContext _) => widget
.destinations[i]
.destination[j]
.destinations[j]
.widget;
}
}
Expand Down

0 comments on commit b1cc2d7

Please sign in to comment.