Responsive Sign In
Loading preview
Starting the Flutter app
A responsive sign-in form that adapts to different screen sizes, with a clean layout and form validation.
import 'package:flutter/material.dart';
class SignInPage2 extends StatelessWidget {
const SignInPage2({super.key});
@override
Widget build(BuildContext context) {
final bool isSmallScreen = MediaQuery.of(context).size.width < 600;
return Scaffold(
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: isSmallScreen
? const Column(
mainAxisSize: MainAxisSize.min,
children: [_Logo(), _FormContent()],
)
: Container(
padding: const EdgeInsets.all(32.0),
constraints: const BoxConstraints(maxWidth: 800),
child: const Row(
children: [
Expanded(child: _Logo()),
Expanded(child: Center(child: _FormContent())),
],
),
),
),
),
);
}
}
class _Logo extends StatelessWidget {
const _Logo();
@override
Widget build(BuildContext context) {
final bool isSmallScreen = MediaQuery.of(context).size.width < 600;
return Column(
mainAxisSize: MainAxisSize.min,
children: [
FlutterLogo(size: isSmallScreen ? 100 : 200),
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
"Welcome to Flutter!",
textAlign: TextAlign.center,
style: isSmallScreen
? Theme.of(context).textTheme.titleMedium
: Theme.of(context).textTheme.titleSmall,
),
),
],
);
}
}
class _FormContent extends StatefulWidget {
const _FormContent();
@override
State<_FormContent> createState() => __FormContentState();
}
class __FormContentState extends State<_FormContent> {
bool _isPasswordVisible = false;
bool _rememberMe = false;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Container(
constraints: const BoxConstraints(maxWidth: 300),
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
validator: (value) {
// add email validation
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
bool emailValid = RegExp(
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$",
).hasMatch(value.trim());
if (!emailValid) {
return 'Please enter a valid email';
}
return null;
},
decoration: const InputDecoration(
labelText: 'Email',
hintText: 'Enter your email',
prefixIcon: Icon(Icons.email_outlined),
border: OutlineInputBorder(),
),
),
_gap(),
TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
if (value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
obscureText: !_isPasswordVisible,
decoration: InputDecoration(
labelText: 'Password',
hintText: 'Enter your password',
prefixIcon: const Icon(Icons.lock_outline_rounded),
border: const OutlineInputBorder(),
suffixIcon: IconButton(
tooltip: _isPasswordVisible ? 'Hide password' : 'Show password',
icon: Icon(
_isPasswordVisible
? Icons.visibility_off
: Icons.visibility,
),
onPressed: () {
setState(() {
_isPasswordVisible = !_isPasswordVisible;
});
},
),
),
),
_gap(),
CheckboxListTile(
value: _rememberMe,
onChanged: (value) {
if (value == null) return;
setState(() {
_rememberMe = value;
});
},
title: const Text('Remember me'),
controlAffinity: ListTileControlAffinity.leading,
dense: true,
contentPadding: const EdgeInsets.all(0),
),
_gap(),
SizedBox(
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),
),
),
child: const Padding(
padding: EdgeInsets.all(10.0),
child: Text(
'Sign in',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
),
onPressed: () {
if (_formKey.currentState?.validate() ?? false) {
/// do something
}
},
),
),
],
),
),
);
}
Widget _gap() => const SizedBox(height: 16);
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
SignInPage2 reads MediaQuery width and stacks _Logo above _FormContent below 600px; wider screens get them side by side in a Row capped at 800px. Both layouts sit in a SingleChildScrollView, so short screens can scroll when the keyboard opens. _FormContent owns the Form, the password toggle and _rememberMe. A valid submit reaches a /// do something comment, so no one is signed in.
Adapting it
- Add email and password controllers, and call your auth service at the
/// do somethingcomment. - The email
RegExponly checks format, so confirm the address on your server.