Skip to content

Instantly share code, notes, and snippets.

@awaismirza
Last active September 20, 2022 13:38
Show Gist options
  • Save awaismirza/8727ba913b05821db3fc7c2c0e3c837e to your computer and use it in GitHub Desktop.
Save awaismirza/8727ba913b05821db3fc7c2c0e3c837e to your computer and use it in GitHub Desktop.
Flutter-Dart Code Snippets
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:worktime_client/src/extensions/email_validator.dart';
import 'package:worktime_client/src/config/constants.dart';
import 'package:worktime_client/src/services/http-client.dart';
class LoginFormState extends State<LoginForm> {
final _formKey = GlobalKey<FormState>();
// create email and password controller
final emailController = TextEditingController();
final passwordController = TextEditingController();
final emailFocusNode = FocusNode();
final passwordFocusNode = FocusNode();
var _isObscured;
@override
void initState() {
super.initState();
// emailController.text = testEmail;
// passwordController.text = testPassword;
_isObscured = true;
}
@override
void dispose() {
emailController.dispose();
passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
TextFormField(
focusNode: emailFocusNode,
keyboardType: TextInputType.emailAddress,
controller: emailController,
decoration: const InputDecoration(
hintText: 'email',
icon: Icon(Icons.person),
),
validator: (value) {
if (value == null || value.isEmpty) {
emailFocusNode.requestFocus();
return 'Please enter some text';
}
return value.isValidEmail() ? null : 'Invalid email';
},
),
TextFormField(
obscureText: _isObscured,
focusNode: passwordFocusNode,
keyboardType: TextInputType.emailAddress,
controller: passwordController,
decoration: InputDecoration(
suffixIcon: IconButton(
padding: const EdgeInsetsDirectional.only(end: 12.0),
icon: _isObscured ? const Icon(Icons.visibility) : const Icon(Icons.visibility_off),
onPressed: () {
setState(() {
_isObscured =!_isObscured;
});
}
),
hintText: 'password',
icon: const Icon(Icons.lock),
),
validator: (value) {
if (value == null || value.isEmpty) {
passwordFocusNode.requestFocus();
return 'Please enter some text';
}
if(value.length < 6) {
passwordFocusNode.requestFocus();
return 'Password must be at least 6 characters';
}
},
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false otherwise.
if (_formKey.currentState!.validate()) {
// If the form is valid, display a snackbar. In the real world,
// you'd often call a server or save the information in a database.
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Trying to Login')),
);
}
},
child: const Text('Submit'),
),
),
]));
}
}
// Define a custom form widget
class LoginForm extends StatefulWidget {
const LoginForm({super.key});
@override
LoginFormState createState() => LoginFormState();
}
class LoginScreen extends StatelessWidget {
const LoginScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
padding: const EdgeInsets.all(30.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Login',
style: Theme.of(context).textTheme.headline3,
),
const LoginForm(),
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'src/screens/auth/login.dart';
void main() {
runApp(const WorkTime());
}
class WorkTime extends StatelessWidget {
const WorkTime({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Title',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.green,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
/* dark theme settings */
),
themeMode: ThemeMode.dark,
home: const LoginScreen(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment