-
Notifications
You must be signed in to change notification settings - Fork 36
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
set limitation to select #9
Comments
Such feature is not supported yet, but we can work around that by undoing any changes that lead to selecting above the limit. Please take this example: class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const selectionLimit = 10;
final controller = DragSelectGridViewController();
Selection currentSelection;
@override
void initState() {
super.initState();
controller.addListener(handleSelectionChange);
}
@override
void dispose() {
controller.removeListener(handleSelectionChange);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: SelectionAppBar(
selection: controller.selection,
title: const Text('Grid Example'),
),
body: DragSelectGridView(
gridController: controller,
padding: const EdgeInsets.all(8),
itemCount: 90,
itemBuilder: (context, index, selected) {
return SelectableItem(
index: index,
color: Colors.blue,
selected: selected,
);
},
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 150,
crossAxisSpacing: 8,
mainAxisSpacing: 8,
),
),
);
}
void handleSelectionChange() {
if (controller.selection.amount > selectionLimit) {
setState(() => controller.selection = currentSelection);
} else {
setState(() {
currentSelection = Selection(
Set.of(controller.selection.selectedIndexes),
);
});
}
}
} |
You can set a maximum parameter to your Grid Widget like that :
Then in the
|
@Loopex2019 could you reformat your code? and |
No gridview is just an example. I mean it is the widget you created , which its build method return the |
@Loopex2019 I appreciate your suggestion, but it is not correct. Not calling The only way to unselect an item is through @MahdiPishguy Have you tried that? |
@hugocbpassos thanks for explaining to me . This actually worked for me. Because in the |
The thing is, the item is already selected when By the way, those callbacks ( Furthermore, for semantic purposes, the only information |
@hugocbpassos don't worry it's just a callback when the item gets selected to add my custom logic for adding the selected items inside a List variable. |
@Loopex2019 You should be doing that with |
Ah okay thanks for clarifying |
i test this source code which that is here #9 (comment) its work fine :) |
in this library how can i set limited selected items? for example user can't select more than 10 items?
The text was updated successfully, but these errors were encountered: