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