簡體   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,),
                  )
                  
                ],
               )
            ],
          ),
        ),
      ),
    );
  }
}

您可以在獲取屏幕大小的構建上下文中創建局部變量。

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

之后,您始終可以使用相對於上下文的大小。

例如像這里:

SizedBox(height: size.height * 0.5)

要在其他答案上加一點,您還可以創建一個包含您的維度的抽象類

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;
}

如果您希望在超過某個閾值時更改布局,則可以檢查此類的當前尺寸

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

試試下面的代碼:

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,
      );
}

在小型/標簽/桌面和可折疊屏幕上的預先處理使用下面給出的代碼:對於雙屏使用包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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM