Skip to content

Commit

Permalink
Changed MorpheusTabView behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
Salby committed Sep 2, 2019
1 parent f839bc4 commit 750188b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 45 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.2.1] - 2019-09-02

### Changed
- `MorpheusTabView` only transitions when a new child is provided.

## [1.2.0+6] - 2019-08-30

### Fixes
Expand Down
56 changes: 26 additions & 30 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';

import 'package:morpheus/morpheus.dart';

void main() => runApp(MyApp());
Expand Down Expand Up @@ -55,42 +54,25 @@ class MyApp extends StatelessWidget {
}
}

class HomeScreen extends StatelessWidget {
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
final profileKey = GlobalKey();
final settingsKey = GlobalKey();
final createKey = GlobalKey();
int _currentIndex = 0;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: ListView(
children: <Widget>[
ListTile(
key: profileKey,
title: Text('Profile'),
onTap: () => Navigator.of(context).pushNamed(
'/profile',
arguments: MorpheusRouteArguments(
parentKey: profileKey,
),
),
),
Divider(height: 1.0),
ListTile(
key: settingsKey,
title: Text('Settings'),
onTap: () => Navigator.of(context).pushNamed(
'/settings',
arguments: MorpheusRouteArguments(
parentKey: settingsKey,
),
),
),
Divider(height: 1.0),
],
body: MorpheusTabView(
child: <Widget>[
ProfileScreen(),
SettingsScreen(),
][_currentIndex],
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: FloatingActionButton.extended(
Expand All @@ -106,6 +88,20 @@ class HomeScreen extends StatelessWidget {
),
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (index) => setState(() => _currentIndex = index),
items: [
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
title: Text('Profile'),
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: Text('Settings'),
),
],
),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.2"
version: "1.2.0+6"
path:
dependency: transitive
description:
Expand Down
51 changes: 44 additions & 7 deletions lib/widgets/morpheus_tab_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,52 @@ class _MorpheusTabViewState extends AnimatedWidgetBaseState<MorpheusTabView> {
_TopLevelScaleTween _scaleTween;
_TopLevelChildTween _childTween;

/// Change the value of this property after the initial build to make sure
/// that the transition isn't built when the user hasn't changed screens.
bool _initialBuild = true;

/// Used to compare with the new [widget.child] to determine whether to
/// animate a change or not.
Widget _oldWidget;

/// Returns true if [widget.child] is different from [_oldWidget].
bool get _isNewWidget => !Widget.canUpdate(_oldWidget, widget.child);

@override
void initState() {
super.initState();

// Update [_oldWidget] when the animation ends.
controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
setState(() => _oldWidget = widget.child);

// Mark [_initialBuild] as false after the first screen has appeared.
if (_initialBuild) {
_initialBuild = false;
}
}
});
}

/// Builds the top-level transition.
///
/// The transition is only built when [widget.child] changes as defined by
/// [_isNewWidget] and if [_initialBuild] is false.
@override
Widget build(BuildContext context) {
return Opacity(
opacity: _opacityTween.evaluate(animation),
child: Transform.scale(
scale: _scaleTween.evaluate(animation),
child: _childTween.evaluate(animation),
),
);
// Build transition if a new widget has been supplied.
if (_isNewWidget && !_initialBuild) {
return Opacity(
opacity: _opacityTween.evaluate(animation),
child: Transform.scale(
scale: _scaleTween.evaluate(animation),
child: _childTween.evaluate(animation),
),
);
} else {
return widget.child;
}
}

@override
Expand Down
12 changes: 6 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.2.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -73,14 +73,14 @@ packages:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
version: "1.7.0"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "2.0.3"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -127,7 +127,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.4"
version: "0.2.5"
typed_data:
dependency: transitive
description:
Expand All @@ -143,4 +143,4 @@ packages:
source: hosted
version: "2.0.8"
sdks:
dart: ">=2.2.0 <3.0.0"
dart: ">=2.2.2 <3.0.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: morpheus
description: A Flutter package for easily implementing Material Design navigation transitions.
version: 1.2.0+6
version: 1.2.1
author: Sander Dalby Larsen <[email protected]>
homepage: https://github.com/salby/morpheus

Expand Down

0 comments on commit 750188b

Please sign in to comment.