Skip to content

Commit

Permalink
rfac: remove unnecessary focus state variable
Browse files Browse the repository at this point in the history
Signed-off-by: Aman <aman2@me.iitr.ac.in>
  • Loading branch information
aman-singh7 committed Jul 11, 2022
1 parent fd886fc commit 5580352
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 83 deletions.
14 changes: 2 additions & 12 deletions lib/presentation/login/bloc/login_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
if (!state.isFormFilled()) return;

emit(state.copyWith(
isPasswordFocused: false,
isUsernameFocused: false,
status: const Status.loading(),
));

Expand Down Expand Up @@ -84,18 +82,10 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
}

void _updatePasswordInput(PasswordInput event, Emitter<LoginState> emit) {
emit(state.copyWith(
isPasswordFocused: true,
isUsernameFocused: false,
password: event.value,
));
emit(state.copyWith(password: event.value));
}

void _updateUsernameInput(UsernameInput event, Emitter<LoginState> emit) {
emit(state.copyWith(
isUsernameFocused: true,
isPasswordFocused: false,
username: event.value,
));
emit(state.copyWith(username: event.value));
}
}
6 changes: 0 additions & 6 deletions lib/presentation/login/bloc/login_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ part of 'login_bloc.dart';
class LoginState with _$LoginState {
/// State class for [LoginBloc].
const factory LoginState({
/// Whether the username field is in focus.
@Default(false) bool isUsernameFocused,

/// Whether the password field is in focus.
@Default(false) bool isPasswordFocused,

/// Whether the password field should be obscured.
@Default(true) bool obscurePassword,

Expand Down
4 changes: 2 additions & 2 deletions lib/presentation/login/widgets/background_decoration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class BackgroundDecoration extends StatelessWidget {
Positioned(
top: 0,
left: 0,
child: SvgPicture.asset(
child: svg.SvgPicture.asset(
AppAssets.circle1,
width: 170.r,
),
),
Positioned(
top: 80.r,
right: 0,
child: SvgPicture.asset(
child: svg.SvgPicture.asset(
AppAssets.triangle,
width: 140.r,
),
Expand Down
3 changes: 2 additions & 1 deletion lib/presentation/login/widgets/login_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_svg/flutter_svg.dart' as svg;
import 'package:flutter_svg_provider/flutter_svg_provider.dart';
import 'package:get/get.dart';

import '../../../data/constants/assets.dart';
Expand Down
93 changes: 38 additions & 55 deletions lib/presentation/login/widgets/text_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,35 @@ class PasswordField extends StatelessWidget {

@override
Widget build(BuildContext context) {
// [BlocSelector] is not used because widget depends on both
// [state.isPasswordFocused] and [state.obscurePassword].
return BlocBuilder<LoginBloc, LoginState>(
builder: (context, state) {
return Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextInput(
action: TextInputAction.done,
hint: 'Password',
keyboard: TextInputType.visiblePassword,
obscureText: state.obscurePassword,
onChanged: (value) =>
context.read<LoginBloc>().add(PasswordInput(value)),
prefix: Padding(
padding: EdgeInsets.symmetric(
horizontal: 8.r,
vertical: 10.r,
),
child: SvgPicture.asset(
AppAssets.lock,
// TODO(BURG3R5): Deal with Focus transfer.
color: (state.isPasswordFocused)
? AppColors.primary
: AppColors.grey1,
),
),
return BlocSelector<LoginBloc, LoginState, bool>(
selector: (state) => state.obscurePassword,
builder: (context, obscurePassword) {
return TextInput(
action: TextInputAction.done,
hint: 'Password',
keyboard: TextInputType.visiblePassword,
obscureText: obscurePassword,
onChanged: (value) =>
context.read<LoginBloc>().add(PasswordInput(value)),
prefix: Padding(
padding: EdgeInsets.symmetric(
horizontal: 8.r,
vertical: 10.r,
),
// TODO(BURG3R5): Deal with Focus transfer.
child: const ImageIcon(
Svg(AppAssets.lock),
),
IconButton(
icon: SvgPicture.asset(
(state.obscurePassword) ? AppAssets.eyeOff : AppAssets.eyeOn,
color: state.isPasswordFocused
? AppColors.primary
: AppColors.grey1,
),
suffix: IconButton(
icon: ImageIcon(
Svg(
obscurePassword ? AppAssets.eyeOff : AppAssets.eyeOn,
),
onPressed: () =>
context.read<LoginBloc>().add(const ToggleObscure()),
),
],
onPressed: () =>
context.read<LoginBloc>().add(const ToggleObscure()),
),
);
},
);
Expand All @@ -59,25 +49,18 @@ class UsernameField extends StatelessWidget {

@override
Widget build(BuildContext context) {
return BlocSelector<LoginBloc, LoginState, bool>(
selector: (state) => state.isUsernameFocused,
builder: (context, isUsernameFocused) {
return TextInput(
hint: 'Username',
onChanged: (value) =>
context.read<LoginBloc>().add(UsernameInput(value)),
prefix: Padding(
padding: EdgeInsets.symmetric(
horizontal: 8.r,
vertical: 10.r,
),
child: SvgPicture.asset(
AppAssets.person,
color: isUsernameFocused ? AppColors.primary : AppColors.grey1,
),
),
);
},
return TextInput(
hint: 'Username',
onChanged: (value) => context.read<LoginBloc>().add(UsernameInput(value)),
prefix: Padding(
padding: EdgeInsets.symmetric(
horizontal: 8.r,
vertical: 10.r,
),
child: const ImageIcon(
Svg(AppAssets.person),
),
),
);
}
}
8 changes: 1 addition & 7 deletions test/presentation/login/login_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ void blocTests() {
bloc.state,
const LoginState(
rememberMe: true,
isUsernameFocused: false,
isPasswordFocused: false,
obscurePassword: true,
showDialog: false,
status: Status(),
Expand Down Expand Up @@ -60,9 +58,7 @@ void blocTests() {
..add(const PasswordInput(''))
..add(const UsernameInput('A')),
expect: () => const <LoginState>[
LoginState(isUsernameFocused: true),
LoginState(isPasswordFocused: true),
LoginState(isUsernameFocused: true, username: 'A'),
LoginState(username: 'A'),
],
);

Expand All @@ -75,12 +71,10 @@ void blocTests() {
expect: () => const <LoginState>[
LoginState(
username: 'username',
isUsernameFocused: true,
),
LoginState(
username: 'username',
password: 'password',
isPasswordFocused: true,
),
],
);
Expand Down

0 comments on commit 5580352

Please sign in to comment.