简体   繁体   English

Flutter:出现键盘时如何缩小图像或滚动到底部

[英]Flutter: How to shrink images or scroll to bottom when keyboard appears

I would like to shrink the image when the keyboard appears to be able to click on the button "continuer"当键盘似乎能够点击“continuer”按钮时,我想缩小图像

在此处输入图片说明

在此处输入图片说明

The code I'm using :我正在使用的代码:

// EnterEmailScreen extends StatelessWidget
Widget build(BuildContext context) {
    // This size provide us total height and width of our screen
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: size.height * 0.19),
            Image.asset(
              'assets/images/login.png',
              height: size.height * 0.45,
            ),
            SizedBox(height: size.height * 0.05),
            EmailForm(),
          ],
        ),
      ),
    );
  }

Use an Expanded :)使用Expanded :)

return Scaffold(
     body: Column(
       children: <Widget>[
         Expanded(child: Image.network('https://i.stack.imgur.com/y0V0Y.png')),
         TextField(),
         RaisedButton(
           onPressed: () {},
           child: Text('Email'),
         ),
         SizedBox(height: 50)
       ]
    ),
);

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
To shrink image, you can check MediaQuery.of(context).viewInsets.bottom == 0 to change height要缩小图像,您可以检查MediaQuery.of(context).viewInsets.bottom == 0以更改height
code snippet代码片段

Image.network(
          'https://via.placeholder.com/600/',
          height: MediaQuery.of(context).viewInsets.bottom == 0
              ? size.height * 0.45
              : size.height * 0.2,
        ),

working demo工作演示

在此处输入图片说明

full code完整代码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    print(MediaQuery.of(context).viewInsets.bottom);
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: size.height * 0.19),
            Image.network(
              'https://via.placeholder.com/600/',
              height: MediaQuery.of(context).viewInsets.bottom == 0
                  ? size.height * 0.45
                  : size.height * 0.2,
            ),
            SizedBox(height: size.height * 0.05),
            EmailForm(),
          ],
        ),
      ),
    );
  }
}

class EmailForm extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextField(),
        RaisedButton(
          child: Text('Continue'),
          onPressed: () {},
        ),
      ],
    );
  }
}

I found an alternative to my problem, instead of resizing the images.我找到了解决问题的替代方法,而不是调整图像大小。

I'm scrolling directly to the bottom (with the reverse ) when the keyboard appears.当键盘出现时,我直接滚动到底部( reverse )。

  Widget build(BuildContext context) {
    // This size provide us total height and width of our screen
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      body: SingleChildScrollView(
        reverse: true,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: size.height * 0.19),
            Image.asset(
              'assets/images/enter_email.png',
              height: size.height * 0.35,
            ),
            SizedBox(height: size.height * 0.05),
            AuthenticateEmailForm()
          ],
        ),
      ),
    );
  }

在此处输入图片说明

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

相关问题 Flutter :- 如何在 Stack 视图上滚动屏幕而不缩小屏幕?(当键盘出现时滚动整个屏幕) - Flutter :- How to scroll the sceen without shrink the screen on the Stack view?(Scroll the the whole screen when the keyboard appears ) Flutter 键盘出现时溢出底部 - Flutter overflow bottom when keyboard appears 出现键盘时颤振列表视图构建器不会缩小 - Flutter List view builder doesn't shrink when Keyboard appears Flutter:出现键盘时向上滚动屏幕 - Flutter: Scroll the screen up when keyboard appears Flutter - 如何在键盘出现时使用 SingleScrollView() 滚动整个屏幕,而不仅仅是自动显示文本 - Flutter - how to use SingleScrollView() to Scroll Entire Screen when Keyboard Appears not just text automatically Flutter - SingleChildScrollView 不会在打开键盘的情况下滚动到列的底部 - Flutter - SingleChildScrollView not scroll to the bottom of the column with keyboard on 当键盘出现时,Flutter 小部件会调整大小。 如何防止这种情况? - When the keyboard appears, the Flutter widgets resize. How to prevent this? 颤抖-键盘出现时,堆栈上的文本如何不显示? - flutter - How can text on stack not up when keyboard appears? 如何防止 Flutter 小部件在键盘出现时调整大小? - How to prevent Flutter widgets resize When the keyboard appears? 出现键盘时 Flutter TextFormField 隐藏 - Flutter TextFormField hide when keyboard appears
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM