Skip to content

Instantly share code, notes, and snippets.

@pratamatama
Created September 2, 2020 21:59
Show Gist options
  • Save pratamatama/6071f066b03a6d872eeccf2615ba1a93 to your computer and use it in GitHub Desktop.
Save pratamatama/6071f066b03a6d872eeccf2615ba1a93 to your computer and use it in GitHub Desktop.
LoginController not found eventough it is binded via binding property on GetPage
abstract class AppPages {
static final pages = [
GetPage(
name: Routes.LOGIN,
page: () => LoginPage(),
binding: LoginBinding(),
),
GetPage(
name: Routes.HOME,
page: () => HomePage(),
binding: HomePageBinding(),
)
];
}
class LoginBinding implements Bindings {
@override
void dependencies() {
Get.lazyPut<LoginController>(() => LoginController(
repository:
AuthRepository(provider: AuthProvider(httpClient: Request.dio))));
}
}
class LoginController extends GetxController {
LoginController({@required this.repository}) : assert(repository != null);
final AuthRepository repository;
static LoginController to = Get.find();
AppLocalizations_Labels labels;
final GlobalKey formKey = GlobalKey<FormState>();
get form => formKey.currentState;
final FocusNode emailFocus = FocusNode();
final FocusNode passwordFocus = FocusNode();
final Rx<User> _user = User().obs;
User get user => this._user.value;
void set user(value) => this._user.value = value;
final _isSubmitting = false.obs;
get isSubmitting => this._isSubmitting.value;
set isSubmitting(value) => this._isSubmitting.value = value;
final _email = ''.obs;
get email => this._email.value;
set email(value) => this._email.value = value;
final _password = ''.obs;
get password => this._password.value;
set password(value) => this._password.value = value;
final _peekPassword = false.obs;
get peekPassword => this._peekPassword.value;
set peekPassword(value) => this._peekPassword.value = value;
final _error = false.obs;
get error => this._error.value;
set error(value) => this._error.value = value;
final _errorMessage = ''.obs;
get errorMessage => this._errorMessage.value;
set errorMessage(value) => this._errorMessage.value = value;
@override
Future<void> onClose() async {
emailFocus?.dispose();
passwordFocus?.dispose();
super.onClose();
}
void login() async {
if (!form.validate()) return;
form.save();
try {
final Auth response = await repository.login(user.toJson());
LocalStorage.setToken(response.token);
LocalStorage.setAuthenticatedUser(response.user);
Get.toNamed(Routes.HOME);
} catch (e) {}
}
void showPassword() {
peekPassword = !peekPassword;
}
}
class LoginForm extends GetWidget<LoginController> {
@override
Widget build(BuildContext context) {
final _focusScope = FocusScope.of(context);
final _labels = AppLocalizations.of(context);
return Form(
key: controller.formKey,
child: Column(
children: <Widget>[
FormInput(
focusNode: controller.emailFocus,
label: _labels.input.email,
hintText: 'email@domain.com',
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
validator: Validator(
labels: _labels,
label: _labels.input.email,
).email,
onSaved: (value) => controller.email = value,
onFieldSubmitted: (_) =>
_focusScope.requestFocus(controller.passwordFocus),
),
Obx(
() => FormInput(
focusNode: controller.passwordFocus,
label: _labels.input.password,
hintText: _labels.input.passwordHint,
obscureText: !controller.peekPassword,
suffixIcon: IconButton(
icon: controller.peekPassword
? Icon(Icons.visibility_off)
: Icon(Icons.visibility),
color: Colors.grey,
onPressed: () {
controller.showPassword();
},
),
keyboardType: TextInputType.visiblePassword,
validator: Validator(
labels: _labels,
label: _labels.input.password,
).isRequired,
onSaved: (value) => controller.password = value,
),
),
Container(
margin: const EdgeInsets.only(bottom: 30.0, top: 10.0),
child: Flex(
direction: Axis.horizontal,
children: [
Expanded(
child: FlatButton(
onPressed: () {},
child: Text(
_labels.button.forgotPassword,
style: TextStyle(
fontWeight: FontWeight.w600,
color: Theme.of(context).primaryColor,
),
),
),
),
Expanded(
child: SizedBox(
height: 55,
child: RaisedButton(
onPressed: controller.login,
child: Text(_labels.button.signin),
),
),
),
],
),
),
],
),
);
}
}
class LoginPage extends GetView<LoginController> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 10.0),
child: Column(
children: [
Logo(),
Spacer(),
LoginForm(),
AppInfo(),
],
),
),
),
);
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await DotEnv().load('.env');
Get.lazyPut<ThemeController>(() => ThemeController());
Get.put<LanguageController>(LanguageController());
await GetStorage.init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetBuilder<LanguageController>(builder: (languageController) {
return GetMaterialApp(
supportedLocales: AppLocalizations.languages.keys.toList(),
locale: languageController.getLocale,
localizationsDelegates: [
const AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
title: 'Test GetX',
theme: CustomTheme.light(),
darkTheme: CustomTheme.dark(),
themeMode: ThemeController.to.themeMode,
debugShowCheckedModeBanner: false,
getPages: AppPages.pages,
initialRoute: '/',
home: LocalStorage.hasToken() ? HomePage() : LoginPage(),
defaultTransition: Transition.cupertino,
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment