2020-10-14 19:09:16 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
import 'package:audio_service/audio_service.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2020-07-16 20:25:30 +00:00
|
|
|
import 'package:flutter/rendering.dart';
|
2020-10-09 18:52:45 +00:00
|
|
|
import 'package:flutter/services.dart';
|
2021-03-21 21:46:44 +00:00
|
|
|
import 'package:flutter_displaymode/flutter_displaymode.dart';
|
2020-09-18 17:36:41 +00:00
|
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
2021-08-29 22:25:18 +00:00
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
2020-10-09 18:52:45 +00:00
|
|
|
import 'package:freezer/api/cache.dart';
|
2020-10-15 18:37:36 +00:00
|
|
|
import 'package:freezer/api/definitions.dart';
|
2020-06-23 19:23:12 +00:00
|
|
|
import 'package:freezer/ui/library.dart';
|
|
|
|
import 'package:freezer/ui/login_screen.dart';
|
|
|
|
import 'package:freezer/ui/search.dart';
|
2020-11-15 19:25:28 +00:00
|
|
|
import 'package:freezer/ui/updater.dart';
|
2020-09-18 17:36:41 +00:00
|
|
|
import 'package:i18n_extension/i18n_widget.dart';
|
2020-06-23 19:23:12 +00:00
|
|
|
import 'package:move_to_background/move_to_background.dart';
|
2020-09-18 17:36:41 +00:00
|
|
|
import 'package:freezer/translations.i18n.dart';
|
2020-10-16 18:54:04 +00:00
|
|
|
import 'package:quick_actions/quick_actions.dart';
|
2020-10-14 19:09:16 +00:00
|
|
|
import 'package:uni_links/uni_links.dart';
|
2020-06-23 19:23:12 +00:00
|
|
|
|
|
|
|
import 'api/deezer.dart';
|
|
|
|
import 'api/download.dart';
|
|
|
|
import 'api/player.dart';
|
2020-09-18 17:36:41 +00:00
|
|
|
import 'settings.dart';
|
2020-06-23 19:23:12 +00:00
|
|
|
import 'ui/home_screen.dart';
|
2020-09-18 17:36:41 +00:00
|
|
|
import 'ui/player_bar.dart';
|
2020-07-16 20:25:30 +00:00
|
|
|
|
2021-09-01 12:38:32 +00:00
|
|
|
late Function updateTheme;
|
|
|
|
late Function logOut;
|
2020-06-23 19:23:12 +00:00
|
|
|
GlobalKey<NavigatorState> mainNavigatorKey = GlobalKey<NavigatorState>();
|
2021-09-01 12:38:32 +00:00
|
|
|
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
2021-08-30 12:51:51 +00:00
|
|
|
|
2020-09-18 17:36:41 +00:00
|
|
|
void main() async {
|
2020-06-23 19:23:12 +00:00
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
|
|
|
//Initialize globals
|
|
|
|
settings = await Settings().loadSettings();
|
|
|
|
await downloadManager.init();
|
2020-10-09 18:52:45 +00:00
|
|
|
cache = await Cache.load();
|
2020-06-23 19:23:12 +00:00
|
|
|
|
2020-11-09 21:05:47 +00:00
|
|
|
//Do on BG
|
|
|
|
playerHelper.authorizeLastFM();
|
|
|
|
|
2021-09-01 12:38:32 +00:00
|
|
|
// initialize our audiohandler instance
|
|
|
|
audioHandler = await AudioService.init<AudioPlayerTask>(
|
|
|
|
builder: () => AudioPlayerTask(),
|
|
|
|
config: AudioServiceConfig(
|
|
|
|
androidStopForegroundOnPause: false,
|
|
|
|
androidNotificationOngoing: false,
|
|
|
|
androidNotificationClickStartsActivity: true,
|
|
|
|
androidNotificationChannelDescription: 'Freezer',
|
|
|
|
androidNotificationChannelName: 'Freezer',
|
|
|
|
androidNotificationIcon: 'drawable/ic_logo',
|
|
|
|
preloadArtwork: true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
runApp(FreezerApp());
|
|
|
|
}
|
|
|
|
|
|
|
|
class FreezerApp extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_FreezerAppState createState() => _FreezerAppState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _FreezerAppState extends State<FreezerApp> {
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
//Make update theme global
|
|
|
|
updateTheme = _updateTheme;
|
2020-10-16 18:54:04 +00:00
|
|
|
_updateTheme();
|
2020-06-23 19:23:12 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2020-08-16 20:17:22 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
void _updateTheme() {
|
|
|
|
setState(() {
|
|
|
|
settings.themeData;
|
|
|
|
});
|
2020-10-16 18:54:04 +00:00
|
|
|
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
|
2021-09-01 12:38:32 +00:00
|
|
|
systemNavigationBarColor: settings.themeData!.bottomAppBarColor,
|
2021-04-05 21:27:15 +00:00
|
|
|
systemNavigationBarIconBrightness:
|
|
|
|
settings.isDark ? Brightness.light : Brightness.dark,
|
2020-10-16 18:54:04 +00:00
|
|
|
));
|
2020-06-23 19:23:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 12:38:32 +00:00
|
|
|
Locale? _locale() {
|
|
|
|
if (settings.language == null || settings.language!.split('_').length < 2)
|
2021-04-05 21:27:15 +00:00
|
|
|
return null;
|
|
|
|
return Locale(
|
2021-09-01 12:38:32 +00:00
|
|
|
settings.language!.split('_')[0], settings.language!.split('_')[1]);
|
2020-09-18 17:36:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-08-29 22:25:18 +00:00
|
|
|
return ScreenUtilInit(
|
|
|
|
designSize: Size(1080, 720),
|
|
|
|
builder: () => MaterialApp(
|
|
|
|
title: 'Freezer',
|
|
|
|
shortcuts: <ShortcutActivator, Intent>{
|
|
|
|
...WidgetsApp.defaultShortcuts,
|
|
|
|
LogicalKeySet(LogicalKeyboardKey.select):
|
|
|
|
const ActivateIntent(), // DPAD center key, for remote controls
|
2020-06-23 19:23:12 +00:00
|
|
|
},
|
2021-08-29 22:25:18 +00:00
|
|
|
theme: settings.themeData,
|
|
|
|
localizationsDelegates: [
|
|
|
|
GlobalMaterialLocalizations.delegate,
|
|
|
|
GlobalWidgetsLocalizations.delegate,
|
|
|
|
GlobalCupertinoLocalizations.delegate,
|
|
|
|
],
|
|
|
|
supportedLocales: supportedLocales,
|
|
|
|
home: WillPopScope(
|
|
|
|
onWillPop: () async {
|
2021-09-01 12:38:32 +00:00
|
|
|
if (navigatorKey.currentState!.canPop()) {
|
|
|
|
await navigatorKey.currentState!.maybePop();
|
2021-08-29 22:25:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
await MoveToBackground.moveTaskToBack();
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
child: I18n(
|
|
|
|
initialLocale: _locale(),
|
|
|
|
child: LoginMainWrapper(),
|
|
|
|
),
|
2020-09-18 17:36:41 +00:00
|
|
|
),
|
2021-08-29 22:25:18 +00:00
|
|
|
navigatorKey: mainNavigatorKey,
|
2020-06-23 19:23:12 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Wrapper for login and main screen.
|
|
|
|
class LoginMainWrapper extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_LoginMainWrapperState createState() => _LoginMainWrapperState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LoginMainWrapperState extends State<LoginMainWrapper> {
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
if (settings.arl != null) {
|
|
|
|
playerHelper.start();
|
|
|
|
//Load token on background
|
|
|
|
deezerAPI.arl = settings.arl;
|
|
|
|
settings.offlineMode = true;
|
2021-09-01 12:38:32 +00:00
|
|
|
deezerAPI.authorize()!.then((b) async {
|
2020-06-23 19:23:12 +00:00
|
|
|
if (b) setState(() => settings.offlineMode = false);
|
|
|
|
});
|
|
|
|
}
|
2020-07-16 20:25:30 +00:00
|
|
|
//Global logOut function
|
|
|
|
logOut = _logOut;
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2020-07-16 20:25:30 +00:00
|
|
|
Future _logOut() async {
|
|
|
|
setState(() {
|
|
|
|
settings.arl = null;
|
2020-12-04 17:02:50 +00:00
|
|
|
settings.offlineMode = false;
|
2020-07-16 20:25:30 +00:00
|
|
|
deezerAPI = new DeezerAPI();
|
|
|
|
});
|
|
|
|
await settings.save();
|
2020-12-04 17:02:50 +00:00
|
|
|
await Cache.wipe();
|
2020-07-16 20:25:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (settings.arl == null)
|
2020-09-18 17:36:41 +00:00
|
|
|
return LoginWidget(
|
|
|
|
callback: () => setState(() => {}),
|
|
|
|
);
|
2020-06-23 19:23:12 +00:00
|
|
|
return MainScreen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MainScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_MainScreenState createState() => _MainScreenState();
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:27:15 +00:00
|
|
|
class _MainScreenState extends State<MainScreen>
|
|
|
|
with SingleTickerProviderStateMixin, WidgetsBindingObserver {
|
2020-09-18 17:36:41 +00:00
|
|
|
List<Widget> _screens = [HomeScreen(), SearchScreen(), LibraryScreen()];
|
2021-09-01 12:38:32 +00:00
|
|
|
final _selected = ValueNotifier<int>(0);
|
|
|
|
StreamSubscription? _urlLinkStream;
|
2020-10-31 20:52:23 +00:00
|
|
|
int _keyPressed = 0;
|
2020-11-22 10:17:37 +00:00
|
|
|
bool textFieldVisited = false;
|
2020-06-23 19:23:12 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2021-03-21 21:46:44 +00:00
|
|
|
//Set display mode
|
2021-09-01 12:38:32 +00:00
|
|
|
if (settings.displayMode != null && settings.displayMode! >= 0) {
|
2021-03-21 21:46:44 +00:00
|
|
|
FlutterDisplayMode.supported.then((modes) async {
|
2021-09-01 12:38:32 +00:00
|
|
|
if (modes.length - 1 >= settings.displayMode!)
|
|
|
|
FlutterDisplayMode.setPreferredMode(modes[settings.displayMode!]);
|
2021-03-21 21:46:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-28 21:32:17 +00:00
|
|
|
_startStreamingServer();
|
|
|
|
|
2020-10-16 18:54:04 +00:00
|
|
|
//Start with parameters
|
|
|
|
_setupUniLinks();
|
2020-10-15 18:37:36 +00:00
|
|
|
_loadPreloadInfo();
|
2020-10-16 18:54:04 +00:00
|
|
|
_prepareQuickActions();
|
2020-10-15 18:37:36 +00:00
|
|
|
|
2020-11-15 19:25:28 +00:00
|
|
|
//Check for updates on background
|
|
|
|
Future.delayed(Duration(seconds: 5), () {
|
|
|
|
FreezerVersions.checkUpdate();
|
|
|
|
});
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
super.initState();
|
2021-09-01 12:38:32 +00:00
|
|
|
WidgetsBinding.instance!.addObserver(this);
|
2020-06-23 19:23:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 21:32:17 +00:00
|
|
|
void _startStreamingServer() async {
|
2021-04-05 21:27:15 +00:00
|
|
|
await DownloadManager.platform
|
|
|
|
.invokeMethod("startServer", {"arl": settings.arl});
|
2020-11-28 21:32:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 18:54:04 +00:00
|
|
|
void _prepareQuickActions() {
|
|
|
|
final QuickActions quickActions = QuickActions();
|
|
|
|
quickActions.initialize((type) {
|
2021-09-01 12:38:32 +00:00
|
|
|
_startPreload(type);
|
2020-10-16 18:54:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//Actions
|
|
|
|
quickActions.setShortcutItems([
|
2021-04-05 21:27:15 +00:00
|
|
|
ShortcutItem(
|
|
|
|
type: 'favorites',
|
|
|
|
localizedTitle: 'Favorites'.i18n,
|
|
|
|
icon: 'ic_favorites'),
|
2020-10-16 18:54:04 +00:00
|
|
|
ShortcutItem(type: 'flow', localizedTitle: 'Flow'.i18n, icon: 'ic_flow'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _startPreload(String type) async {
|
|
|
|
await deezerAPI.authorize();
|
|
|
|
if (type == 'flow') {
|
|
|
|
await playerHelper.playFromSmartTrackList(SmartTrackList(id: 'flow'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type == 'favorites') {
|
|
|
|
Playlist p = await deezerAPI.fullPlaylist(deezerAPI.favoritesPlaylistId);
|
2021-09-01 12:38:32 +00:00
|
|
|
playerHelper.playFromPlaylist(p, p.tracks![0]!.id);
|
2020-10-16 18:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 18:37:36 +00:00
|
|
|
void _loadPreloadInfo() async {
|
2021-09-01 12:38:32 +00:00
|
|
|
String? info =
|
|
|
|
await DownloadManager.platform.invokeMethod('getPreloadInfo');
|
2020-10-15 18:37:36 +00:00
|
|
|
if (info != null) {
|
|
|
|
//Used if started from android auto
|
|
|
|
await deezerAPI.authorize();
|
2020-10-16 18:54:04 +00:00
|
|
|
_startPreload(info);
|
2020-10-15 18:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 19:09:16 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2021-09-01 12:38:32 +00:00
|
|
|
_urlLinkStream?.cancel();
|
|
|
|
WidgetsBinding.instance!.removeObserver(this);
|
2020-10-14 19:09:16 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-11-22 10:17:37 +00:00
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
|
|
if (state == AppLifecycleState.resumed) {
|
|
|
|
setState(() {
|
|
|
|
textFieldVisited = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 18:54:04 +00:00
|
|
|
void _setupUniLinks() async {
|
2020-10-14 19:09:16 +00:00
|
|
|
//Listen to URLs
|
2021-09-01 12:38:32 +00:00
|
|
|
_urlLinkStream = linkStream.listen((String? link) {
|
|
|
|
if (link == null) return;
|
|
|
|
openScreenByURL(context, link);
|
2020-10-14 19:09:16 +00:00
|
|
|
}, onError: (err) {});
|
|
|
|
//Get initial link on cold start
|
|
|
|
try {
|
2021-09-01 12:38:32 +00:00
|
|
|
String? link = await getInitialLink();
|
2021-04-05 21:27:15 +00:00
|
|
|
if (link != null && link.length > 4) openScreenByURL(context, link);
|
2020-10-14 19:09:16 +00:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:27:15 +00:00
|
|
|
ValueChanged<RawKeyEvent> _handleKey(
|
|
|
|
FocusScopeNode navigationBarFocusNode, FocusNode screenFocusNode) {
|
2020-10-31 20:52:23 +00:00
|
|
|
return (event) {
|
2021-09-01 12:38:32 +00:00
|
|
|
FocusNode primaryFocus = FocusManager.instance.primaryFocus!;
|
2020-11-22 10:17:37 +00:00
|
|
|
// After visiting text field, something goes wrong and KeyDown events are not sent, only KeyUp-s.
|
|
|
|
// So, set this flag to indicate a transition to other "mode"
|
2021-09-01 12:38:32 +00:00
|
|
|
if (primaryFocus.context!.widget.runtimeType.toString() ==
|
2021-04-05 21:27:15 +00:00
|
|
|
'EditableText') {
|
2020-11-22 10:17:37 +00:00
|
|
|
setState(() {
|
|
|
|
textFieldVisited = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Movement to navigation bar and back
|
2021-04-05 21:27:15 +00:00
|
|
|
if (event.runtimeType.toString() ==
|
|
|
|
(textFieldVisited ? 'RawKeyUpEvent' : 'RawKeyDownEvent')) {
|
2020-10-31 20:52:23 +00:00
|
|
|
int keyCode = (event.data as RawKeyEventDataAndroid).keyCode;
|
|
|
|
switch (keyCode) {
|
|
|
|
case 127: // Menu on Android TV
|
|
|
|
case 327: // EPG on Hisense TV
|
2020-11-22 10:17:37 +00:00
|
|
|
focusToNavbar(navigationBarFocusNode);
|
2020-10-31 20:52:23 +00:00
|
|
|
break;
|
|
|
|
case 22: // LEFT + RIGHT
|
|
|
|
case 21:
|
2021-04-05 21:27:15 +00:00
|
|
|
if (_keyPressed == 21 && keyCode == 22 ||
|
|
|
|
_keyPressed == 22 && keyCode == 21) {
|
2020-11-22 10:17:37 +00:00
|
|
|
focusToNavbar(navigationBarFocusNode);
|
2020-10-31 20:52:23 +00:00
|
|
|
}
|
|
|
|
_keyPressed = keyCode;
|
2021-04-05 21:27:15 +00:00
|
|
|
Future.delayed(
|
|
|
|
Duration(milliseconds: 100), () => {_keyPressed = 0});
|
2020-10-31 20:52:23 +00:00
|
|
|
break;
|
2020-11-03 15:26:24 +00:00
|
|
|
case 20: // DOWN
|
|
|
|
// If it's bottom row, go to navigation bar
|
2020-11-22 10:17:37 +00:00
|
|
|
var row = primaryFocus.parent;
|
|
|
|
if (row != null) {
|
2021-09-01 12:38:32 +00:00
|
|
|
var column = row.parent!;
|
2020-11-22 10:17:37 +00:00
|
|
|
if (column.children.last == row) {
|
|
|
|
focusToNavbar(navigationBarFocusNode);
|
|
|
|
}
|
2020-11-03 15:26:24 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-10-31 20:52:23 +00:00
|
|
|
case 19: // UP
|
2020-11-22 10:17:37 +00:00
|
|
|
if (navigationBarFocusNode.hasFocus) {
|
2021-09-01 12:38:32 +00:00
|
|
|
screenFocusNode.parent!.parent!.children
|
2021-04-05 21:27:15 +00:00
|
|
|
.last // children.last is used for handling "playlists" screen in library. Under CustomNavigator 2 screens appears.
|
2020-11-22 10:17:37 +00:00
|
|
|
.nextFocus(); // nextFocus is used instead of requestFocus because it focuses on last, bottom, non-visible tile of main page
|
|
|
|
|
2020-10-31 20:52:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-22 10:17:37 +00:00
|
|
|
// After visiting text field, something goes wrong and KeyDown events are not sent, only KeyUp-s.
|
|
|
|
// Focus moving works only on KeyDown events, so here we simulate keys handling as it's done in Flutter
|
|
|
|
if (textFieldVisited && event.runtimeType.toString() == 'RawKeyUpEvent') {
|
2021-09-01 12:38:32 +00:00
|
|
|
Map<LogicalKeySet, Intent> shortcuts =
|
|
|
|
Shortcuts.of(context).shortcuts as Map<LogicalKeySet, Intent>;
|
|
|
|
final BuildContext? primaryContext = primaryFocus.context;
|
|
|
|
Intent? intent = shortcuts[LogicalKeySet(event.logicalKey)];
|
2020-11-22 10:17:37 +00:00
|
|
|
if (intent != null) {
|
2021-09-01 12:38:32 +00:00
|
|
|
Actions.invoke(primaryContext!, intent);
|
2020-11-22 10:17:37 +00:00
|
|
|
}
|
|
|
|
// WA for "Search field -> navigator -> UP -> DOWN" case. Prevents focus hanging.
|
2021-09-01 12:38:32 +00:00
|
|
|
FocusNode? newFocus = FocusManager.instance.primaryFocus;
|
2020-11-22 10:17:37 +00:00
|
|
|
if (newFocus is FocusScopeNode) {
|
|
|
|
navigationBarFocusNode.requestFocus();
|
2020-10-31 20:52:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-10-14 19:09:16 +00:00
|
|
|
|
2020-11-03 15:26:24 +00:00
|
|
|
void focusToNavbar(FocusScopeNode navigatorFocusNode) {
|
|
|
|
navigatorFocusNode.requestFocus();
|
2021-04-05 21:27:15 +00:00
|
|
|
navigatorFocusNode.focusInDirection(TraversalDirection
|
|
|
|
.down); // If player bar is hidden, focus won't be visible, so go down once more
|
2020-11-03 15:26:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-04-05 21:27:15 +00:00
|
|
|
FocusScopeNode navigationBarFocusNode =
|
|
|
|
FocusScopeNode(); // for bottom navigation bar
|
|
|
|
FocusNode screenFocusNode = FocusNode(); // for CustomNavigator
|
|
|
|
|
2020-10-31 20:52:23 +00:00
|
|
|
return RawKeyboardListener(
|
2020-11-22 10:17:37 +00:00
|
|
|
focusNode: FocusNode(),
|
|
|
|
onKey: _handleKey(navigationBarFocusNode, screenFocusNode),
|
2020-10-31 20:52:23 +00:00
|
|
|
child: Scaffold(
|
2021-09-01 12:38:32 +00:00
|
|
|
bottomNavigationBar: FocusScope(
|
|
|
|
node: navigationBarFocusNode,
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
PlayerBar(),
|
|
|
|
ValueListenableBuilder<int>(
|
|
|
|
valueListenable: _selected,
|
|
|
|
builder: (context, value, _) {
|
|
|
|
return BottomNavigationBar(
|
|
|
|
backgroundColor: Theme.of(context).bottomAppBarColor,
|
|
|
|
currentIndex: value,
|
|
|
|
onTap: (int s) async {
|
|
|
|
//Pop all routes until home screen
|
|
|
|
while (navigatorKey.currentState!.canPop()) {
|
|
|
|
await navigatorKey.currentState!.maybePop();
|
|
|
|
}
|
|
|
|
|
|
|
|
await navigatorKey.currentState!.maybePop();
|
|
|
|
_selected.value = s;
|
|
|
|
},
|
|
|
|
selectedItemColor: Theme.of(context).primaryColor,
|
|
|
|
items: <BottomNavigationBarItem>[
|
|
|
|
BottomNavigationBarItem(
|
|
|
|
icon: Icon(Icons.home), label: 'Home'.i18n),
|
|
|
|
BottomNavigationBarItem(
|
|
|
|
icon: Icon(Icons.search),
|
|
|
|
label: 'Search'.i18n,
|
|
|
|
),
|
|
|
|
BottomNavigationBarItem(
|
|
|
|
icon: Icon(Icons.library_music),
|
|
|
|
label: 'Library'.i18n)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
)),
|
|
|
|
body: _MainRouteNavigator(
|
|
|
|
navigatorKey: navigatorKey,
|
|
|
|
home: Focus(
|
|
|
|
focusNode: screenFocusNode,
|
|
|
|
skipTraversal: true,
|
|
|
|
canRequestFocus: false,
|
|
|
|
child: ValueListenableBuilder<int>(
|
|
|
|
valueListenable: _selected,
|
|
|
|
builder: (context, value, _) => _screens[value]))),
|
|
|
|
));
|
2020-06-23 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-30 12:51:51 +00:00
|
|
|
|
2021-09-01 12:38:32 +00:00
|
|
|
// hella simple null-safe reimplementation of custom_navigator, which is NOT null-safe
|
2021-08-30 12:51:51 +00:00
|
|
|
class _MainRouteNavigator extends StatelessWidget with WidgetsBindingObserver {
|
|
|
|
final Widget home;
|
|
|
|
final GlobalKey<NavigatorState> navigatorKey;
|
2021-09-01 12:38:32 +00:00
|
|
|
const _MainRouteNavigator(
|
|
|
|
{Key? key, required this.home, required this.navigatorKey})
|
2021-08-30 12:51:51 +00:00
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
// A system method that get invoked when user press back button on Android or back slide on iOS
|
|
|
|
@override
|
|
|
|
Future<bool> didPopRoute() async {
|
2021-09-01 12:38:32 +00:00
|
|
|
final NavigatorState? navigator = navigatorKey.currentState;
|
2021-08-30 12:51:51 +00:00
|
|
|
if (navigator == null) return false;
|
|
|
|
return await navigator.maybePop();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> didPushRoute(String route) async {
|
2021-09-01 12:38:32 +00:00
|
|
|
final NavigatorState? navigator = navigatorKey.currentState;
|
2021-08-30 12:51:51 +00:00
|
|
|
if (navigator == null) return false;
|
|
|
|
navigator.pushNamed(route);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Navigator(
|
2021-09-01 12:38:32 +00:00
|
|
|
key: navigatorKey,
|
2021-08-30 12:51:51 +00:00
|
|
|
initialRoute: Navigator.defaultRouteName,
|
|
|
|
onGenerateRoute: _onGenerateRoute,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-01 12:38:32 +00:00
|
|
|
Route<dynamic>? _onGenerateRoute(RouteSettings settings) {
|
2021-08-30 12:51:51 +00:00
|
|
|
if (settings.name == Navigator.defaultRouteName) {
|
|
|
|
return MaterialPageRoute(builder: (context) => home, settings: settings);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|