Last active
February 14, 2020 19:48
-
-
Save ybakos/59624f441c2a3401b9d8f06c734faf4e to your computer and use it in GitHub Desktop.
CS 492 Week 7 Exploration 3 Exercise Forms
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
void main() => runApp(App()); | |
class App extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
appBar: AppBar(title: Text('Sign In')), | |
body: NameForm() | |
) | |
); | |
} | |
} | |
class NameForm extends StatefulWidget { | |
@override | |
NameFormState createState() => NameFormState(); | |
} | |
class NameFormState extends State<NameForm> { | |
final formKey = GlobalKey<FormState>(); | |
@override | |
Widget build(BuildContext context) { | |
return Form( | |
key: formKey, | |
child: Padding( | |
padding: const EdgeInsets.all(20), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: formFields(context) | |
) | |
) | |
); | |
} | |
List<Widget> formFields(BuildContext context) { | |
return [ | |
TextFormField( | |
autofocus: true, | |
decoration: InputDecoration( | |
labelText: 'Username', | |
border: OutlineInputBorder()), | |
validator: (value) => validateUsername(value), | |
), | |
SizedBox(height: 20), | |
TextFormField( | |
autofocus: true, | |
decoration: InputDecoration( | |
labelText: 'Password', | |
border: OutlineInputBorder()), | |
// TODO: Specify the validator closure | |
), | |
SizedBox(height: 10), | |
RaisedButton( | |
// TODO: invoke validateAndSignIn when pressed | |
onPressed: () {}, | |
child: Text('Sign In') | |
) | |
]; | |
} | |
void validateAndSignIn(BuildContext context) { | |
final formState = formKey.currentState; | |
if (formState.validate()) { | |
Scaffold.of(context).showSnackBar( | |
SnackBar(content: Text('Logging you in...')) | |
); | |
} | |
} | |
String validateUsername(String username) { | |
// TODO: Implement validation logic. | |
// Return a String (error message) or null | |
} | |
String validatePassword(String password) { | |
// TODO: Implement validation logic. | |
// Return a String (error message) or null | |
} | |
} |
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
void main() => runApp(App()); | |
class App extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
appBar: AppBar(title: Text('Sign In')), | |
body: NameForm() | |
) | |
); | |
} | |
} | |
class NameForm extends StatefulWidget { | |
@override | |
NameFormState createState() => NameFormState(); | |
} | |
class NameFormState extends State<NameForm> { | |
final formKey = GlobalKey<FormState>(); | |
@override | |
Widget build(BuildContext context) { | |
return Form( | |
key: formKey, | |
child: Padding( | |
padding: const EdgeInsets.all(20), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: formFields(context) | |
) | |
) | |
); | |
} | |
List<Widget> formFields(BuildContext context) { | |
return [ | |
TextFormField( | |
autofocus: true, | |
decoration: InputDecoration( | |
labelText: 'Username', | |
border: OutlineInputBorder()), | |
validator: (value) => validateUsername(value), | |
), | |
SizedBox(height: 20), | |
TextFormField( | |
autofocus: true, | |
decoration: InputDecoration( | |
labelText: 'Password', | |
border: OutlineInputBorder()), | |
// DONE | |
validator: (value) => validatePassword(value) | |
), | |
SizedBox(height: 10), | |
RaisedButton( | |
// DONE | |
onPressed: () => validateAndSignIn(context), | |
child: Text('Sign In') | |
) | |
]; | |
} | |
void validateAndSignIn(BuildContext context) { | |
final formState = formKey.currentState; | |
if (formState.validate()) { | |
Scaffold.of(context).showSnackBar( | |
SnackBar(content: Text('Logging you in...')) | |
); | |
} | |
} | |
String validateUsername(String username) { | |
// DONE | |
if (username == '') { | |
return 'Username cannot be blank'; | |
} else return null; | |
} | |
String validatePassword(String password) { | |
// DONE | |
if (password == '') { | |
return 'Password cannot be blank'; | |
} else return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment