[英]Grey screen flutter
每当我尝试启动应用程序时都会出现灰屏(我使用的是 Flutlab。我已在 Manifest 文件中启用 Internet 权限。)我还删除了 Widget 内的 Expanded()。 您能否指出可能导致此问题的原因? 这里是版本:1.0.0+1 和环境:sdk: ">=2.12.0 <3.0.0" 这里是代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:image_picker/image_picker.dart';
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
// Variables
File? imageFile;
final _formKey = GlobalKey<FormState>();
final _widthTextController = TextEditingController();
final _heightTextController = TextEditingController();
final _focusWidth = FocusNode();
final _focusHeight = FocusNode();
/// Widget
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Image Picker"),
),
body: Container(
child: imageFile == null
? Column(children: <Widget>[
Align(
alignment: Alignment.center,
child: Center(
child: Padding(
padding: EdgeInsets.all(8.00),
child: Container(
decoration: BoxDecoration(color: Colors.blue),
child: Column(children: <Widget>[
SizedBox(height: 20.00),
FractionallySizedBox(
widthFactor: 0.8,
heightFactor: 0.4,
child: InkWell(
onTap: () {
_getFromGallery();
},
child: Container(
decoration: BoxDecoration(color: Colors.white, image: DecorationImage(image: AssetImage('images/upload.png')), border: Border.all(width: 1.00, color: Colors.black), borderRadius: BorderRadius.circular(6.00)),
child: Image.file(
imageFile!,
fit: BoxFit.cover,
)))),
SizedBox(height: 10),
Form(
key: _formKey,
child: FractionallySizedBox(
widthFactor: 0.2,
heightFactor: 0.4,
child: Column(children: <Widget>[
TextFormField(
controller: _heightTextController,
focusNode: _focusHeight,
decoration: InputDecoration(
hintText: "Height",
border: UnderlineInputBorder(borderRadius: BorderRadius.circular(6.0)),
)),
SizedBox(height: 8.00),
TextFormField(
controller: _widthTextController,
focusNode: _focusWidth,
decoration: InputDecoration(
hintText: "Width",
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
),
)),
InkWell(
onTap: () {
Navigator.of(context).pushNamed('/Results');
},
child: Container(decoration: BoxDecoration(image: DecorationImage(image: AssetImage('images/submit.png'))), width: 100, height: 100, child: Container()))
])))
])))))
])
: Container(
child: Align(
alignment: Alignment.center,
child: Center(
child: Padding(
padding: EdgeInsets.all(8.00),
child: Container(
decoration: BoxDecoration(color: Colors.blue),
child: Column(children: <Widget>[
SizedBox(height: 20.00),
FractionallySizedBox(
widthFactor: 0.8,
heightFactor: 0.4,
child: InkWell(
onTap: () {
_getFromGallery();
},
child: Container(
decoration: BoxDecoration(color: Colors.white, image: DecorationImage(image: AssetImage('images/upload.png')), border: Border.all(width: 1.00, color: Colors.black), borderRadius: BorderRadius.circular(6.00)),
child: Image.file(
imageFile!,
fit: BoxFit.cover,
)))),
SizedBox(height: 10),
Form(
key: _formKey,
child: FractionallySizedBox(
widthFactor: 0.2,
heightFactor: 0.4,
child: Column(children: <Widget>[
TextFormField(
controller: _heightTextController,
focusNode: _focusHeight,
decoration: InputDecoration(
hintText: "Height",
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
))),
SizedBox(height: 8.00),
TextFormField(
controller: _widthTextController,
focusNode: _focusWidth,
decoration: InputDecoration(
hintText: "Width",
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
),
)),
InkWell(
onTap: () {
Navigator.of(context).pushNamed('/Results');
},
child: Container(decoration: BoxDecoration(image: DecorationImage(image: AssetImage('images/submit.png'))), width: 100, height: 100, child: Container()))
])))
]))))))));
}
_getFromGallery() async {
PickedFile? pickedFile = await ImagePicker().getImage(
source: ImageSource.gallery,
maxWidth: 1800,
maxHeight: 1800,
);
if (pickedFile != null) {
setState(() {
imageFile = File(pickedFile.path);
});
}
}
/// Get from Camera
_getFromCamera() async {
PickedFile? pickedFile = await ImagePicker().getImage(
source: ImageSource.camera,
maxWidth: 1800,
maxHeight: 1800,
);
if (pickedFile != null) {
setState(() {
imageFile = File(pickedFile.path);
});
}
}
}
这是错误的样子在此处输入图像描述
如果imageFile
上有一个空指针,请将Image.file()
小部件包装在 if 语句中
child: imageFile != null ? Image.file(
imageFile!,
fit: BoxFit.cover,
) : null,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.