-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path49.flutter_color_drag_drop.dart
84 lines (76 loc) · 2.53 KB
/
49.flutter_color_drag_drop.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import 'package:flutter/material.dart';
import 'package:ig_posts/season_2/63.transperant_design_pattern.dart';
import 'package:ig_posts/utils/colors.dart';
import 'package:provider/provider.dart';
class ColorDragNotifier extends ChangeNotifier {
Color defColor = KConstantColors.bgColorFaint;
void setDraggedColor({required Color value}) {
defColor = value;
notifyListeners();
}
}
class FlutterColorDragAndDropView extends StatelessWidget {
const FlutterColorDragAndDropView({super.key});
@override
Widget build(BuildContext context) {
ColorDragNotifier colorDragNotifier({required bool renderUI}) =>
Provider.of<ColorDragNotifier>(context, listen: renderUI);
renderDragTarget() {
return DragTarget<Color>(
onAccept: (val) {
colorDragNotifier(renderUI: false).setDraggedColor(value: val);
},
builder: (context, candidateItems, rejectedItems) {
return Container(
height: 300,
width: 400,
decoration: BoxDecoration(
color: colorDragNotifier(renderUI: true).defColor,
borderRadius: BorderRadius.circular(12)),
);
},
);
}
renderColorToDrop() {
_colorBlock({required Color color}) {
return LongPressDraggable<Color>(
data: color,
dragAnchorStrategy: pointerDragAnchorStrategy,
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
color: color, borderRadius: BorderRadius.circular(12))),
feedback: SizedBox(
height: 100,
width: 100,
child: Image.asset("assets/brush.png"),
));
}
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_colorBlock(color: KConstantColors.redColor),
_colorBlock(color: KConstantColors.blueColor),
_colorBlock(color: KConstantColors.whiteColor),
_colorBlock(color: KConstantColors.textFieldTextColor),
],
);
}
return Scaffold(
backgroundColor: KConstantColors.bgColor,
appBar: CustomAppBar(title: "Flutter Custom Color Dropper"),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
renderDragTarget(),
SizedBox(height: 50),
renderColorToDrop()
],
),
),
);
}
}