-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cat-voices): add password strength indicator widget
- Loading branch information
Showing
8 changed files
with
199 additions
and
20 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
catalyst_voices/lib/widgets/indicators/voices_password_strength_indicator.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import 'package:catalyst_voices_brands/catalyst_voices_brands.dart'; | ||
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart'; | ||
import 'package:catalyst_voices_models/catalyst_voices_models.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
/// An indicator for a [PasswordStrength]. | ||
/// | ||
/// Fills in all the available horizontal space, | ||
/// use a [SizedBox] to limit it's width. | ||
final class VoicesPasswordStrengthIndicator extends StatelessWidget { | ||
final PasswordStrength passwordStrength; | ||
|
||
const VoicesPasswordStrengthIndicator({ | ||
super.key, | ||
required this.passwordStrength, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
mainAxisSize: MainAxisSize.min, | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
_Label(passwordStrength: passwordStrength), | ||
const SizedBox(height: 16), | ||
_Indicator(passwordStrength: passwordStrength), | ||
], | ||
); | ||
} | ||
} | ||
|
||
class _Label extends StatelessWidget { | ||
final PasswordStrength passwordStrength; | ||
|
||
const _Label({required this.passwordStrength}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Text( | ||
switch (passwordStrength) { | ||
PasswordStrength.weak => context.l10n.weakPasswordStrength, | ||
PasswordStrength.normal => context.l10n.normalPasswordStrength, | ||
PasswordStrength.strong => context.l10n.goodPasswordStrength, | ||
}, | ||
style: Theme.of(context).textTheme.bodySmall, | ||
); | ||
} | ||
} | ||
|
||
class _Indicator extends StatelessWidget { | ||
static const double _backgroundTrackHeight = 4; | ||
static const double _foregroundTrackHeight = 6; | ||
static const double _tracksGap = 8; | ||
|
||
final PasswordStrength passwordStrength; | ||
|
||
const _Indicator({required this.passwordStrength}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SizedBox( | ||
height: _foregroundTrackHeight, | ||
child: LayoutBuilder( | ||
builder: (context, constraints) { | ||
final totalWidthOfAllGaps = | ||
(PasswordStrength.values.length - 1) * _tracksGap; | ||
final availableWidth = constraints.maxWidth - totalWidthOfAllGaps; | ||
final trackWidth = availableWidth / PasswordStrength.values.length; | ||
|
||
return Stack( | ||
children: [ | ||
Positioned.fill( | ||
top: 1, | ||
child: Container( | ||
height: _backgroundTrackHeight, | ||
decoration: BoxDecoration( | ||
color: Theme.of(context).colors.onSurfaceSecondary08, | ||
borderRadius: BorderRadius.circular(_backgroundTrackHeight), | ||
), | ||
), | ||
), | ||
for (final strength in PasswordStrength.values) | ||
if (passwordStrength.index >= strength.index) | ||
Positioned( | ||
left: strength.index * (trackWidth + _tracksGap), | ||
width: trackWidth, | ||
child: _Track(passwordStrength: strength), | ||
), | ||
], | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
} | ||
|
||
class _Track extends StatelessWidget { | ||
final PasswordStrength passwordStrength; | ||
|
||
const _Track({required this.passwordStrength}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
height: _Indicator._foregroundTrackHeight, | ||
decoration: BoxDecoration( | ||
color: switch (passwordStrength) { | ||
PasswordStrength.weak => Theme.of(context).colorScheme.error, | ||
PasswordStrength.normal => Theme.of(context).colors.warning, | ||
PasswordStrength.strong => Theme.of(context).colors.success, | ||
}, | ||
borderRadius: BorderRadius.circular(_Indicator._foregroundTrackHeight), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
catalyst_voices/packages/catalyst_voices_models/lib/src/auth/password_strength.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters