fixed WillPopScope

This commit is contained in:
Moritz Weber 2020-04-12 12:34:19 +02:00
parent d8e7dd2f17
commit 9609a89c23
6 changed files with 128 additions and 36 deletions

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'presentation/pages/currently_playing.dart'; import 'presentation/pages/currently_playing.dart';
@ -8,6 +9,7 @@ import 'presentation/pages/library_page.dart';
import 'presentation/pages/settings_page.dart'; import 'presentation/pages/settings_page.dart';
import 'presentation/state/audio_store.dart'; import 'presentation/state/audio_store.dart';
import 'presentation/state/music_data_store.dart'; import 'presentation/state/music_data_store.dart';
import 'presentation/state/navigation_store.dart';
import 'presentation/theming.dart'; import 'presentation/theming.dart';
import 'presentation/widgets/audio_service_widget.dart'; import 'presentation/widgets/audio_service_widget.dart';
import 'presentation/widgets/injection_widget.dart'; import 'presentation/widgets/injection_widget.dart';
@ -25,14 +27,14 @@ class MyApp extends StatelessWidget {
return InjectionWidget( return InjectionWidget(
child: AudioServiceWidget( child: AudioServiceWidget(
child: MaterialApp( child: MaterialApp(
title: 'mucke', title: 'mucke',
theme: theme(), theme: theme(),
initialRoute: '/', initialRoute: '/',
routes: { routes: {
'/': (context) => const RootPage(), '/': (context) => const RootPage(),
'/playing': (context) => const CurrentlyPlayingPage(), '/playing': (context) => const CurrentlyPlayingPage(),
}, },
), ),
), ),
); );
@ -47,8 +49,6 @@ class RootPage extends StatefulWidget {
} }
class _RootPageState extends State<RootPage> { class _RootPageState extends State<RootPage> {
var navIndex = 1;
final List<Widget> _pages = <Widget>[ final List<Widget> _pages = <Widget>[
const HomePage(), const HomePage(),
const LibraryPage( const LibraryPage(
@ -79,19 +79,19 @@ class _RootPageState extends State<RootPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final NavigationStore navStore = Provider.of<NavigationStore>(context);
print('RootPage.build'); print('RootPage.build');
return Scaffold( return Observer(
body: IndexedStack( builder: (BuildContext context) => Scaffold(
index: navIndex, body: IndexedStack(
children: _pages, index: navStore.navIndex,
), children: _pages,
bottomNavigationBar: NavBar( ),
onTap: (int index) { bottomNavigationBar: NavBar(
setState(() { onTap: (int index) => navStore.setNavIndex(index),
navIndex = index; currentIndex: navStore.navIndex,
}); ),
},
currentIndex: navIndex,
), ),
); );
} }

View file

@ -1,5 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../state/navigation_store.dart';
import 'library_tab_container.dart'; import 'library_tab_container.dart';
class LibraryPage extends StatelessWidget { class LibraryPage extends StatelessWidget {
@ -10,6 +12,8 @@ class LibraryPage extends StatelessWidget {
print('LibraryPage.build'); print('LibraryPage.build');
final GlobalKey<NavigatorState> nav = GlobalKey(); final GlobalKey<NavigatorState> nav = GlobalKey();
final NavigationStore navStore = Provider.of<NavigationStore>(context);
return WillPopScope( return WillPopScope(
child: Navigator( child: Navigator(
key: nav, key: nav,
@ -27,8 +31,10 @@ class LibraryPage extends StatelessWidget {
}, },
), ),
onWillPop: () async { onWillPop: () async {
print('onWillPop'); if (navStore.navIndex == 1) {
return !await nav.currentState.maybePop(); return !await nav.currentState.maybePop();
}
return Future.value(true);
}, },
); );
} }

View file

@ -0,0 +1,28 @@
import 'package:mobx/mobx.dart';
part 'navigation_store.g.dart';
class NavigationStore extends _NavigationStore with _$NavigationStore {
NavigationStore() : super();
}
abstract class _NavigationStore with Store {
_NavigationStore();
bool _initialized = false;
@observable int navIndex = 1;
@action
Future<void> init() async {
if (!_initialized) {
print('NavigationStore.init');
_initialized = true;
}
}
@action
void setNavIndex(int i) {
navIndex = i;
}
}

View file

@ -0,0 +1,54 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'navigation_store.dart';
// **************************************************************************
// StoreGenerator
// **************************************************************************
// ignore_for_file: non_constant_identifier_names, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic
mixin _$NavigationStore on _NavigationStore, Store {
final _$navIndexAtom = Atom(name: '_NavigationStore.navIndex');
@override
int get navIndex {
_$navIndexAtom.context.enforceReadPolicy(_$navIndexAtom);
_$navIndexAtom.reportObserved();
return super.navIndex;
}
@override
set navIndex(int value) {
_$navIndexAtom.context.conditionallyRunInAction(() {
super.navIndex = value;
_$navIndexAtom.reportChanged();
}, _$navIndexAtom, name: '${_$navIndexAtom.name}_set');
}
final _$initAsyncAction = AsyncAction('init');
@override
Future<void> init() {
return _$initAsyncAction.run(() => super.init());
}
final _$_NavigationStoreActionController =
ActionController(name: '_NavigationStore');
@override
void setNavIndex(int i) {
final _$actionInfo = _$_NavigationStoreActionController.startAction();
try {
return super.setNavIndex(i);
} finally {
_$_NavigationStoreActionController.endAction(_$actionInfo);
}
}
@override
String toString() {
final string = 'navIndex: ${navIndex.toString()}';
return '{$string}';
}
}

View file

@ -11,6 +11,7 @@ import '../../system/repositories/audio_repository_impl.dart';
import '../../system/repositories/music_data_repository_impl.dart'; import '../../system/repositories/music_data_repository_impl.dart';
import '../state/audio_store.dart'; import '../state/audio_store.dart';
import '../state/music_data_store.dart'; import '../state/music_data_store.dart';
import '../state/navigation_store.dart';
class InjectionWidget extends StatelessWidget { class InjectionWidget extends StatelessWidget {
const InjectionWidget({Key key, this.child}) : super(key: key); const InjectionWidget({Key key, this.child}) : super(key: key);
@ -44,6 +45,9 @@ class InjectionWidget extends StatelessWidget {
audioRepository: audioRepository, audioRepository: audioRepository,
), ),
), ),
Provider<NavigationStore>(
create: (BuildContext context) => NavigationStore(),
),
], ],
); );
} }

