简体   繁体   English

如何根据不同的屏幕尺寸使 Flutter 应用响应?

[英]How can I make a Flutter app responsive according to different screen size?

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Appcolors.white,
     appBar: AppBar(
      backgroundColor: Appcolors.white,
      elevation: 0,
     ),
      body: SingleChildScrollView(
        child: SafeArea(
          child: Column(
            children:  [
              const Padding(
                padding: EdgeInsets.fromLTRB(0, 30,0,0),
                child: Center(child: Text('Welcome Back!',style: TextStyle(fontSize: 30,fontWeight: FontWeight.w700),)),
              ),
              const SizedBox(height: 8),
              const Text('Please enter your account here',style: TextStyle(fontSize: 15,fontWeight: FontWeight.w400),),
      
              const SizedBox(height: 59,),
              //Email textfield
              Padding(
                padding: const EdgeInsets.only(left: 31, right: 31),
                child: TextField(
                  cursorColor: Appcolors.black,
                  decoration: InputDecoration(
                    fillColor: Appcolors.white,
                    filled: true,
                    prefixIcon: const Icon(Icons.email_outlined,color: Appcolors.black),
                    label: Text('Email'),
                    
                    enabledBorder: OutlineInputBorder(
                      borderSide: const BorderSide(color: Appcolors.textfieldborder),
                      borderRadius: BorderRadius.circular(32)),
                    focusedBorder:  OutlineInputBorder(
                       borderSide: const BorderSide(color: Appcolors.buttonColor),
                      borderRadius: BorderRadius.circular(32)),
                  ),
                ),
              ),
      
      
               const SizedBox(height: 30,),
               //Password textfield
              Padding(
                padding: const EdgeInsets.only(left: 31, right: 31),
                child: TextField(
                   cursorColor: Appcolors.black,
                   decoration: InputDecoration(
                    prefixIcon: const Icon(Icons.lock,color: Appcolors.black),
                    label: const Text('Password'),
                     labelStyle: TextStyle(color: Appcolors.buttonColor),
                    suffixIcon: IconButton(onPressed: (){},
                     icon: const Icon(Icons.remove_red_eye_outlined,color: Appcolors.black)
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: const BorderSide(color: Appcolors.textfieldborder),
                      borderRadius: BorderRadius.circular(32)),
                    focusedBorder:  OutlineInputBorder(
                       borderSide: const BorderSide(color: Appcolors.buttonColor),
                      borderRadius: BorderRadius.circular(32)),
                  ),
                ),
              ),
              
              Padding(
                padding: const EdgeInsets.fromLTRB(150, 10, 0, 0),
                child: TextButton(onPressed: (){}, 
                child: const Text('Forgot password?',style: TextStyle(color: Appcolors.forgotpassword, fontSize: 15,fontWeight: FontWeight.w500),)
                ),
              ),

             const SizedBox(height: 30,),
              ElevatedButton(
                style: ElevatedButton.styleFrom(
                  minimumSize: const Size(300, 56),
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(32)),
                  elevation: 0,primary: Appcolors.buttonColor),
                onPressed: (){}, 
              child: const Text('Login',
              style: TextStyle(shadows: [
                Shadow(offset: Offset(5.0, 5.0),
                blurRadius: 12.0)
                ],
                fontSize: 16,
                fontWeight: FontWeight.w700),
                )
                ),
    

               Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Text('Don\'t have any accont?',style: TextStyle(fontSize: 15,fontWeight: FontWeight.w500)),
                  TextButton(onPressed: (){}, 
                  child: const Text('Sign Up',style: TextStyle(fontSize: 15,fontWeight: FontWeight.w700, color: Appcolors.buttonColor),))
                ],
               ),

              const SizedBox(height: 15,),

              
              Row(
                children: const [
                  Expanded(
                    child: Divider(thickness: 1.5,indent: 30,endIndent: 10,)),
                  Text('Sign in with',style: TextStyle(fontSize: 15),),
                  Expanded(
                    child: Divider(thickness: 1.5,indent: 10,endIndent: 30))

                ],
              ),

              const SizedBox(height: 40,),

               Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  InkWell(
                    onTap: () {
                      
                    },
                    child: const Image(
                      image:
                       AssetImage('assets/images/Facebook_Logo.png'),height: 30,),
                  ),
                  const SizedBox(width: 50,),
                  InkWell(
                    onTap: () {
                      
                    },
                    child: const Image(
                      image:
                       AssetImage('assets/images/Google__Logo.png'),height: 30,),
                  )
                  
                ],
               )
            ],
          ),
        ),
      ),
    );
  }
}

You can create a local variable in the context of the build that gets the size of the screen.您可以在获取屏幕大小的构建上下文中创建局部变量。

Size size = MediaQuery.of(context).size;

After that you can always use the size relative to the context.之后,您始终可以使用相对于上下文的大小。

For example like here:例如像这里:

SizedBox(height: size.height * 0.5)

To add up a bit on the other answer, you can also create an abstract class that contains your dimensions要在其他答案上加一点,您还可以创建一个包含您的维度的抽象类

import 'package:flutter/material.dart';

abstract class ResponsiveUtils extends StatelessWidget {
  static const double mobileWidthLimit = 650;
  static const double tabletWidthLimit = 1100;
  final Widget screenWeb;
  final Widget screenTablet;
  final Widget screenMobile;

  const ResponsiveUtils(
      {Key? key,
      required this.screenWeb,
      required this.screenTablet,
      required this.screenMobile})
      : super(key: key);

  static bool isScreenWeb(final BuildContext context) =>
      MediaQuery.of(context).size.width >= tabletWidthLimit;

  static bool isScreenTablet(final BuildContext context) =>
      MediaQuery.of(context).size.width >= mobileWidthLimit &&
      MediaQuery.of(context).size.width <= tabletWidthLimit;

  static bool isScreenMobile(final BuildContext context) =>
      MediaQuery.of(context).size.width <= mobileWidthLimit;
}

If you then want to change your layout whenever a certain threshold is passed, you can check this class for the current dimensions如果您希望在超过某个阈值时更改布局,则可以检查此类的当前尺寸

ResponsiveUtils.isScreenWeb(context)
                  ? Container()
                  : Container()

Try the following code:试试下面的代码:

import 'package:flutter/material.dart';

class Responsive extends StatelessWidget {
  const Responsive({required this.mobile, required this.tablet, required this.desktop, super.key});

  final Widget mobile;
  final Widget tablet;
  final Widget desktop;

  static bool isMobile(BuildContext context) => MediaQuery.of(context).size.width < 600.0;

  static bool isTablet(BuildContext context) => MediaQuery.of(context).size.width < 1100.0 && MediaQuery.of(context).size.width >= 600.0;

  static bool isDesktop(BuildContext context) => MediaQuery.of(context).size.width >= 1100.0;

  @override
  Widget build(BuildContext context) => LayoutBuilder(
        builder: (context, constraints) => isDesktop(context)
            ? desktop
            : isTablet(context)
                ? tablet
                : mobile,
      );
}

On advance handling on small/tab/desktop and foldable screens use code given below: for dual screen use package https://pub.dev/packages/dual_screen , just adjust your widgets accordingly.在小型/标签/桌面和可折叠屏幕上的预先处理使用下面给出的代码:对于双屏使用包https://pub.dev/packages/dual_screen ,只需相应地调整你的小部件。

import 'package:adaptive_breakpoints/adaptive_breakpoints.dart';
import 'package:dual_screen/dual_screen.dart';
import 'package:flutter/material.dart';

/// The maximum width taken up by each item on the home screen.
const maxHomeItemWidth = 1400.0;

/// Returns a boolean value whether the window is considered medium or large size.
///
/// When running on a desktop device that is also foldable, the display is not
/// considered desktop. Widgets using this method might consider the display is
/// large enough for certain layouts, which is not the case on foldable devices,
/// where only part of the display is available to said widgets.
///
/// Used to build adaptive and responsive layouts.
bool isDisplayDesktop(BuildContext context) =>
    !isDisplayFoldable(context) &&
    getWindowType(context) >= AdaptiveWindowType.medium;

/// Returns boolean value whether the window is considered medium size.
///
/// Used to build adaptive and responsive layouts.
bool isDisplaySmallDesktop(BuildContext context) {
  return getWindowType(context) == AdaptiveWindowType.medium;
}

bool isDisplayXSmallDesktop(BuildContext context) {
  return getWindowType(context) <= AdaptiveWindowType.xsmall;
}

/// Returns a boolean value whether the display has a hinge that splits the
/// screen into two, left and right sub-screens. Horizontal splits (top and
/// bottom sub-screens) are ignored for this application.
bool isDisplayFoldable(BuildContext context) {
  final hinge = MediaQuery.of(context).hinge;
  if (hinge == null) {
    return false;
  } else {
    // Vertical
    return hinge.bounds.size.aspectRatio < 1;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让flutter app根据不同的屏幕尺寸做出响应? - How to make flutter app responsive according to different screen size? 如何根据不同的屏幕尺寸使 flutter web drawer 响应? - how to make flutter web drawer responsive according to different screen size? 如何使我的应用程序响应更快并处理不同的屏幕尺寸? - How can I make my app more responsive and handle different screen size? 如何使 Flutter 应用 UI 响应不同的屏幕尺寸 [Flutter]? - How to make flutter app UI responsive for different screen sizes [Flutter]? flutter)如何让按钮位置根据屏幕大小自动调整? - flutter)How can I make the button position automatically adjust according to the screen size? 如何使可滚动和响应式屏幕颤动 - how can I make scrollable and responsive screen flutter 如何根据每个屏幕尺寸调整我的 flutter 应用程序的大小? - how to resize my flutter app according to every screen size? 如何在 Flutter 中使此屏幕响应? - How to make this screen responsive in Flutter? 如何使对话框响应不同的屏幕尺寸? Flutter - How to make dialog responsive for different screen sizes? Flutter Flutter - 根据屏幕方向/大小使 DataTable 响应 - Flutter - Making DataTable responsive according to screen orientation/size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM