扩展的小部件出现异常(每个孩子必须只布置一次)?



我已经实现了使用 Firebase ML 工具包读取捕获的图像文本的应用程序,在我的屏幕中,我显示了捕获的图像并在图像下方放置了一个按钮来读取图像并在按钮下方显示提取文本,但是当我单击按钮时,我得到了以下异常,

"每个孩子必须只布置一次",

我使用华为GR5 Mini测试了该应用程序,

我的代码是,

Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: Text(
'Read PDF',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.lightBlue),
),
elevation: 0.0,
backgroundColor: Colors.transparent,
iconTheme: IconThemeData(color: Colors.blue),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Flexible(
child: Container(
height: 300,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(
top: 20.0, left: 40.0, right: 40.0, bottom: 20.0),
child: Image.file(File(widget.imageRoute), fit: BoxFit.cover),
),
),
),
),
SizedBox(
height: 10,
),
Center(
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.blue)),
color: Colors.blue,
textColor: Colors.white,
padding: EdgeInsets.all(10.0),
onPressed: () async {
pickImage();
},
child: Text(
"Read".toUpperCase(),
style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.bold),
),
),
),
SizedBox(
height: 10,
),
Center(
child: text == ''
? Text('Text will display here')
: Expanded(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: SelectableText(
text,
),
),
),
),
),
],
),
);
}

你在中心小部件中使用消耗。您可以像这样使用:

Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: Text(
'Read PDF',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.lightBlue),
),
elevation: 0.0,
backgroundColor: Colors.transparent,
iconTheme: IconThemeData(color: Colors.blue),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
height: 300,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(
top: 20.0, left: 40.0, right: 40.0, bottom: 20.0),
child: Image.network("https://i1.wp.com/www.muratoner.net/wp-content/uploads/2019/01/flutterlogo.png?fit=800%2C800&ssl=1", fit: BoxFit.cover),
),
),
),
SizedBox(
height: 10,
),
Center(
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.blue)),
color: Colors.blue,
textColor: Colors.white,
padding: EdgeInsets.all(10.0),
onPressed: () async {
text = "Response text";
setState(() {
});
//                pickImage();
},
child: Text(
"Read".toUpperCase(),
style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.bold),
),
),
),
SizedBox(
height: 10,
),
text == ''
? Text('Text will display here',textAlign: TextAlign.center,)
: Expanded(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: SelectableText(
text,
),
),
),
),
],
),
);
}

我解决了同样的问题,虽然上面的解决方案是正确的,但有时似乎会失败。 在 Android Studio 的终端中尝试此命令。

1.   flutter clean
2.   flutter run

相关内容

最新更新