Create a responsive Login Screen for mobile, tablet and desktop
To create a responsive widget for mobile, tablet, and desktop devices in Flutter, you can use the MediaQuery class to get the screen size and orientation, and then use the LayoutBuilder widget to determine the layout based on the screen size.
Here's an example:
import 'package:flutter/material.dart';
class ResponsiveWidget extends StatelessWidget {
final Widget mobile;
final Widget tablet;
final Widget desktop;
const ResponsiveWidget(
{super.key,
required this.mobile,
required this.tablet,
required this.desktop});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
var deviceWidth = MediaQuery.of(context).size.width;
var deviceHeight = MediaQuery.of(context).size.height;
var orientation = MediaQuery.of(context).orientation;
if (deviceWidth < 600) {
return mobile;
} else if (deviceWidth >= 600 && deviceWidth < 900) {
return tablet;
} else {
return desktop;
}
},
);
}
}
In this example, the MediaQuery class is used to get the screen size and orientation, and the LayoutBuilder widget is used to determine the layout based on the screen size. The layout is decided based on the screen width. The thresholds of 600 and 900 can be adjusted as needed.
You can then use this ResponsiveWidget by passing in different widgets for mobile, tablet, and desktop devices and wrapping it in your main widget.
We have created Login page using this Responsive widget:
import 'package:flutter/material.dart';
import 'package:responsive_design/widgets/responsive_helper.dart';
class LoginForm extends StatefulWidget {
@override
_LoginFormState createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final _formKey = GlobalKey<FormState>();
late String _email, _password;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ResponsiveWidget(
mobile: Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const logo(),
Loginform(),
],
),
),
tablet: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const logo(),
SingleChildScrollView(
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.3,
child: Loginform()),
),
],
),
),
],
),
desktop: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const logo(),
Container(
width: MediaQuery.of(context).size.width * 0.3,
child: Loginform()),
],
),
],
),
),
));
}
Form Loginform() {
return Form(
key: _formKey,
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
decoration: const InputDecoration(labelText: 'Email'),
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your email';
}
return null;
},
onSaved: (value) => _email = value!,
),
TextFormField(
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your password';
}
return null;
},
onSaved: (value) => _password = value!,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
print('Email: $_email, Password: $_password');
}
},
child: const Text('Submit'),
),
],
),
),
);
}
}
class logo extends StatelessWidget {
const logo({
super.key,
});
@override
Widget build(BuildContext context) {
return Image.asset(
'assets/images/logo.png',
height: MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.height * 0.3,
fit: BoxFit.cover,
);
}
}
Comments
Post a Comment