View file

@ -28,21 +28,21 @@ packages:
name: archive name: archive
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.11" version: "2.0.13"
args: args:
dependency: transitive dependency: transitive
description: description:
name: args name: args
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.5.2" version: "1.6.0"
async: async:
dependency: transitive dependency: transitive
description: description:
name: async name: async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.4.0" version: "2.4.1"
audio_service: audio_service:
dependency: "direct main" dependency: "direct main"
description: description:
@ -56,7 +56,7 @@ packages:
name: boolean_selector name: boolean_selector
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.5" version: "2.0.0"
build: build:
dependency: transitive dependency: transitive
description: description:
@ -119,7 +119,7 @@ packages:
name: charcode name: charcode
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.2" version: "1.1.3"
checked_yaml: checked_yaml:
dependency: transitive dependency: transitive
description: description:
@ -147,7 +147,7 @@ packages:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.14.11" version: "1.14.12"
convert: convert:
dependency: transitive dependency: transitive
description: description:
@ -161,7 +161,7 @@ packages:
name: crypto name: crypto
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.3" version: "2.1.4"
csslib: csslib:
dependency: transitive dependency: transitive
description: description:
@ -288,7 +288,7 @@ packages:
name: image name: image
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.4" version: "2.1.12"
io: io:
dependency: transitive dependency: transitive
description: description:
@ -512,7 +512,7 @@ packages:
name: quiver name: quiver
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.5" version: "2.1.3"
recase: recase:
dependency: transitive dependency: transitive
description: description:
@ -559,7 +559,7 @@ packages:
name: source_span name: source_span
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.5.5" version: "1.7.0"
sqlparser: sqlparser:
dependency: transitive dependency: transitive
description: description:
@ -615,7 +615,7 @@ packages:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.11" version: "0.2.15"
timing: timing:
dependency: transitive dependency: transitive
description: description:
@ -664,7 +664,7 @@ packages:
name: xml name: xml
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "3.5.0" version: "3.6.1"
yaml: yaml:
dependency: transitive dependency: transitive
description: description: