[英]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.