letsar / flutter_slidable

A Flutter implementation of slidable list item with directional slide actions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

jaydeepkaria opened this issue · comments

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

I am also facing same problem.

Same issue occurred, if anyone resolved please comment ans.

flutter_swipe_action_cell, i have you this library

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;
}
}

still relevant