Open diablodale opened 5 years ago
Created a pull request for this issue: #36613
@goderbauer and I chatted offline about making this change and we have closed the associated PR. Using a Dismissible widget should be dismissible, and making the direction nullable creates a weird api without a clear intention. If you would like to simplify code to 'enable/disable' the Dismissible, you can easily create a wrapper widget that handles that for you. :) I'm going to close this issue then, feel free to discuss further if you think the assert is incorrect.
I disagree with your assessment. However without the above conversation being public, there is no way that I can participate in it.
I also disagree and would also have appreciated it if the discussion happened on GitHub.
Glad to hear! The discussion actually took place on the PR: #48921 The extent of our offline chat was to just close the issue given the comments on the PR. There are a number of concerns around removing the assert on the direction, as it introduces the opportunity for exceptions to fire elsewhere that rely on that assertion. Feel free to discuss further! :)
Based on the PR feedback, I'm going to re-open this and re-name it for the newly proposed api change. Feel free to continue discussing here and thanks for the input! :)
Hi,
Why not just add a DismissDirection.none ?
Regards,
Regarding the code duplication issue mentioned earlier, here's a simple technique that can be used as an alternative:
class ExampleWidget extends StatelessWidget {
final DismissDirectionCallback? onDismissed;
final String text;
const ExampleWidget({
required this.text,
this.onDismissed,
super.key,
});
@override
Widget build(BuildContext context) {
// Define the core widget
var item = Container(
padding: const EdgeInsets.all(10),
color: Colors.blue,
child: Text(text, style: const TextStyle(color: Colors.black)),
);
// Conditionally wrap in Dismissible
if (onDismissed != null) {
return Dismissible(
key: ObjectKey(text),
onDismissed: onDismissed,
child: item,
);
}
return item;
}
}
This approach demonstrates how to avoid duplicating widget code when conditionally applying
a Dismissible
wrapper. By assigning the core widget to a variable first, we can easily return
either the wrapped or unwrapped version without repeating the widget's definition.
While this doesn't solve the original issue, I think it is a somewhat neat and clean way to achieve effectively the same thing.
If anyone knows of any reasons why this approach should not be used (e.g., performance issues, potential bugs this might cause), please feel free to correct me.
@HelgeSverre thanks for the interesting example. I don't have a current Flutter project to experiment with it. The issue seems to be one that appears more than once, and your example may be workaround for the ongoing core gap in feature/functionality.
Attempt to disable the Dismissible() behavior by setting
.direction = null
causes assert at https://github.com/flutter/flutter/blob/6d134e0c866ce90a4dc7a02af6a8f1079c2a4fdc/packages/flutter/lib/src/widgets/dismissible.dart#L337This is a feature enhancement to simplify customer code, not a code fault.
Setup
Flutter version 0.11.9 Framework revision d48e6e433c (5 days ago), 2018-11-20 22:05:23 -0500 Engine revision 5c8147450d Dart version 2.1.0 (build 2.1.0-dev.9.4 f9ebf21297) Platform android-27, build-tools 27.0.3
Repo Scenario
It would be desired to use the
Dismissible.direction
field to set the means to be dismissed. With flutter v0.11.9, we can set that field to any choice ofDismissDirection
but unfortunately not tonull
. Naturally,null
as a.direction
would indicate there is no direction in which it can be dismissed...making the Dismissible a noop.Support for
null
as a value toDismissible.direction
will simply customer code. Rather than using a condition expression and duplicating the child widget code, it can more easily be done by just setting thedirection
to null.Today's unhappy duplicate code π
Future happy simplified code π
Log of assert
Doctor