Thursday, October 1, 2026
HomeWeb DevelopmentConstructing a responsive login web page in Flutter

Constructing a responsive login web page in Flutter


Flutter, as a cross-platform utility framework, lets you develop apps that feel and appear nice on any platform utilizing a single codebase. With the discharge of Flutter 3.0, we now have Home windows, macOS, and net utterly steady on it.

Which means that when growing an app for these platforms, we should have in mind completely different system specs and be certain that the enchantment of our app is constant and offers customers with a seamless expertise.

With this in thoughts, Flutter offers a wide range of widgets and courses for creating responsive layouts in purposes; together with MediaQuery, LayoutBuilder, Expanded, Versatile, and AspectRatio.

On this article, we’ll be taking a look at how we will construct a responsive login web page utilizing the 2 important approaches advisable by the Flutter staff, that are the MediaQuery and LayoutBuilder courses.

Earlier than continuing, I like to recommend that you simply make sure you meet the next stipulations to proceed with this tutorial:

So with all that out of the way in which, let’s get began.

Desk of Contents

The MediaQuery class has a .of methodology that takes in a context and offers you entry to the dimensions (width/top) and orientation (portrait/panorama) of your present app.

Right here is an instance:

//...
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget construct(BuildContext context) {
    last measurement = MediaQuery.of(context).measurement; //getting the dimensions property
    last orientation = MediaQuery.of(context).orientation; //getting the orientation
    return Scaffold(
      appBar: AppBar(
        title: Textual content(widget.title),
      ),
      physique: Middle(
        baby: Column(
          mainAxisAlignment: MainAxisAlignment.heart,
          kids: <Widget>[
            Text('Size of the screen: ${size.width}x${size.height}',
                style: const TextStyle(fontSize: 16)),
            const SizedBox(height: 20),
            Text('Orientation of the screen: $orientation',
                style: const TextStyle(fontSize: 16)),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: incrementCounter,
        tooltip: 'Increment',
        baby: const Icon(Icons.add),
      ),
    );
  }
}
Using MediaQuery Class
Utilizing MediaQuery class

The LayoutBuilder class

The LayoutBuilder class has a builder property that permits us to entry a BoxConstraint object.

This object incorporates constraint info for a particular widget, which can be utilized to calculate the widget’s maxWidth and maxHeight. These values are essential in adjusting our show primarily based on the dimensions constraints assigned to our widget.

Right here’s an instance:

//...
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget construct(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Textual content('USING LAYOUTBUILDER'),
      ),
      physique: Row(
        kids: [
          Expanded(
            flex: 2,
            child: LayoutBuilder(builder: (context, constraints) {
              return Container(
                width: constraints.maxWidth,
                height: constraints.maxHeight,
                color: Colors.indigo.shade900,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Center(
                      child: Text(
                          'This is my max height: ${constraints.maxHeight}',
                          style: const TextStyle(
                              fontSize: 16, color: Colors.white)),
                    ),
                    const SizedBox(height: 20),
                    Center(
                      child: Text(
                          'This is my max width: ${constraints.maxWidth.toStringAsFixed(2)}',
                          style: const TextStyle(
                              fontSize: 16, color: Colors.white)),
                    ),
                  ],
                ),
              );
            }),
          ),
          Expanded(
            flex: 4,
            baby: LayoutBuilder(builder: (context, constraints) {
              return Container(
                width: constraints.maxWidth,
                top: constraints.maxHeight,
                shade: Colours.white,
                baby: Column(
                  mainAxisAlignment: MainAxisAlignment.heart,
                  kids: <Widget>[
                    Text('This is my max height: ${constraints.maxHeight}',
                        style: const TextStyle(fontSize: 16)),
                    const SizedBox(height: 20),
                    Text('This is my max width: ${constraints.maxWidth}',
                        style: const TextStyle(fontSize: 16)),
                  ],
                ),
              );
            }),
          ),
        ],
      ),
    );
  }
}
iPhone using layoutBuilder widger
Utilizing LayoutBuilder widget

Now that we’ve got a normal thought of how they operate, let’s use them to construct a responsive login web page that appears nice on all platforms.

Challenge Setup

To start with, we might be constructing a easy login display screen much like the pictures under:

Login page on mobile device
Cellular View
Login page on a desktop web browser
Internet/Desktop View
Photograph by Daniel on Figma Group

Now, let’s create a brand new Flutter undertaking, and paste the next code into your important.dart file as the place to begin of our app:

import 'bundle:flutter/materials.dart';

void important() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : tremendous(key: key);
  // This widget is the foundation of your utility.
  @override
  Widget construct(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colours.blue,
      ),
      residence: const LoginScreen(), //<--
    );
  }
}

Discover within the above code snippet that we’ve got a LoginScreen widget; now let’s create the LoginScreen widget. This widget can home our LayoutBuilder class, in addition to how we will return completely different screens primarily based on the system measurement specs.

