Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to prioritize submittedPinTheme over focusedPinTheme #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/src/pinput.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Pinput extends StatefulWidget {
this.followingPinTheme,
this.disabledPinTheme,
this.errorPinTheme,
this.prioritizeSubmittedOverFocused = false,
this.onChanged,
this.onCompleted,
this.onSubmitted,
Expand Down Expand Up @@ -136,6 +137,9 @@ class Pinput extends StatefulWidget {
/// Theme of the pin in error state
final PinTheme? errorPinTheme;

/// Whether to use submittedPinTheme or focusedPinTheme if the pin in focused & submitted state.
final bool prioritizeSubmittedOverFocused;

/// If true keyboard will be closed
final bool closeKeyboardWhenCompleted;

Expand Down
7 changes: 6 additions & 1 deletion lib/src/pinput_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,12 @@ class _PinputState extends State<Pinput>
separatorBuilder: widget.separatorBuilder,
mainAxisAlignment: widget.mainAxisAlignment,
children: Iterable<int>.generate(widget.length).map<Widget>((index) {
return _PinItem(state: this, index: index);
return _PinItem(
state: this,
index: index,
prioritizeSubmittedOverFocused:
widget.prioritizeSubmittedOverFocused,
);
}).toList(),
);
}
Expand Down
22 changes: 16 additions & 6 deletions lib/src/widgets/_pin_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ part of '../pinput.dart';
class _PinItem extends StatelessWidget {
final _PinputState state;
final int index;
final bool prioritizeSubmittedOverFocused;

const _PinItem({required this.state, required this.index});
const _PinItem({
required this.state,
required this.index,
required this.prioritizeSubmittedOverFocused,
});

@override
Widget build(BuildContext context) {
final pinTheme = _pinTheme(index);
final pinTheme = _pinTheme(index, prioritizeSubmittedOverFocused);

return Flexible(
child: AnimatedContainer(
Expand All @@ -32,7 +37,7 @@ class _PinItem extends StatelessWidget {
);
}

PinTheme _pinTheme(int index) {
PinTheme _pinTheme(int index, bool prioritizeSubmittedOverFocused) {
/// Disabled pin or default
if (!state.isEnabled) {
return _pinThemeOrDefault(state.widget.disabledPinTheme);
Expand All @@ -42,16 +47,21 @@ class _PinItem extends StatelessWidget {
return _pinThemeOrDefault(state.widget.errorPinTheme);
}

final submittedPinTheme =
_pinThemeOrDefault(state.widget.submittedPinTheme);

/// Submitted pin or default
if (prioritizeSubmittedOverFocused && index < state.selectedIndex)
return submittedPinTheme;

/// 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);
}
if (index < state.selectedIndex) return submittedPinTheme;

/// Following pin or default
return _pinThemeOrDefault(state.widget.followingPinTheme);
Expand Down