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

View file

@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../state/navigation_store.dart';
import 'library_tab_container.dart';
class LibraryPage extends StatelessWidget {
@ -10,6 +12,8 @@ class LibraryPage extends StatelessWidget {
print('LibraryPage.build');
final GlobalKey<NavigatorState> nav = GlobalKey();
final NavigationStore navStore = Provider.of<NavigationStore>(context);
return WillPopScope(
child: Navigator(
key: nav,
@ -27,8 +31,10 @@ class LibraryPage extends StatelessWidget {
},
),
onWillPop: () async {
print('onWillPop');
return !await nav.currentState.maybePop();
if (navStore.navIndex == 1) {
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 '../state/audio_store.dart';
import '../state/music_data_store.dart';
import '../state/navigation_store.dart';
class InjectionWidget extends StatelessWidget {
const InjectionWidget({Key key, this.child}) : super(key: key);
@ -44,6 +45,9 @@ class InjectionWidget extends StatelessWidget {
audioRepository: audioRepository,
),
),
Provider<NavigationStore>(
create: (BuildContext context) => NavigationStore(),
),
],
);
}

View file

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