class LoginScreen extends StatelessWidget {
  const LoginScreen({Key? key}) : tremendous(key: key);
  @override
  Widget construct(BuildContext context) {
    return Scaffold(
      physique: LayoutBuilder(
        builder: (context, constraints) {
          if (constraints.maxWidth < 600) {
            return const LoginMobile();
          } else if (constraints.maxWidth > 600 && constraints.maxWidth < 900) {
            return const LoginTablet();
          } else {
            return const LoginDesktop();
          }
        },
      ),
    );
  }
}

From the code snippet above, discover how we set a width breakpoint after which used the constraint object derived from the LayoutBuilder widget to verify if the system maxWidth worth is bigger or lower than the width breakpoint — and return applicable widgets as wanted.

In our case, we return the cellular view if the maxWidth is lower than 600, the pill view whether it is between 600 and 900, and the desktop/net view in any other case.

Cellular/pill view

The LoginMobile widget principally has two TextFormField widgets that function our e mail and password fields, in addition to a button to deal with occasion submission, as proven within the code snippet under.

The cellular and pill view primarily renders the identical factor, so for the sake of brevity, I received’t be together with the pill view code.

class _LoginMobileState extends State<LoginMobile> {
  bool _isChecked = false;
  @override
  Widget construct(BuildContext context) {
    return Middle(
      baby: SingleChildScrollView(
        baby: Padding(
          padding: const EdgeInsets.all(30),
          baby: Middle(
            baby: SizedBox(
              width: 300,
              baby: Column(
                mainAxisAlignment: MainAxisAlignment.heart,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                kids: [
                  Text(
                    'Welcome back',
                    style: GoogleFonts.inter(
                      fontSize: 17,
                      color: Colors.black,
                    ),
                  ),
                  const SizedBox(height: 8),
                  Text(
                    'Login to your account',
                    style: GoogleFonts.inter(
                      fontSize: 23,
                      color: Colors.black,
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                  const SizedBox(height: 35),
                  TextFormField(
                   //...
                  ),
                  const SizedBox(height: 20),
                  TextFormField(
                   //...
                  ),
                  const SizedBox(height: 25),
                  Row(
                    //...
                  ),
                  const SizedBox(height: 30),
                  TextButton(
                   //...
                  ),
                  const SizedBox(height: 15),
                  TextButton(
                  //...
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

Internet/desktop view

For the online view, we’ll present a cut up display screen with a picture and our beforehand created login elements. We try this by putting them aspect by aspect utilizing a Row widget after which wrapping each side with an Expanded widget to take up the remaining free house.

//...
class _LoginDesktopState extends State<LoginDesktop> {
  //...
  bool _isChecked = false;
  @override
  Widget construct(BuildContext context) {
    return Row(
      kids: [
        Expanded( //<-- Expanded widget
          child: Image.asset(
             'assets/image 1.png', 
             fit: BoxFit.cover,
        )),
        Expanded( //<-- Expanded widget
          child: Container(
            constraints: const BoxConstraints(maxWidth: 21),
            padding: const EdgeInsets.symmetric(horizontal: 50),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Text(
                  'Welcome back',
                  style: GoogleFonts.inter(
                    fontSize: 17,
                    color: Colors.black,
                  ),
                ),
                const SizedBox(height: 8),
                Text(
                  'Login to your account',
                  style: GoogleFonts.inter(
                    fontSize: 23,
                    color: Colors.black,
                    fontWeight: FontWeight.w700,
                  ),
                ),
                const SizedBox(height: 35),
                TextField(
                 //...
                ),
                const SizedBox(height: 20),
                TextField(
                 //...
                ),
                const SizedBox(height: 25),
                Row(
                  //...
                ),
                const SizedBox(height: 30),
                TextButton(
                  //...
                ),
                const SizedBox(height: 15),
                TextButton(
                 //...
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
Web View
Internet view:
Desktop (macOS) View
Desktop (macOS) view:

Changing Window Size

Conclusion

Attaining responsiveness in your app makes it look constant and offers customers a seamless expertise, and we’ve seen a wide range of approaches to tackling responsiveness. You can even look into responsive packages like responsivebuilder, responsivesizer, and responsive_framework.

Right here’s a GitHub hyperlink to the pattern undertaking we made:

https://github.com/dev-tayy/responsivelogindisplay screen

: Full visibility into your net and cellular apps

LogRocket is a frontend utility monitoring answer that permits you to replay issues as in the event that they occurred in your individual browser. As a substitute of guessing why errors occur, or asking customers for screenshots and log dumps, LogRocket permits you to replay the session to shortly perceive what went incorrect. It really works completely with any app, no matter framework, and has plugins to log further context from Redux, Vuex, and @ngrx/retailer.

Along with logging Redux actions and state, LogRocket data console logs, JavaScript errors, stacktraces, community requests/responses with headers + our bodies, browser metadata, and customized logs. It additionally devices the DOM to file the HTML and CSS on the web page, recreating pixel-perfect movies of even essentially the most complicated single-page net and cellular apps.

.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments