Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Scaffold.of(context) calls and update Scaffold constructors #4

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 20 additions & 32 deletions lib/contextual_scaffold.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:provider/provider.dart';

import 'controller/items_controller.dart';
Expand Down Expand Up @@ -75,38 +76,25 @@ class ContextualScaffold<T> extends StatelessWidget {
builder: (BuildContext context, Widget child) {
bool isActionModeEnable() =>
Provider.of<ItemsController<T>>(context).actionModeEnable;
return Stack(
children: [
Positioned.fill(
child: Scaffold(
appBar: appBar,
body: child,
floatingActionButton: floatingActionButton,
floatingActionButtonLocation: floatingActionButtonLocation,
floatingActionButtonAnimator: floatingActionButtonAnimator,
persistentFooterButtons: persistentFooterButtons,
drawer: drawer,
endDrawer: endDrawer,
bottomNavigationBar: bottomNavigationBar,
bottomSheet: bottomSheet,
backgroundColor: backgroundColor,
resizeToAvoidBottomInset: resizeToAvoidBottomInset,
primary: primary,
drawerDragStartBehavior: drawerDragStartBehavior,
extendBody: extendBody,
extendBodyBehindAppBar: extendBody,
drawerScrimColor: drawerScrimColor,
drawerEdgeDragWidth: drawerEdgeDragWidth,
),
),
if (isActionModeEnable())
Positioned(
top: 0,
right: 0,
left: 0,
child: contextualAppBar,
),
],
return Scaffold(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a reason the contextual appbar was stacked. The reason being that you can have multiple contextual appbar. Run the "Whatsapp" sample code to see what i mean here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. So to avoid the issue I described, "where ContextualActionBar was calling Scaffold.of(context) (contextual_action_bar.dart line 216) but since it was a sibling in the Stack, it could not find the correct Scaffold", it looks like you wrap everything in another Scaffold? I wonder if there is a way to avoid wrapping it with another Scaffold.

appBar: isActionModeEnable() ? contextualAppBar : appBar,
body: child,
floatingActionButton: floatingActionButton,
floatingActionButtonLocation: floatingActionButtonLocation,
floatingActionButtonAnimator: floatingActionButtonAnimator,
persistentFooterButtons: persistentFooterButtons,
drawer: drawer,
endDrawer: endDrawer,
bottomNavigationBar: bottomNavigationBar,
bottomSheet: bottomSheet,
backgroundColor: backgroundColor,
resizeToAvoidBottomInset: resizeToAvoidBottomInset,
primary: primary,
drawerDragStartBehavior: drawerDragStartBehavior,
extendBody: extendBody,
extendBodyBehindAppBar: extendBody,
drawerScrimColor: drawerScrimColor,
drawerEdgeDragWidth: drawerEdgeDragWidth,
);
},
child: body,
Expand Down
5 changes: 5 additions & 0 deletions lib/sliver/contextual_scrollview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,11 @@ class _NestedScrollPosition extends ScrollPosition
_parent?.detach(this);
super.dispose();
}

@override
void pointerScroll(double delta) {
// TODO: implement pointerScroll
}
}

enum _NestedBallisticScrollActivityMode { outer, inner, independent }
Expand Down
17 changes: 13 additions & 4 deletions lib/widgets/contextual_action_bar.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'package:contextualactionbar/actions/action_mode.dart';
import 'package:contextualactionbar/widgets/contextual_close_action.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

import '../contextualactionbar.dart';
import 'contextual_action.dart';
import '../controller/items_controller.dart';
import '../typedef/counter_builder.dart';
Expand All @@ -14,6 +16,7 @@ class ContextualAppBar<T> extends StatefulWidget
Key key,
@required this.counterBuilder,
@required this.contextualActions,
this.leading,
this.flexibleSpace,
this.bottom,
this.elevation,
Expand Down Expand Up @@ -51,6 +54,9 @@ class ContextualAppBar<T> extends StatefulWidget
/// last action.
final List<ContextualAction<T>> contextualActions;

/// Action to take on cancel
final ContextualCloseAction<T> leading;

/// This widget is stacked behind the toolbar and the tab bar. It's height will
/// be the same as the app bar's overall height.
///
Expand Down Expand Up @@ -207,7 +213,7 @@ class _ContextualAppBarState<T> extends State<ContextualAppBar> {
assert(debugCheckHasMaterialLocalizations(context));
final ThemeData theme = Theme.of(context);
final AppBarTheme appBarTheme = AppBarTheme.of(context);
final ScaffoldState scaffold = Scaffold.of(context, nullOk: true);
final ScaffoldState scaffold = Scaffold.of(context);

final bool hasEndDrawer = scaffold?.hasEndDrawer ?? false;

Expand Down Expand Up @@ -264,9 +270,12 @@ class _ContextualAppBarState<T> extends State<ContextualAppBar> {
}

final Widget toolbar = NavigationToolbar(
leading: IconButton(
icon: Icon(widget.closeIcon ?? Icons.close),
onPressed: () => ActionMode.disable<T>(context)),
leading: widget.leading ??
IconButton(
icon: Icon(widget.closeIcon ?? Icons.close),
onPressed: () {
ActionMode.disable<T>(context);
}),
middle: Consumer<ItemsController<T>>(
builder:
(BuildContext context, ItemsController<T> value, Widget child) {
Expand Down
33 changes: 33 additions & 0 deletions lib/widgets/contextual_close_action.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:contextualactionbar/actions/action_mode.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../controller/items_controller.dart';
import '../typedef/handle_items.dart';

class ContextualCloseAction<T> extends StatelessWidget {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the usecase for this ContextualCloseAction?. The material design guideline doesn't have this specification. Personally i will prefer a callback to handle this or better still use
ContextualAction instead of create an entirely new type of widget.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So initially I tried to just pass HandleItems<T>, but the type was getting erased (sorry, not an expert in Dart but that appears to be what was happening). Wrapping it in ContextualAction<T> got rid of that issue, but if you know of a way to prevent the type from being erased, definitely would like to go that route. The use case for this is simply to do an action on the list of selected items when the close button is selected, just like a ContextualAction does.

final HandleItems<T> itemsHandler;
final IconData closeIcon;
const ContextualCloseAction(
{Key key, this.itemsHandler, this.closeIcon})
: super(key: key);
@override
Widget build(BuildContext context) {
return Consumer<ItemsController<T>>(
builder: (BuildContext context, ItemsController<T> value, Widget child) {
return IconButton(
icon: Icon(closeIcon ?? Icons.close),
onPressed: () {
if (itemsHandler != null) {
itemsHandler(value.items);
}
ActionMode.disable<T>(context);
});
},
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Container(),
),
);
}
}