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 all 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
79 changes: 42 additions & 37 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 All @@ -10,6 +11,10 @@ class ContextualScaffold<T> extends StatelessWidget {

final bool extendBodyBehindAppBar;

final bool externalProvider;

final bool allowZeroItems;

final PreferredSizeWidget appBar;
final ContextualAppBar<T> contextualAppBar;

Expand Down Expand Up @@ -45,6 +50,8 @@ class ContextualScaffold<T> extends StatelessWidget {
Key key,
this.appBar,
@required this.contextualAppBar,
this.externalProvider = false,
this.allowZeroItems = false,
this.body,
this.floatingActionButton,
this.floatingActionButtonLocation,
Expand All @@ -70,43 +77,41 @@ class ContextualScaffold<T> extends StatelessWidget {
super(key: key);
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<ItemsController<T>>(
create: (BuildContext context) => ItemsController<T>(),
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,
),
],
if (externalProvider) {
return buildScaffold(context);
} else {
return ChangeNotifierProvider<ItemsController<T>>(
create: (BuildContext context) => ItemsController<T>(allowZeroItems: allowZeroItems),
builder: (BuildContext context, Widget child) {
return buildScaffold(context);
}
);
}
}

Widget buildScaffold(BuildContext context) {
return Consumer<ItemsController<T>>(
builder: (context, itemsController, child) {
bool isActionModeEnable() => itemsController.actionModeEnable;
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
17 changes: 16 additions & 1 deletion lib/controller/items_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class ItemsController<T> extends ChangeNotifier {

List<T> get items => _items.toList();

final bool allowZeroItems;

ItemsController({this.allowZeroItems = false});

bool isItemPresent(T item) => _items.contains(item);
StreamController<bool> _isActionModeEnableController =
StreamController.broadcast();
Expand All @@ -34,6 +38,17 @@ class ItemsController<T> extends ChangeNotifier {
_modifyActionMode(true);
}

void enableActionModeList(Iterable<T> items) {
_items.addAll(items);
_modifyActionMode(true);
}

void enableActionModeZeroItems() {
if (allowZeroItems) {
_modifyActionMode(true);
}
}

void disableActionMode() {
_modifyActionMode(false);
emptySelection();
Expand All @@ -56,7 +71,7 @@ class ItemsController<T> extends ChangeNotifier {
if (!_actionModeEnabled) {
_modifyActionMode(true);
}
if (_items.isEmpty) {
if (_items.isEmpty && !allowZeroItems) {
disableActionMode();
}
notifyListeners();
Expand Down
10 changes: 9 additions & 1 deletion lib/sliver/contextual_scrollview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ContextualScrollView<T> extends StatefulWidget {
this.scrollDirection = Axis.vertical,
this.reverse = false,
this.physics,
this.allowZeroItems = false,
@required this.headerSliverBuilder,
@required this.body,
@required this.contextualAppBar,
Expand Down Expand Up @@ -60,6 +61,8 @@ class ContextualScrollView<T> extends StatefulWidget {
/// Defaults to false.
final bool reverse;

final bool allowZeroItems;

/// How the scroll view should respond to user input.
///
/// For example, determines how the scroll view continues to animate after the
Expand Down Expand Up @@ -277,7 +280,7 @@ class ContextualScrollViewState<T> extends State<ContextualScrollView> {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<ItemsController<T>>(
create: (BuildContext context) => ItemsController<T>(),
create: (BuildContext context) => ItemsController<T>(allowZeroItems: widget.allowZeroItems),
child: Builder(builder: (context) {
bool isActionModeEnable() =>
Provider.of<ItemsController<T>>(context).actionModeEnable;
Expand Down Expand Up @@ -1200,6 +1203,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(),
),
);
}
}