Skip to content

Commit

Permalink
-changed ontap callback to Future<bool>
Browse files Browse the repository at this point in the history
-bugs resolved
  • Loading branch information
anirudhsharma committed Jan 13, 2024
1 parent 46ae808 commit b2a6736
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 83 deletions.
71 changes: 22 additions & 49 deletions example/lib/main.dart
@@ -1,3 +1,4 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:slider_button/slider_button.dart';

Expand All @@ -17,56 +18,38 @@ class MainApp extends StatelessWidget {
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
SliderButton(
action: () {
///Do something here OnSlide
print("working");
Center(
child: SliderButton(
action: () async {
///Do something here
return false;
},

///Put label over here
label: Text(
"Slide to cancel !",
"Slide to cancel Event",
style: TextStyle(
color: Color(0xff4a4a4a),
fontWeight: FontWeight.w500,
fontSize: 17),
),
icon: Center(
child: Icon(
Icons.power_settings_new,
color: Colors.white,
size: 40.0,
CupertinoIcons.power,
color: Colors.redAccent,
size: 30.0,
semanticLabel: 'Text to announce in accessibility modes',
)),

//Put BoxShadow here
boxShadow: BoxShadow(
color: Colors.black,
color: Colors.black.withOpacity(0.3),
blurRadius: 4,
),

//Adjust effects such as shimmer and flag vibration here
shimmer: true,
vibrationFlag: true,

///Change All the color and size from here.
width: 230,
radius: 10,
buttonColor: Color(0xffd60000),
backgroundColor: Color(0xff534bae),
highlightedColor: Colors.white,
baseColor: Colors.red,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("Example of a wide button"),
),
)),
SliderButton(
action: () {
action: () async {
///Do something here OnSlide
print("working");
return false;
},

///Put label over here
Expand All @@ -77,23 +60,13 @@ class MainApp extends StatelessWidget {
fontWeight: FontWeight.w500,
fontSize: 17),
),
buttonWidth: 84,
height: 44,
buttonSize: 44,
child: Container(
width: 80,
margin: EdgeInsets.all(2),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10),
),
child: Icon(
Icons.power_settings_new,
color: Colors.white,
size: 40.0,
semanticLabel: 'Text to announce in accessibility modes',
),
),
icon: Center(
child: Icon(
CupertinoIcons.power,
color: Colors.white,
size: 40.0,
semanticLabel: 'Text to announce in accessibility modes',
)),

//Put BoxShadow here
boxShadow: BoxShadow(
Expand Down
76 changes: 42 additions & 34 deletions lib/src/slider.dart
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';
import 'package:vibration/vibration.dart';


class SliderButton extends StatefulWidget {
///To make button more customizable add your child widget
final Widget? child;
Expand Down Expand Up @@ -31,14 +30,11 @@ class SliderButton extends StatefulWidget {
final Alignment alignLabel;
final BoxShadow? boxShadow;
final Widget? icon;
final Function action;
final Future<bool?> Function() action;

///Make it false if you want to deactivate the shimmer effect.
final bool shimmer;

///Make it false if you want maintain the widget in the tree.
final bool dismissible;

final bool vibrationFlag;

///The offset threshold the item has to be dragged in order to be considered
Expand All @@ -50,23 +46,22 @@ class SliderButton extends StatefulWidget {

SliderButton({
required this.action,
this.radius=100,
this.radius = 100,
this.boxShadow,
this.child,
this.vibrationFlag = false,
this.shimmer = true,
this.height = 70,
this.buttonSize,
this.buttonWidth,
this.width = 250,
this.width = 270,
this.alignLabel = const Alignment(0.6, 0),
this.backgroundColor = const Color(0xffe0e0e0),
this.baseColor = Colors.black87,
this.buttonColor = Colors.white,
this.highlightedColor = Colors.white,
this.label,
this.icon,
this.dismissible = true,
this.dismissThresholds = 0.75,
this.disable = false,
}) : assert((buttonSize ?? 60) <= (height));
Expand All @@ -86,28 +81,26 @@ class _SliderButtonState extends State<SliderButton> {

@override
Widget build(BuildContext context) {

return flag == true
? _control()
: widget.dismissible == true
? Container()
: _control();
return flag == true ? _control() : Container();
}

Widget _control() => Container(
height: widget.height,
width: widget.width,
decoration: BoxDecoration(color: widget.disable ? Colors.grey.shade700 : widget.backgroundColor, borderRadius: BorderRadius.circular(widget.radius )),
decoration: BoxDecoration(
color:
widget.disable ? Colors.grey.shade700 : widget.backgroundColor,
borderRadius: BorderRadius.circular(widget.radius)),
alignment: Alignment.centerLeft,
child: Stack(
alignment: Alignment.centerLeft,
children: <Widget>[
Container(

alignment: widget.alignLabel,
child: widget.shimmer && !widget.disable
? Shimmer.fromColors(
baseColor: widget.disable ? Colors.grey : widget.baseColor,
baseColor:
widget.disable ? Colors.grey : widget.baseColor,
highlightColor: widget.highlightedColor,
child: widget.label ?? Text(''),
)
Expand All @@ -122,7 +115,11 @@ class _SliderButtonState extends State<SliderButton> {
height: (widget.height - 70),
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(
left: (widget.height - (widget.buttonSize == null ? widget.height * 0.9 : widget.buttonSize)!) / 2,
left: (widget.height -
(widget.buttonSize == null
? widget.height * 0.9
: widget.buttonSize)!) /
2,
),
child: widget.child ??
Container(
Expand All @@ -135,42 +132,53 @@ class _SliderButtonState extends State<SliderButton> {
]
: null,
color: Colors.grey,
borderRadius: BorderRadius.circular(widget.radius )),
borderRadius:
BorderRadius.circular(widget.radius)),
child: Center(child: widget.icon),
),
),
)
: Dismissible(
key: UniqueKey(),
direction: DismissDirection.startToEnd,
dismissThresholds: {DismissDirection.startToEnd: widget.dismissThresholds},
dismissThresholds: {
DismissDirection.startToEnd: widget.dismissThresholds
},
confirmDismiss: (_) async {
bool result;
try {
result = (await widget.action()) ?? true;
} catch (e) {
result = false;
}

///gives direction of swiping in argument.
onDismissed: (dir) async {
///break the action
if (!result) return false;
setState(() {
if (widget.dismissible) {
flag = false;
} else {
flag = !flag;
}
flag = !flag;
});

widget.action();
final hasVibrator = await Vibration.hasVibrator() ?? false;
final hasVibrator =
await Vibration.hasVibrator() ?? false;
if (widget.vibrationFlag && hasVibrator) {
try {
Vibration.vibrate(duration: 200);
} catch (e) {
print(e);
}
}
return true;
},
child: Container(
width: widget.width - (widget.buttonWidth ?? widget.height),
width:
widget.width - (widget.buttonWidth ?? widget.height),
height: widget.height,
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(
left: (widget.height - (widget.buttonSize == null ? widget.height : widget.buttonSize!)) / 2,
left: (widget.height -
(widget.buttonSize == null
? widget.height
: widget.buttonSize!)) /
2,
),
child: widget.child ??
Container(
Expand All @@ -183,12 +191,12 @@ class _SliderButtonState extends State<SliderButton> {
]
: null,
color: widget.buttonColor,
borderRadius: BorderRadius.circular(widget.radius )),
borderRadius:
BorderRadius.circular(widget.radius)),
child: Center(child: widget.icon),
),
),
),

],
),
);
Expand Down

0 comments on commit b2a6736

Please sign in to comment.