flutter - 颤动的未来' 不是 'Widget' 类型的子类型
问题描述
我必须使用 MultiImagePicker 选择图像,所以我有这个等待所选资产的函数,但是当我点击一个按钮时会调用这个函数。我想在不点击任何按钮的情况下进入页面时选择照片,这可能吗?
List<Asset> images = List<Asset>();
Future<void> loadAssets() async {
List<Asset> resultList = List<Asset>();
String error = '';
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 300,
enableCamera: false,
selectedAssets: images,
);
} on Exception catch (e) {
error = e.toString();
}
if (!mounted) return;
setState(() {
images = resultList;
});
}
@override
Widget build(BuildContext context) {
if(images.length == 0){
return MaterialApp(
home: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Picker Example'),
backgroundColor: Colors.blueAccent
),
body:Container(
child: images.length > 0 ? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("Select photo"),
onPressed: () => loadAssets(),
),
RaisedButton(
child: Text("Remove photo"),
onPressed: removeAssets,
),
RaisedButton(
child: Text("upload photo"),
onPressed: upload,
),
Expanded(
child: buildGridView(),
)
],
) :loadAssets();
)
);
这可能吗 ?
解决方案
调用函数loadAssets()
。initState()
选择后,图像存储在images
列表中。然后做你想做的。
示例取自Docs,
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:multi_image_picker/multi_image_picker.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Asset> images = List<Asset>();
String _error = 'No Error Dectected';
@override
void initState() {
super.initState();
loadAssets();
}
Widget buildGridView() {
return GridView.count(
crossAxisCount: 3,
children: List.generate(images.length, (index) {
Asset asset = images[index];
return AssetThumb(
asset: asset,
width: 300,
height: 300,
);
}),
);
}
Future<void> loadAssets() async {
List<Asset> resultList = List<Asset>();
String error = 'No Error Dectected';
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 300,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
materialOptions: MaterialOptions(
actionBarColor: "#abcdef",
actionBarTitle: "Example App",
allViewTitle: "All Photos",
useDetailsView: false,
selectCircleStrokeColor: "#000000",
),
);
} on Exception catch (e) {
error = e.toString();
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
images = resultList;
_error = error;
});
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: <Widget>[
Center(child: Text('Error: $_error')),
Expanded(
child: images.length > 0 ? buildGridView() : Center(child: Text("No Images")),
)
],
),
),
);
}
}
推荐阅读
- java - JPA规范:选择列表属性包含具有特定值的项目的项目
- c# - 无需提供浏览器的无头“浏览器”解决方案
- java - Android显示多个对话框
- javascript - 节流 NodeJS 中的函数
- c# - C#,如何获取图像内特定区域的颜色
- ruby-on-rails - 从 Rails 控制器中的销毁操作中删除外部 API 中的资源?
- sql-server - SQL Server - 使用 FLOAT 值更新 INT 字段时出错。价值变化
- vim - Vim 帕斯卡格式化不起作用
- html - 有没有办法标记渐进式 Web 应用程序需要硬件键盘?
- swift - ARKit,检测何时跟踪可用