This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: fixed AppBar tab rendering
OMG, this is finally working. I hate myself.
- Loading branch information
1 parent
1bac4fc
commit 0e99947
Showing
3 changed files
with
77 additions
and
104 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters