Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
refactor: fixed AppBar tab rendering
Browse files Browse the repository at this point in the history
OMG, this is finally working. I hate myself.
  • Loading branch information
dalbitresb12 committed Oct 27, 2022
1 parent 1bac4fc commit 0e99947
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 104 deletions.
79 changes: 0 additions & 79 deletions lib/layout/app_bar_widget.dart

This file was deleted.

96 changes: 72 additions & 24 deletions lib/layout/app_layout.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,90 @@
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:youniversity_app/layout/app_bar_widget.dart';
import 'package:youniversity_app/layout/app_location_item.dart';
import 'package:youniversity_app/layout/bottom_nav_bar_widget.dart';

class AppLayout extends StatelessWidget {
class AppLayout extends StatefulWidget {
AppLayout({
required List<AppLocationItem> navigation,
required this.navigation,
super.key,
}) : _navigation = navigation,
_beamerKey = GlobalKey<BeamerState>(),
_routerDelegate = BeamerDelegate(
locationBuilder: BeamerLocationBuilder(
beamLocations: [
...navigation.map((e) => e.beamLocation),
],
),
);

final List<AppLocationItem> _navigation;
final GlobalKey<BeamerState> _beamerKey;
final BeamerDelegate _routerDelegate;
}) : routerDelegate = _createRouter(navigation);

final List<AppLocationItem> navigation;
final BeamerDelegate routerDelegate;
final beamerKey = GlobalKey<BeamerState>();
final initialIndex = 0;

static BeamerDelegate _createRouter(List<AppLocationItem> navigation) {
final locations = navigation.map(((e) => e.beamLocation));

return BeamerDelegate(
locationBuilder: BeamerLocationBuilder(
beamLocations: [
...locations,
],
),
);
}

@override
State<AppLayout> createState() => _AppLayoutState();
}

class _AppLayoutState extends State<AppLayout> {
Widget _createTitle(int index) {
final title = widget.navigation[index].title.toUpperCase();
return Text(title);
}

TabBar? _createTabBar(int index) {
final config = widget.navigation[index].tabBar;
if (config == null) return null;

final items = config.items.map((e) => e.tab).toList();
return TabBar(tabs: items);
}

void _setStateListener() => setState(() {});

@override
void initState() {
super.initState();
widget.routerDelegate.addListener(_setStateListener);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBarWidget(
beamerKey: _beamerKey,
navigation: _navigation,
int index = widget.navigation.indexWhere((e) => e.beamLocation.isCurrent);
if (index == -1) index = widget.initialIndex;
final title = _createTitle(index);
final tabBar = _createTabBar(index);

final scaffold = Scaffold(
appBar: AppBar(
title: title,
bottom: tabBar,
),
body: Beamer(
key: _beamerKey,
routerDelegate: _routerDelegate,
key: widget.beamerKey,
routerDelegate: widget.routerDelegate,
),
bottomNavigationBar: BottomNavBarWidget(
beamerKey: _beamerKey,
navigation: _navigation,
beamerKey: widget.beamerKey,
navigation: widget.navigation,
),
);

if (tabBar == null) return scaffold;

return DefaultTabController(
length: tabBar.tabs.length,
child: scaffold,
);
}

@override
void dispose() {
widget.routerDelegate.removeListener(_setStateListener);
super.dispose();
}
}
6 changes: 5 additions & 1 deletion lib/layout/bottom_nav_bar_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class BottomNavBarWidget extends StatefulWidget {
required this.beamerKey,
required List<AppLocationItem> navigation,
super.key,
}) : navigation = navigation.where((e) => e.navigation != null).toList();
}) : navigation = _filterNavigation(navigation);

static List<AppLocationItem> _filterNavigation(List<AppLocationItem> items) {
return items.where((e) => e.navigation != null).toList();
}

final GlobalKey<BeamerState> beamerKey;
final List<AppLocationItem> navigation;
Expand Down

0 comments on commit 0e99947

Please sign in to comment.