Skip to content

Commit 5580352

Browse files
committed
rfac: remove unnecessary focus state variable
Signed-off-by: Aman <aman2@me.iitr.ac.in>
1 parent fd886fc commit 5580352

6 files changed

Lines changed: 45 additions & 83 deletions

File tree

lib/presentation/login/bloc/login_bloc.dart

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
2929
if (!state.isFormFilled()) return;
3030

3131
emit(state.copyWith(
32-
isPasswordFocused: false,
33-
isUsernameFocused: false,
3432
status: const Status.loading(),
3533
));
3634

@@ -84,18 +82,10 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
8482
}
8583

8684
void _updatePasswordInput(PasswordInput event, Emitter<LoginState> emit) {
87-
emit(state.copyWith(
88-
isPasswordFocused: true,
89-
isUsernameFocused: false,
90-
password: event.value,
91-
));
85+
emit(state.copyWith(password: event.value));
9286
}
9387

9488
void _updateUsernameInput(UsernameInput event, Emitter<LoginState> emit) {
95-
emit(state.copyWith(
96-
isUsernameFocused: true,
97-
isPasswordFocused: false,
98-
username: event.value,
99-
));
89+
emit(state.copyWith(username: event.value));
10090
}
10191
}

lib/presentation/login/bloc/login_state.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ part of 'login_bloc.dart';
66
class LoginState with _$LoginState {
77
/// State class for [LoginBloc].
88
const factory LoginState({
9-
/// Whether the username field is in focus.
10-
@Default(false) bool isUsernameFocused,
11-
12-
/// Whether the password field is in focus.
13-
@Default(false) bool isPasswordFocused,
14-
159
/// Whether the password field should be obscured.
1610
@Default(true) bool obscurePassword,
1711

lib/presentation/login/widgets/background_decoration.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class BackgroundDecoration extends StatelessWidget {
1515
Positioned(
1616
top: 0,
1717
left: 0,
18-
child: SvgPicture.asset(
18+
child: svg.SvgPicture.asset(
1919
AppAssets.circle1,
2020
width: 170.r,
2121
),
2222
),
2323
Positioned(
2424
top: 80.r,
2525
right: 0,
26-
child: SvgPicture.asset(
26+
child: svg.SvgPicture.asset(
2727
AppAssets.triangle,
2828
width: 140.r,
2929
),

lib/presentation/login/widgets/login_widgets.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import 'package:flutter/gestures.dart';
22
import 'package:flutter/material.dart';
33
import 'package:flutter_bloc/flutter_bloc.dart';
44
import 'package:flutter_screenutil/flutter_screenutil.dart';
5-
import 'package:flutter_svg/flutter_svg.dart';
5+
import 'package:flutter_svg/flutter_svg.dart' as svg;
6+
import 'package:flutter_svg_provider/flutter_svg_provider.dart';
67
import 'package:get/get.dart';
78

89
import '../../../data/constants/assets.dart';

lib/presentation/login/widgets/text_fields.dart

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,35 @@ class PasswordField extends StatelessWidget {
77

88
@override
99
Widget build(BuildContext context) {
10-
// [BlocSelector] is not used because widget depends on both
11-
// [state.isPasswordFocused] and [state.obscurePassword].
12-
return BlocBuilder<LoginBloc, LoginState>(
13-
builder: (context, state) {
14-
return Stack(
15-
alignment: Alignment.centerRight,
16-
children: <Widget>[
17-
TextInput(
18-
action: TextInputAction.done,
19-
hint: 'Password',
20-
keyboard: TextInputType.visiblePassword,
21-
obscureText: state.obscurePassword,
22-
onChanged: (value) =>
23-
context.read<LoginBloc>().add(PasswordInput(value)),
24-
prefix: Padding(
25-
padding: EdgeInsets.symmetric(
26-
horizontal: 8.r,
27-
vertical: 10.r,
28-
),
29-
child: SvgPicture.asset(
30-
AppAssets.lock,
31-
// TODO(BURG3R5): Deal with Focus transfer.
32-
color: (state.isPasswordFocused)
33-
? AppColors.primary
34-
: AppColors.grey1,
35-
),
36-
),
10+
return BlocSelector<LoginBloc, LoginState, bool>(
11+
selector: (state) => state.obscurePassword,
12+
builder: (context, obscurePassword) {
13+
return TextInput(
14+
action: TextInputAction.done,
15+
hint: 'Password',
16+
keyboard: TextInputType.visiblePassword,
17+
obscureText: obscurePassword,
18+
onChanged: (value) =>
19+
context.read<LoginBloc>().add(PasswordInput(value)),
20+
prefix: Padding(
21+
padding: EdgeInsets.symmetric(
22+
horizontal: 8.r,
23+
vertical: 10.r,
24+
),
25+
// TODO(BURG3R5): Deal with Focus transfer.
26+
child: const ImageIcon(
27+
Svg(AppAssets.lock),
3728
),
38-
IconButton(
39-
icon: SvgPicture.asset(
40-
(state.obscurePassword) ? AppAssets.eyeOff : AppAssets.eyeOn,
41-
color: state.isPasswordFocused
42-
? AppColors.primary
43-
: AppColors.grey1,
29+
),
30+
suffix: IconButton(
31+
icon: ImageIcon(
32+
Svg(
33+
obscurePassword ? AppAssets.eyeOff : AppAssets.eyeOn,
4434
),
45-
onPressed: () =>
46-
context.read<LoginBloc>().add(const ToggleObscure()),
4735
),
48-
],
36+
onPressed: () =>
37+
context.read<LoginBloc>().add(const ToggleObscure()),
38+
),
4939
);
5040
},
5141
);
@@ -59,25 +49,18 @@ class UsernameField extends StatelessWidget {
5949

6050
@override
6151
Widget build(BuildContext context) {
62-
return BlocSelector<LoginBloc, LoginState, bool>(
63-
selector: (state) => state.isUsernameFocused,
64-
builder: (context, isUsernameFocused) {
65-
return TextInput(
66-
hint: 'Username',
67-
onChanged: (value) =>
68-
context.read<LoginBloc>().add(UsernameInput(value)),
69-
prefix: Padding(
70-
padding: EdgeInsets.symmetric(
71-
horizontal: 8.r,
72-
vertical: 10.r,
73-
),
74-
child: SvgPicture.asset(
75-
AppAssets.person,
76-
color: isUsernameFocused ? AppColors.primary : AppColors.grey1,
77-
),
78-
),
79-
);
80-
},
52+
return TextInput(
53+
hint: 'Username',
54+
onChanged: (value) => context.read<LoginBloc>().add(UsernameInput(value)),
55+
prefix: Padding(
56+
padding: EdgeInsets.symmetric(
57+
horizontal: 8.r,
58+
vertical: 10.r,
59+
),
60+
child: const ImageIcon(
61+
Svg(AppAssets.person),
62+
),
63+
),
8164
);
8265
}
8366
}

test/presentation/login/login_test.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ void blocTests() {
1414
bloc.state,
1515
const LoginState(
1616
rememberMe: true,
17-
isUsernameFocused: false,
18-
isPasswordFocused: false,
1917
obscurePassword: true,
2018
showDialog: false,
2119
status: Status(),
@@ -60,9 +58,7 @@ void blocTests() {
6058
..add(const PasswordInput(''))
6159
..add(const UsernameInput('A')),
6260
expect: () => const <LoginState>[
63-
LoginState(isUsernameFocused: true),
64-
LoginState(isPasswordFocused: true),
65-
LoginState(isUsernameFocused: true, username: 'A'),
61+
LoginState(username: 'A'),
6662
],
6763
);
6864

@@ -75,12 +71,10 @@ void blocTests() {
7571
expect: () => const <LoginState>[
7672
LoginState(
7773
username: 'username',
78-
isUsernameFocused: true,
7974
),
8075
LoginState(
8176
username: 'username',
8277
password: 'password',
83-
isPasswordFocused: true,
8478
),
8579
],
8680
);

0 commit comments

Comments
 (0)