Android 如何使颤振应用程序响应?

Android 如何使颤振应用程序响应?,android,mobile,dart,flutter,Android,Mobile,Dart,Flutter,我一直在尝试制作一个flutter应用程序(仍在学习),我能够制作一个登录屏幕。但是,我注意到该应用程序没有响应。当我在模拟器上测试它时,它工作正常,但是当我把它安装到一个屏幕更小的手机上时,我注意到小部件并不小,所以手机不能一次显示整个屏幕。有人能帮我使这个应用程序具有响应性,使它适合任何大小的屏幕吗 我的完整代码如下 import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class M

我一直在尝试制作一个flutter应用程序(仍在学习),我能够制作一个登录屏幕。但是,我注意到该应用程序没有响应。当我在模拟器上测试它时,它工作正常,但是当我把它安装到一个屏幕更小的手机上时,我注意到小部件并不小,所以手机不能一次显示整个屏幕。有人能帮我使这个应用程序具有响应性,使它适合任何大小的屏幕吗

我的完整代码如下

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  final TextEditingController _usernameController = new TextEditingController();
  final TextEditingController _passwordController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    void printSomething(){
      print("Something was printed");
    }

    Widget buttonSection = new Container(
      padding: new EdgeInsets.all(32.0),
      child: new Row(
        children: [
          new Expanded(
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                new MaterialButton(
                  child: new Text(
                    "Sign In", 
                    style: new TextStyle(
                      color: Colors.white,
                      fontSize: 20.0
                      ),
                    ),
                  onPressed: (){printSomething();},
                  height: 50.0,
                  minWidth: 400.0,
                  color: Colors.blueAccent,
                ),

                SizedBox(height: 10.0),

                new MaterialButton(
                  child: new Text(
                    "Sign Up",
                    style: new TextStyle(
                      color: Colors.white,
                      fontSize: 20.0
                      ),
                    ),
                  onPressed: null,
                  height: 50.0,
                  minWidth: 400.0,
                  color: Colors.blueAccent,
                )
              ],
            ),
          ),
        ],
      ),
    );

    Widget textFieldSection = new Container(
      padding: new EdgeInsets.all(32.0),
      child: new Row(
        children: [
          new Expanded(
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                new TextField(
                  autocorrect: false,
                  obscureText: false,
                  controller: _usernameController,
                  maxLines: 1,
                  decoration: new InputDecoration(
                    hintText: "Username",
                    icon: new Icon(Icons.person),
                  ),

                  style: new TextStyle(
                    fontSize: 20.0,
                    color: Colors.black
                  ),
                ),

                new SizedBox(height: 10.0),

                new TextField(
                  autocorrect: false,
                  obscureText: true,
                  controller: _passwordController,
                  maxLines: 1,
                  decoration: new InputDecoration(
                    hintText: "Password",
                    icon: new Icon(Icons.vpn_key),
                  ),

                  style: new TextStyle(
                    fontSize: 20.0,
                    color: Colors.black
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );

    Widget titleSection = new Container(
      padding: new EdgeInsets.all(32.0),
      child: new Row(
        children: [
          new Expanded(
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                new Container(
                  padding: const EdgeInsets.only(bottom: 8.0),
                  child: new Text(
                    "Please login using your credentials",
                    style: new TextStyle(
                      fontSize: 20.0,
                      fontWeight: FontWeight.bold
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );

    return new MaterialApp(
      title: "Service",
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),

      home: new Scaffold(
        body: new ListView(
          reverse: true,
          children: [
            SizedBox(height: 30.0),
            new Image.asset(
              'assets/logo.png',
              height: 200.0,
            ),
            titleSection,
            textFieldSection,
            buttonSection
          ].reversed.toList(),
        ),
      ),
    );
  }
}
请尝试以下操作:

  • 尽可能避免硬编码尺寸

  • 如果无法避免将宽度或高度固定为某个值,请尝试使用实际屏幕大小计算该值。 示例:
    MediaQuery.of(context.size.width-someValue

  • 使用现有的属性,而不是固定宽度和高度。示例:

    a.crossAxisAlignment:
    crossAxisAlignment.stretch
    用于将按钮拉伸到全屏大小,而不是
    minWidth:400.0

    b.填充:
    const EdgeInsets.对称(垂直:12.0)
    ,用于按钮垂直填充,而不是高度:50.0

  • 请尝试以下操作:

  • 尽可能避免硬编码尺寸

  • 如果无法避免将宽度或高度固定为某个值,请尝试使用实际屏幕大小计算该值。 示例:
    MediaQuery.of(context.size.width-someValue

  • 使用现有的属性,而不是固定宽度和高度。示例:

    a.crossAxisAlignment:
    crossAxisAlignment.stretch
    用于将按钮拉伸到全屏大小,而不是
    minWidth:400.0

    b.填充:
    const EdgeInsets.对称(垂直:12.0)
    ,用于按钮垂直填充,而不是高度:50.0


  • 我写了一篇关于颤振中响应式UI的详细文章,希望能有所帮助

    TDLR(从岗位上):

  • 在响应式UI中,我们不使用硬编码值来表示维度和 位置。使用
    MediaQuery
    获取窗口的实时大小

  • 使用
    Flexible
    Expanded
    小部件可以获得灵活的UI 使用百分比而不是硬编码值

  • 使用
    LayoutBuilder
    获取父窗口小部件的
    ConstraintBox

  • 您可以使用
    MediaQuery
    方向生成器


  • 我写了一篇关于颤振中响应式UI的详细文章,希望能有所帮助

    TDLR(从岗位上):

  • 在响应式UI中,我们不使用硬编码值来表示维度和 位置。使用
    MediaQuery
    获取窗口的实时大小

  • 使用
    Flexible
    Expanded
    小部件可以获得灵活的UI 使用百分比而不是硬编码值

  • 使用
    LayoutBuilder
    获取父窗口小部件的
    ConstraintBox

  • 您可以使用
    MediaQuery
    方向生成器

  • 使用方向

    double width, height;
    
    if (MediaQuery.of(context).orientation == Orientation.landscape){
      width = MediaQuery.of(context).size.width * 0.25; // width = 25% of the screen
      height = MediaQuery.of(context).size.height * 0.25; //height = 25% of the screen 
    }
    else{
      width = MediaQuery.of(context).size.width * 0.10; // width = 10% of the screen
      height = MediaQuery.of(context).size.height * 0.10; //height = 10% of the screen 
    }
    
    child: Padding(
            padding: EdgeInsets.symmetric(horizontal: width),
           )
    
    or
    
    child: Container(
          width: width;
          height: height;
          ), 
    
    使用方向

    double width, height;
    
    if (MediaQuery.of(context).orientation == Orientation.landscape){
      width = MediaQuery.of(context).size.width * 0.25; // width = 25% of the screen
      height = MediaQuery.of(context).size.height * 0.25; //height = 25% of the screen 
    }
    else{
      width = MediaQuery.of(context).size.width * 0.10; // width = 10% of the screen
      height = MediaQuery.of(context).size.height * 0.10; //height = 10% of the screen 
    }
    
    child: Padding(
            padding: EdgeInsets.symmetric(horizontal: width),
           )
    
    or
    
    child: Container(
          width: width;
          height: height;
          ), 
    

    为不同的屏幕大小创建响应用户界面的最简单方法是插件。

    任何屏幕大小的设备中的响应式用户界面也适用于平板电脑。检查这个插件⬇️

    在这样的值之后使用
    .h
    .w
    .sp
    ⬇️

    例如:

    Container(
      height: 10.0.h,  //10% of screen height
      width: 80.0.w,   //80% of screen width
      child: Text('Sizer', style: TextStyle(fontSize: 12.0.sp)),
    );
    

    我已经用这个插件构建了许多响应性用户界面。

    为不同屏幕大小创建响应性用户界面的最简单方法是插件。

    任何屏幕大小的设备中的响应式用户界面也适用于平板电脑。检查这个插件⬇️

    在这样的值之后使用
    .h
    .w
    .sp
    ⬇️

    例如:

    Container(
      height: 10.0.h,  //10% of screen height
      width: 80.0.w,   //80% of screen width
      child: Text('Sizer', style: TextStyle(fontSize: 12.0.sp)),
    );
    

    我用这个插件构建了许多响应性强的用户界面。

    太棒了。谢谢:)太棒了。谢谢:)我在这里写了一个基于第三方软件包的简单解决方案,因为我认为MediaQuery不够。我在这里写了一个基于第三方包的简单解决方案,因为我认为MediaQuery是不够的。谢谢你这是我的工作你这是我的工作