Skip to content

Commit

Permalink
upgrade to nullsafety
Browse files Browse the repository at this point in the history
  • Loading branch information
Hir0Pr0tag0nist committed Jul 9, 2021
1 parent 7fe6c15 commit 6da10bb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 122 deletions.
8 changes: 4 additions & 4 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -118,7 +118,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -153,7 +153,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -183,5 +183,5 @@ packages:
source: hosted
version: "1.6.3-nullsafety.0"
sdks:
dart: ">=2.12.0-0.0 <3.0.0"
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.20.0"
58 changes: 19 additions & 39 deletions lib/src/shimmer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Shimmer extends StatefulWidget {
final int loop;

Shimmer({
Key key,
@required this.child,
@required this.gradient,
Key? key,
required this.child,
required this.gradient,
this.direction = ShimmerDirection.ltr,
this.period = const Duration(milliseconds: 1500),
this.loop = 0,
Expand All @@ -62,30 +62,15 @@ class Shimmer extends StatefulWidget {
/// `highlightColor`.
///
Shimmer.fromColors(
{Key key,
@required this.child,
@required Color baseColor,
@required Color highlightColor,
{Key? key,
required this.child,
required Color baseColor,
required Color highlightColor,
this.period = const Duration(milliseconds: 1500),
this.direction = ShimmerDirection.ltr,
this.loop = 0})
: gradient = LinearGradient(
begin: Alignment.topLeft,
end: Alignment.centerRight,
colors: [
baseColor,
baseColor,
highlightColor,
baseColor,
baseColor
],
stops: [
0.0,
0.35,
0.5,
0.65,
1.0
]),
begin: Alignment.topLeft, end: Alignment.centerRight, colors: [baseColor, baseColor, highlightColor, baseColor, baseColor], stops: [0.0, 0.35, 0.5, 0.65, 1.0]),
super(key: key);

@override
Expand All @@ -94,22 +79,19 @@ class Shimmer extends StatefulWidget {
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(new DiagnosticsProperty<Gradient>('gradient', gradient,
defaultValue: null));
properties.add(new DiagnosticsProperty<Gradient>('gradient', gradient, defaultValue: null));
properties.add(new EnumProperty<ShimmerDirection>('direction', direction));
properties.add(new DiagnosticsProperty<Duration>('period', period,
defaultValue: null));
properties.add(new DiagnosticsProperty<Duration>('period', period, defaultValue: null));
}
}

class _ShimmerState extends State<Shimmer> with SingleTickerProviderStateMixin {
AnimationController _controller;
int _count;
late AnimationController _controller;
int _count = 0;

@override
void initState() {
super.initState();
_count = 0;
_controller = AnimationController(vsync: this, duration: widget.period)
..addListener(() {
setState(() {});
Expand Down Expand Up @@ -149,8 +131,7 @@ class _Shimmer extends SingleChildRenderObjectWidget {
final ShimmerDirection direction;
final Gradient gradient;

_Shimmer({Widget child, this.percent, this.direction, this.gradient})
: super(child: child);
_Shimmer({required Widget child, required this.percent, required this.direction, required this.gradient}) : super(child: child);

@override
_ShimmerFilter createRenderObject(BuildContext context) {
Expand All @@ -169,10 +150,9 @@ class _ShimmerFilter extends RenderProxyBox {
final Gradient _gradient;
final ShimmerDirection _direction;
double _percent;
Rect _rect;
late Rect _rect;

_ShimmerFilter(this._percent, this._direction, this._gradient)
: _gradientPaint = Paint()..blendMode = BlendMode.srcIn;
_ShimmerFilter(this._percent, this._direction, this._gradient) : _gradientPaint = Paint()..blendMode = BlendMode.srcIn;

@override
bool get alwaysNeedsCompositing => child != null;
Expand All @@ -189,8 +169,8 @@ class _ShimmerFilter extends RenderProxyBox {
if (child != null) {
assert(needsCompositing);

final width = child.size.width;
final height = child.size.height;
final width = child!.size.width;
final height = child!.size.height;
Rect rect;
double dx, dy;
if (_direction == ShimmerDirection.rtl) {
Expand All @@ -215,8 +195,8 @@ class _ShimmerFilter extends RenderProxyBox {
_rect = rect;
}

context.canvas.saveLayer(offset & child.size, _clearPaint);
context.paintChild(child, offset);
context.canvas.saveLayer(offset & child!.size, _clearPaint);
context.paintChild(child!, offset);
context.canvas.translate(dx, dy);
context.canvas.drawRect(rect, _gradientPaint);
context.canvas.restore();
Expand Down
119 changes: 46 additions & 73 deletions lib/src/slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import 'shimmer.dart';

class SliderButton extends StatefulWidget {
///To make button more customizable add your child widget
final Widget child;
final Widget? child;

///Sets the radius of corners of a button.
final double radius;
final double? radius;

///Use it to define a height and width of widget.
final double height;
final double width;
final double buttonSize;
final double? buttonSize;

///Use it to define a color of widget.
final Color backgroundColor;
Expand All @@ -22,13 +22,13 @@ class SliderButton extends StatefulWidget {
final Color buttonColor;

///Change it to gave a label on a widget of your choice.
final Text label;
final Text? label;

///Gives a alignment to a slider icon.
final Alignment alignLabel;
final BoxShadow boxShadow;
final Widget icon;
final Function action;
final BoxShadow? boxShadow;
final Widget? icon;
final Function? action;

///Make it false if you want to deactivate the shimmer effect.
final bool shimmer;
Expand All @@ -44,16 +44,17 @@ class SliderButton extends StatefulWidget {
final double dismissThresholds;

final bool disable;

SliderButton({
@required this.action,
required this.action,
this.radius,
this.boxShadow,
this.child,
this.vibrationFlag,
this.shimmer,
this.height,
this.vibrationFlag = false,
this.shimmer = true,
this.height = 70,
this.buttonSize,
this.width,
this.width = 250,
this.alignLabel = const Alignment(0.4, 0),
this.backgroundColor = const Color(0xffe0e0e0),
this.baseColor = Colors.black87,
Expand All @@ -64,14 +65,14 @@ class SliderButton extends StatefulWidget {
this.dismissible = true,
this.dismissThresholds = 1.0,
this.disable = false,
}) : assert((buttonSize ?? 60) <= (height ?? 70));
}) : assert((buttonSize ?? 60) <= (height));

@override
_SliderButtonState createState() => _SliderButtonState();
}

class _SliderButtonState extends State<SliderButton> {
bool flag;
late bool flag;

@override
void initState() {
Expand All @@ -91,24 +92,20 @@ class _SliderButtonState extends State<SliderButton> {
}

Widget _control() => Container(
height: widget.height ?? 70,
width: widget.width ?? 250,
decoration: BoxDecoration(
color:
widget.disable ? Colors.grey.shade700 : widget.backgroundColor,
borderRadius: BorderRadius.circular(widget.radius ?? 100)),
height: widget.height,
width: widget.width,
decoration: BoxDecoration(color: widget.disable ? Colors.grey.shade700 : widget.backgroundColor, borderRadius: BorderRadius.circular(widget.radius ?? 100)),
alignment: Alignment.centerLeft,
child: Stack(
alignment: Alignment.centerLeft,
children: <Widget>[
Container(
alignment: widget.alignLabel,
child: widget.shimmer ?? true && !widget.disable
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,
child: widget.label ?? Text(''),
)
: widget.label,
),
Expand All @@ -117,45 +114,32 @@ class _SliderButtonState extends State<SliderButton> {
verticalOffset: 50,
message: 'Button is disabled',
child: Container(
width: (widget.width ?? 250) - (widget.height ?? 70),
width: (widget.width) - (widget.height),
height: (widget.height - 70),
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(
left: widget.height == null
? (70 -
(widget.buttonSize == null
? 60
: widget.buttonSize)) /
2
: (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(
height:
widget.buttonSize ?? widget.height ?? 70 * 0.9,
width:
widget.buttonSize ?? widget.height ?? 70 * 0.9,
height: widget.buttonSize ?? widget.height,
width: widget.buttonSize ?? widget.height,
decoration: BoxDecoration(
boxShadow: [
widget.boxShadow,
],
boxShadow: widget.boxShadow != null
? [
widget.boxShadow!,
]
: null,
color: Colors.grey,
borderRadius: BorderRadius.circular(
widget.radius ?? 100)),
borderRadius: BorderRadius.circular(widget.radius ?? 100)),
child: Center(child: widget.icon),
),
),
)
: Dismissible(
key: Key("cancel"),
direction: DismissDirection.startToEnd,
dismissThresholds: {
DismissDirection.startToEnd: widget.dismissThresholds
},
dismissThresholds: {DismissDirection.startToEnd: widget.dismissThresholds},

///gives direction of swipping in argument.
onDismissed: (dir) async {
Expand All @@ -167,9 +151,9 @@ class _SliderButtonState extends State<SliderButton> {
}
});

widget.action();
if (widget.vibrationFlag ??
true && await Vibration.hasVibrator()) {
widget.action?.call();
final hasVibrator = await Vibration.hasVibrator() ?? false;
if (widget.vibrationFlag && hasVibrator) {
try {
Vibration.vibrate(duration: 200);
} catch (e) {
Expand All @@ -178,35 +162,24 @@ class _SliderButtonState extends State<SliderButton> {
}
},
child: Container(
width: (widget.width ?? 250) - (widget.height ?? 70),
height: widget.height ?? 70,
width: widget.width - widget.height,
height: widget.height,
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(
left: widget.height == null
? (70 -
(widget.buttonSize == null
? 60
: widget.buttonSize)) /
2
: (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(
height:
widget.buttonSize ?? widget.height ?? 70 * 0.9,
width:
widget.buttonSize ?? widget.height ?? 70 * 0.9,
height: widget.buttonSize ?? widget.height,
width: widget.buttonSize ?? widget.height,
decoration: BoxDecoration(
boxShadow: [
widget.boxShadow,
],
boxShadow: widget.boxShadow != null
? [
widget.boxShadow!,
]
: null,
color: widget.buttonColor,
borderRadius: BorderRadius.circular(
widget.radius ?? 100)),
borderRadius: BorderRadius.circular(widget.radius ?? 100)),
child: Center(child: widget.icon),
),
),
Expand Down
Loading

0 comments on commit 6da10bb

Please sign in to comment.