-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy path_pin_item.dart
More file actions
175 lines (151 loc) · 5.13 KB
/
_pin_item.dart
File metadata and controls
175 lines (151 loc) · 5.13 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
part of '../pinput.dart';
class _PinItem extends StatelessWidget {
final _PinputState state;
final int index;
const _PinItem({required this.state, required this.index});
@override
Widget build(BuildContext context) {
final pinTheme = _pinTheme(index);
final showNeumorphic = state.widget.showNeumorphicDesign;
final neumorphicStyle = state.widget.neumorphicStyle;
return Flexible(
child: showNeumorphic
? Neumorphic(
style: neumorphicStyle ?? NeumorphicStyle(
shape: NeumorphicShape.concave,
boxShape:
NeumorphicBoxShape.roundRect(BorderRadius.circular(19)),
depth: 1,
intensity: 5,
color: Colors.white,
),
child: inputsWithoutNeumorphic(pinTheme, showNeumorphic),
)
: inputsWithoutNeumorphic(pinTheme, showNeumorphic),
);
}
AnimatedContainer inputsWithoutNeumorphic(
PinTheme pinTheme,
bool showNeumorphic,
) {
return AnimatedContainer(
height: pinTheme.height,
width: pinTheme.width,
constraints: pinTheme.constraints,
padding: pinTheme.padding,
margin: pinTheme.margin,
decoration: showNeumorphic
? pinTheme.decoration!
.copyWith(border: Border.all(color: Colors.transparent))
: pinTheme.decoration,
alignment: state.widget.pinContentAlignment,
duration: state.widget.animationDuration,
curve: state.widget.animationCurve,
child: AnimatedSwitcher(
switchInCurve: state.widget.animationCurve,
switchOutCurve: state.widget.animationCurve,
duration: state.widget.animationDuration,
transitionBuilder: (child, animation) {
return _getTransition(child, animation);
},
child: _buildFieldContent(index, pinTheme),
),
);
}
PinTheme _pinTheme(int index) {
/// Disabled pin or default
if (!state.isEnabled) {
return _pinThemeOrDefault(state.widget.disabledPinTheme);
}
if (state.showErrorState) {
return _pinThemeOrDefault(state.widget.errorPinTheme);
}
/// Focused pin or default
if (state.hasFocus &&
index == state.selectedIndex.clamp(0, state.widget.length - 1)) {
return _pinThemeOrDefault(state.widget.focusedPinTheme);
}
/// Submitted pin or default
if (index < state.selectedIndex) {
return _pinThemeOrDefault(state.widget.submittedPinTheme);
}
/// Following pin or default
return _pinThemeOrDefault(state.widget.followingPinTheme);
}
PinTheme _getDefaultPinTheme() =>
state.widget.defaultPinTheme ?? PinputConstants._defaultPinTheme;
PinTheme _pinThemeOrDefault(PinTheme? theme) =>
theme ?? _getDefaultPinTheme();
Widget _buildFieldContent(int index, PinTheme pinTheme) {
final pin = state.pin;
final key = ValueKey<String>(index < pin.length ? pin[index] : '');
final isSubmittedPin = index < pin.length;
if (isSubmittedPin) {
if (state.widget.obscureText && state.widget.obscuringWidget != null) {
return SizedBox(key: key, child: state.widget.obscuringWidget);
}
return Text(
state.widget.obscureText ? state.widget.obscuringCharacter : pin[index],
key: key,
style: pinTheme.textStyle,
);
}
final isActiveField = index == pin.length;
final focused =
state.effectiveFocusNode.hasFocus || !state.widget.useNativeKeyboard;
final shouldShowCursor =
state.widget.showCursor && state.isEnabled && isActiveField && focused;
if (shouldShowCursor) {
return _buildCursor(pinTheme);
}
if (state.widget.preFilledWidget != null) {
return SizedBox(key: key, child: state.widget.preFilledWidget);
}
return Text('', key: key, style: pinTheme.textStyle);
}
Widget _buildCursor(PinTheme pinTheme) {
if (state.widget.isCursorAnimationEnabled) {
return _PinputAnimatedCursor(
textStyle: pinTheme.textStyle,
cursor: state.widget.cursor,
);
}
return _PinputCursor(
textStyle: pinTheme.textStyle,
cursor: state.widget.cursor,
);
}
Widget _getTransition(Widget child, Animation animation) {
if (child is _PinputAnimatedCursor) {
return child;
}
switch (state.widget.pinAnimationType) {
case PinAnimationType.none:
return child;
case PinAnimationType.fade:
return FadeTransition(
opacity: animation as Animation<double>,
child: child,
);
case PinAnimationType.scale:
return ScaleTransition(
scale: animation as Animation<double>,
child: child,
);
case PinAnimationType.slide:
return SlideTransition(
position: Tween<Offset>(
begin:
state.widget.slideTransitionBeginOffset ?? const Offset(0.8, 0),
end: Offset.zero,
).animate(animation as Animation<double>),
child: child,
);
case PinAnimationType.rotation:
return RotationTransition(
turns: animation as Animation<double>,
child: child,
);
}
}
}