Skip to content

Commit

Permalink
refactor(flutter_login): use context.select instead of BlocBuilders (
Browse files Browse the repository at this point in the history
…#4212)

Co-authored-by: Felix Angelov <felangelov@gmail.com>
  • Loading branch information
LukasMirbt and felangel authored Jul 26, 2024
1 parent 4235f7b commit 9f393a7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 72 deletions.
2 changes: 1 addition & 1 deletion examples/flutter_login/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
8 changes: 4 additions & 4 deletions examples/flutter_login/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C8080294A63A400263BE5 = {
Expand Down Expand Up @@ -344,7 +344,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -471,7 +471,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -520,7 +520,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
48 changes: 29 additions & 19 deletions examples/flutter_login/lib/home/view/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,40 @@ class HomePage extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Scaffold(
return const Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Builder(
builder: (context) {
final userId = context.select(
(AuthenticationBloc bloc) => bloc.state.user.id,
);
return Text('UserID: $userId');
},
),
ElevatedButton(
child: const Text('Logout'),
onPressed: () {
context
.read<AuthenticationBloc>()
.add(AuthenticationLogoutRequested());
},
),
],
children: [_UserId(), _LogoutButton()],
),
),
);
}
}

class _LogoutButton extends StatelessWidget {
const _LogoutButton();

@override
Widget build(BuildContext context) {
return ElevatedButton(
child: const Text('Logout'),
onPressed: () {
context.read<AuthenticationBloc>().add(AuthenticationLogoutRequested());
},
);
}
}

class _UserId extends StatelessWidget {
const _UserId();

@override
Widget build(BuildContext context) {
final userId = context.select(
(AuthenticationBloc bloc) => bloc.state.user.id,
);

return Text('UserID: $userId');
}
}
80 changes: 39 additions & 41 deletions examples/flutter_login/lib/login/view/login_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,63 +38,61 @@ class LoginForm extends StatelessWidget {
class _UsernameInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<LoginBloc, LoginState>(
buildWhen: (previous, current) => previous.username != current.username,
builder: (context, state) {
return TextField(
key: const Key('loginForm_usernameInput_textField'),
onChanged: (username) =>
context.read<LoginBloc>().add(LoginUsernameChanged(username)),
decoration: InputDecoration(
labelText: 'username',
errorText:
state.username.displayError != null ? 'invalid username' : null,
),
);
final displayError = context.select(
(LoginBloc bloc) => bloc.state.username.displayError,
);

return TextField(
key: const Key('loginForm_usernameInput_textField'),
onChanged: (username) {
context.read<LoginBloc>().add(LoginUsernameChanged(username));
},
decoration: InputDecoration(
labelText: 'username',
errorText: displayError != null ? 'invalid username' : null,
),
);
}
}

class _PasswordInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<LoginBloc, LoginState>(
buildWhen: (previous, current) => previous.password != current.password,
builder: (context, state) {
return TextField(
key: const Key('loginForm_passwordInput_textField'),
onChanged: (password) =>
context.read<LoginBloc>().add(LoginPasswordChanged(password)),
obscureText: true,
decoration: InputDecoration(
labelText: 'password',
errorText:
state.password.displayError != null ? 'invalid password' : null,
),
);
final displayError = context.select(
(LoginBloc bloc) => bloc.state.password.displayError,
);

return TextField(
key: const Key('loginForm_passwordInput_textField'),
onChanged: (password) {
context.read<LoginBloc>().add(LoginPasswordChanged(password));
},
obscureText: true,
decoration: InputDecoration(
labelText: 'password',
errorText: displayError != null ? 'invalid password' : null,
),
);
}
}

class _LoginButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<LoginBloc, LoginState>(
builder: (context, state) {
return state.status.isInProgress
? const CircularProgressIndicator()
: ElevatedButton(
key: const Key('loginForm_continue_raisedButton'),
onPressed: state.isValid
? () {
context.read<LoginBloc>().add(const LoginSubmitted());
}
: null,
child: const Text('Login'),
);
},
final isInProgress = context.select(
(LoginBloc bloc) => bloc.state.status.isInProgress,
);

if (isInProgress) return const CircularProgressIndicator();

final isValid = context.select((LoginBloc bloc) => bloc.state.isValid);

return ElevatedButton(
key: const Key('loginForm_continue_raisedButton'),
onPressed: isValid
? () => context.read<LoginBloc>().add(const LoginSubmitted())
: null,
child: const Text('Login'),
);
}
}
9 changes: 3 additions & 6 deletions examples/flutter_login/lib/login/view/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ class LoginPage extends StatelessWidget {
body: Padding(
padding: const EdgeInsets.all(12),
child: BlocProvider(
create: (context) {
return LoginBloc(
authenticationRepository:
RepositoryProvider.of<AuthenticationRepository>(context),
);
},
create: (context) => LoginBloc(
authenticationRepository: context.read<AuthenticationRepository>(),
),
child: const LoginForm(),
),
),
Expand Down

0 comments on commit 9f393a7

Please sign in to comment.