在颤动网络上制作响应式网络

时间:2019-08-14 09:21:29

标签: flutter-web

我正在使用Web Flutter进行表单登录,但是它仍然没有响应...当我尝试添加SingleChildScrollView小部件时,使其在移动浏览器上运行时可以滚动。但笔记本电脑上的显示表单浮在顶部

我的代码看起来像这样

  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.indigo[100],
      body: SingleChildScrollView(
              child: Container(
          child: Form(
            key: formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Center(
                  child: Card() //I'm sorry the code inside the Card () was deleted
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

2 个答案:

答案 0 :(得分:0)

以下存储库显示了如何制作“响应式”的Flutter Web项目。

https://github.com/iampawan/myportfolio

基本上,您可以根据屏幕大小选择不同的小部件。

import 'package:flutter_web/material.dart';

    class ResponsiveWidget extends StatelessWidget {
      final Widget largeScreen;
      final Widget mediumScreen;
      final Widget smallScreen;

      const ResponsiveWidget(
          {Key key,
          @required this.largeScreen,
          this.mediumScreen,
          this.smallScreen})
          : super(key: key);

      static bool isSmallScreen(BuildContext context) {
        return MediaQuery.of(context).size.width < 800;
      }

      static bool isLargeScreen(BuildContext context) {
        return MediaQuery.of(context).size.width > 800;
      }

      static bool isMediumScreen(BuildContext context) {
        return MediaQuery.of(context).size.width > 800 &&
            MediaQuery.of(context).size.width < 1200;
      }

      @override
      Widget build(BuildContext context) {
        return LayoutBuilder(
          builder: (context, constraints) {
            if (constraints.maxWidth > 800) {
              return largeScreen;
            } else if (constraints.maxWidth < 1200 && constraints.maxWidth > 800) {
              return mediumScreen ?? largeScreen;
            } else {
              return smallScreen ?? largeScreen;
            }
          },
        );
      }
    }

来源:https://github.com/iampawan/myportfolio/blob/master/lib/responsive_widget.dart

答案 1 :(得分:0)

另一种方法:)

class SampleView extends StatelessWidget {
@override
Widget build(BuildContext context) {
 return Center(
  child: Container(
    width: 200,
    height: 200,
    color: Responsive().getResponsiveValue(
        forLargeScreen: Colors.red,
        forMediumScreen: Colors.green,
        forShortScreen: Colors.yellow,
        forMobLandScapeMode: Colors.blue,
        context: context),
    // You dodn't need to provide the values for every 
    //parameter(except shortScreen & context)
    // but default its provide the value as ShortScreen for Larger and 
    //mediumScreen
    ),
   );
 }
}



 // utility 
          class Responsive {
            // function reponsible for providing value according to screensize
            getResponsiveValue(
                {dynamic forShortScreen,
                dynamic forMediumScreen,
                dynamic forLargeScreen,
                dynamic forMobLandScapeMode,
                BuildContext context}) {

              if (isLargeScreen(context)) {

                return forLargeScreen ?? forShortScreen;
              } else if (isMediumScreen(context)) {

                return forMediumScreen ?? forShortScreen;
              } 
           else if (isSmallScreen(context) && isLandScapeMode(context)) {

                return forMobLandScapeMode ?? forShortScreen;
              } else {
                return forShortScreen;
              }
            }
          
            isLandScapeMode(BuildContext context) {
              if (MediaQuery.of(context).orientation == Orientation.landscape) {
                return true;
              } else {
                return false;
              }
            }
          
            static bool isLargeScreen(BuildContext context) {
              return getWidth(context) > 1200;
            }
          
            static bool isSmallScreen(BuildContext context) {
              return getWidth(context) < 800;
            }
          
            static bool isMediumScreen(BuildContext context) {
              return getWidth(context) > 800 && getWidth(context) < 1200;
            }
          
            static double getWidth(BuildContext context) {
              return MediaQuery.of(context).size.width;
            }
          }