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

How to close slidable on tap of anywhere on the screen outside Slidable widget #437

Open
jaydeepkaria opened this issue Sep 10, 2023 · 6 comments

Comments

@jaydeepkaria
Copy link

I want to close slidable even if click bottom tab or something on spp bar

@sharma-shubham12
Copy link

I am also facing same problem.

@AymericLeFeyer
Copy link

me too

@AniketDhanakFintaar
Copy link

Same issue occurred, if anyone resolved please comment ans.

@sharma-shubham12
Copy link

flutter_swipe_action_cell, i have you this library

@huykaiser
Copy link

this code work for me, you guys will like it:

import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
home: SlidableOwner(
child: Scaffold(
appBar: AppBar(
elevation: 0,
title: const Text('Slidable from outside'),
),
body: SlidableAutoCloseBehavior(
closeWhenOpened: true,
closeWhenTapped: true,
child: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return MyTile(index: index);
},
),
),
),
),
);
}
}

class MyText extends StatelessWidget {
final int index;

const MyText({
super.key,
required this.index,
});

@OverRide
Widget build(BuildContext context) {
return Slidable(
closeOnScroll: true,
endActionPane: const ActionPane(
dragDismissible: false,
motion: ScrollMotion(),
children: [
SlidableAction(
backgroundColor: Color(0xFFe0e0e0),
icon: Icons.remove_circle_outline_outlined,
autoClose: true,
onPressed: null,
)
],
),
child: SlidableOwnerTarget(
id: index,
child: Container(
padding: const EdgeInsets.all(24),
child: Text("HuyKaiser"),
),
),
);
}
}

class SlidableOwnerScope extends InheritedWidget {
final SlidableOwnerState state;

const SlidableOwnerScope({
super.key,
required super.child,
required this.state,
});

@OverRide
bool updateShouldNotify(SlidableOwnerScope oldWidget) {
return false;
}
}

class SlidableOwner extends StatefulWidget {
final Widget child;

const SlidableOwner({
super.key,
required this.child,
});

@OverRide
State createState() => SlidableOwnerState();

static SlidableOwnerState of(BuildContext context) {
return context.getInheritedWidgetOfExactType()!.state;
}
}

class SlidableOwnerState extends State {
final _controllers = <Object, SlidableController>{};

@OverRide
Widget build(BuildContext context) {
return SlidableOwnerScope(
state: this,
child: widget.child,
);
}

Future close(Object id) async {
final controller = _controllers[id];

if (controller == null) return;

return controller.close();

}

Future closeAll() async =>
await Future.wait(_controllers.values.map((e) => e.close()).toList());
}

class SlidableOwnerTarget extends StatefulWidget {
final Widget child;
final Object id;

const SlidableOwnerTarget({
super.key,
required this.child,
required this.id,
});

@OverRide
State createState() => _SlidableOwnerTargetState();
}

class _SlidableOwnerTargetState extends State {
late SlidableOwnerState _slidableOwnerState;

@OverRide
void didChangeDependencies() {
super.didChangeDependencies();

_slidableOwnerState = SlidableOwner.of(context);

_slidableOwnerState._controllers[widget.id] = Slidable.of(context)!;

}

@OverRide
void dispose() {
super.dispose();
_slidableOwnerState._controllers.remove(widget.id);
}

@OverRide
Widget build(BuildContext context) {
return widget.child;
}
}

@8thgencore
Copy link

still relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants