mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-01 20:43:20 +08:00
PurchaseSlider: Make it prettier
It now appears as a triangle which is getting filled up. It no longer lets you click on it, but we have buttons on the side for that.
This commit is contained in:
@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
@ -13,7 +15,7 @@ class PaymentInfo extends Equatable {
|
|||||||
List<Object> get props => [value, text, id];
|
List<Object> get props => [value, text, id];
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef PaymentSliderChanged = Function(PaymentInfo);
|
typedef PaymentSliderChanged = void Function(PaymentInfo);
|
||||||
|
|
||||||
class PurchaseSlider extends StatelessWidget {
|
class PurchaseSlider extends StatelessWidget {
|
||||||
final List<PaymentInfo> values;
|
final List<PaymentInfo> values;
|
||||||
@ -30,33 +32,72 @@ class PurchaseSlider extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Slider(
|
return Container(
|
||||||
min: values.first.value,
|
height: 50,
|
||||||
max: values.last.value + 0.50,
|
child: CustomPaint(
|
||||||
value: selectedValue.value,
|
painter: ShapePainter(
|
||||||
onChanged: (double val) {
|
values: values,
|
||||||
int i = -1;
|
selectedValue: selectedValue,
|
||||||
for (i = 1; i < values.length; i++) {
|
color: Theme.of(context).primaryColor,
|
||||||
var prev = values[i - 1].value;
|
),
|
||||||
var cur = values[i].value;
|
child: Container(),
|
||||||
|
),
|
||||||
if (prev < val && val <= cur) {
|
|
||||||
i--;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (val == values.first.value) {
|
|
||||||
i = 0;
|
|
||||||
} else if (val >= values.last.value) {
|
|
||||||
i = values.length - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i != -1) {
|
|
||||||
onChanged(values[i]);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
label: selectedValue.text,
|
|
||||||
divisions: values.length,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ShapePainter extends CustomPainter {
|
||||||
|
final List<PaymentInfo> values;
|
||||||
|
final PaymentInfo selectedValue;
|
||||||
|
final Color color;
|
||||||
|
|
||||||
|
ShapePainter({
|
||||||
|
@required this.values,
|
||||||
|
@required this.selectedValue,
|
||||||
|
@required this.color,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
void paint(Canvas canvas, Size size) {
|
||||||
|
var paint = Paint()
|
||||||
|
..color = color
|
||||||
|
..strokeWidth = 2
|
||||||
|
..strokeCap = StrokeCap.round
|
||||||
|
..style = PaintingStyle.stroke;
|
||||||
|
|
||||||
|
var filledPaint = Paint()
|
||||||
|
..color = color
|
||||||
|
..strokeWidth = 2
|
||||||
|
..strokeCap = StrokeCap.round
|
||||||
|
..style = PaintingStyle.fill;
|
||||||
|
|
||||||
|
var diff = (values.last.value - values.first.value);
|
||||||
|
var w = (size.width / diff) * (selectedValue.value - values.first.value);
|
||||||
|
|
||||||
|
var angle = atan(size.height / size.width);
|
||||||
|
var h = w * tan(angle);
|
||||||
|
|
||||||
|
var filledPath = Path();
|
||||||
|
filledPath.moveTo(0, size.height);
|
||||||
|
filledPath.lineTo(w, size.height);
|
||||||
|
filledPath.lineTo(w, size.height - h);
|
||||||
|
filledPath.lineTo(0, size.height);
|
||||||
|
filledPath.close();
|
||||||
|
|
||||||
|
canvas.drawPath(filledPath, filledPaint);
|
||||||
|
|
||||||
|
var emptyPath = Path();
|
||||||
|
emptyPath.moveTo(0, size.height);
|
||||||
|
emptyPath.lineTo(size.width, size.height);
|
||||||
|
emptyPath.lineTo(size.width, 0);
|
||||||
|
emptyPath.lineTo(0, size.height);
|
||||||
|
emptyPath.close();
|
||||||
|
|
||||||
|
canvas.drawPath(emptyPath, paint);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool shouldRepaint(CustomPainter oldDelegate) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user