Flutter 如何使用图像采集器上传图像,并将其显示在Flatter的另一个页面上

Flutter 如何使用图像采集器上传图像,并将其显示在Flatter的另一个页面上,flutter,flutter-layout,flutter-dependencies,flutter-image,imagepicker,Flutter,Flutter Layout,Flutter Dependencies,Flutter Image,Imagepicker,我有一个关于如何使用图像选择器上传图像并在另一个页面上显示它的问题。在这段代码中,我上传了5张图片,但我不知道如何在另一个页面上显示这5张上传的图片。我为上传图片创建了一个模型。希望你能理解这个问题。请帮帮我,我在这一点上陷入了困境。你的帮助可以让我开心 这是我试过的代码。谢谢你的帮助 import 'dart:io'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import

我有一个关于如何使用图像选择器上传图像并在另一个页面上显示它的问题。在这段代码中,我上传了5张图片,但我不知道如何在另一个页面上显示这5张上传的图片。我为上传图片创建了一个模型。希望你能理解这个问题。请帮帮我,我在这一点上陷入了困境。你的帮助可以让我开心

这是我试过的代码。谢谢你的帮助

import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:image_picker/image_picker.dart';

import 'package:tudo/src/modules/bsp_signup/bsp_signup_common_model.dart';
import 'package:tudo/src/modules/bsp_signup/business_profile/business_profile_page.dart';
import 'package:tudo/src/styles/colors.dart';
import 'package:tudo/src/utils/navigation_helper.dart';

class BspLicensedSignupPage extends StatefulWidget {
  static const String routeName = "/bspLicensedSignup";

  final BspSignupCommonModel bspSignupCommonModel;

  BspLicensedSignupPage({
    Key key,
    @required this.bspSignupCommonModel,
  }) : super(key: key);

  @override
  _BspLicensedSignupPageState createState() => _BspLicensedSignupPageState();
}

class _BspLicensedSignupPageState extends State<BspLicensedSignupPage> {
  List<Object> images = List<Object>();
  Future<File> _imageFile;
  @override
  void initState() {
    super.initState();
    debugPrint(
        'BSP SIGNUP DATA: ${widget.bspSignupCommonModel.businessLegalAddress}');
    setState(() {
      images.add("Add Image");
      images.add("Add Image");
      images.add("Add Image");
      images.add("Add Image");
      images.add("Add Image");
    });
  }

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  int radioValue = -1;
  File _image;

  Future<File> licenceimage;
  Future<File> profilepicture;
  picklicensepicture(ImageSource source) {
    setState(() {
      licenceimage = ImagePicker.pickImage(source: source);
    });
  }

  pickprofilepicture(ImageSource source) {
    setState(() {
      profilepicture = ImagePicker.pickImage(source: source);
    });
  }

  Map<String, dynamic> bspsignupdata = new Map<String, dynamic>();

  Future getImage() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _image = image;
    });
  }

  @override
  Widget _buildbusinesslicensepicture() {
    return GridView.count(
      shrinkWrap: true,
      crossAxisCount: 5,
      childAspectRatio: 1,
      children: List.generate(images.length, (index) {
        if (images[index] is ImageUploadModel) {
          ImageUploadModel uploadModel = images[index];
          return Card(
            clipBehavior: Clip.antiAlias,
            child: Stack(
              children: <Widget>[
                Image.file(
                  uploadModel.imageFile,
                  width: 300,
                  height: 300,
                ),
                Positioned(
                  right: 5,
                  top: 5,
                  child: InkWell(
                    child: Icon(
                      Icons.remove_circle,
                      size: 20,
                      color: Colors.red,
                    ),
                    onTap: () {
                      setState(() {
                        images.replaceRange(index, index + 1, ['Add Image']);
                      });
                    },
                  ),
                ),
              ],
            ),
          );
        } else {
          return Card(
            child: IconButton(
              icon: Icon(Icons.add),
              onPressed: () {
                _onAddImageClick(index);
              },
            ),
          );
        }
      }),
    );
  }

  Future _onAddImageClick(int index) async {
    setState(() {
      _imageFile = ImagePicker.pickImage(source: ImageSource.gallery);
      getFileImage(index);
    });
  }

  void getFileImage(int index) async {
    _imageFile.then((file) async {
      setState(() {
        ImageUploadModel imageUpload = new ImageUploadModel();
        imageUpload.isUploaded = false;
        imageUpload.uploading = false;
        imageUpload.imageFile = file;
        imageUpload.imageUrl = '';
        images.replaceRange(index, index + 1, [imageUpload]);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("BSP Licensed Signup"),
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: () {
            NavigationHelper.navigatetoBack(context);
          },
        ),
      ),
      bottomNavigationBar: Container(
        height: 56,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            new FlatButton.icon(
              icon: Icon(FontAwesomeIcons.arrowCircleRight),
              label: Text('Next'),
              color: colorStyles["primary"],
              textColor: Colors.white,
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 30),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(7),
              ),
              onPressed: () async {
                if (_formKey.currentState.validate()) {
                  BspSignupCommonModel model = widget.bspSignupCommonModel;
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) =>
                          BusinessProfilePage(bspSignupCommonModel: model),
                    ),
                  );
                }
              },
            ),
          ],
        ),
      ),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Stack(
          children: <Widget>[
            // Background(),
            SingleChildScrollView(
              child: SafeArea(
                top: false,
                bottom: false,
                child: Form(
                  autovalidate: true,
                  key: _formKey,
                  child: Scrollbar(
                    child: SingleChildScrollView(
                      dragStartBehavior: DragStartBehavior.down,
                      padding: const EdgeInsets.symmetric(horizontal: 16.0),
                      child: new Container(
                        margin: EdgeInsets.fromLTRB(30, 30, 30, 0),
                        child: new Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            SizedBox(
                              height: 10,
                            ),
                            _buildbusinesslicensepicture(),
                            SizedBox(
                              height: 20,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class ImageUploadModel {
  bool isUploaded;
  bool uploading;
  File imageFile;
  String imageUrl;

  ImageUploadModel({
    this.isUploaded,
    this.uploading,
    this.imageFile,
    this.imageUrl,
  });
}
导入'dart:io';
导入“package:flatter/signatures.dart”;
进口“包装:颤振/材料.省道”;
导入“package:font_awesome_flatter/font_awesome_flatter.dart”;
导入“包:image_picker/image_picker.dart”;
导入“包:tudo/src/modules/bsp_signup/bsp_signup_common_model.dart”;
导入“包:tudo/src/modules/bsp_注册/business_profile/business_profile_page.dart”;
导入“package:tudo/src/style/colors.dart”;
导入“package:tudo/src/utils/navigation_helper.dart”;
类BspLicensedSignupPage扩展StatefulWidget{
静态常量字符串routeName=“/bspLicensedSignup”;
最终BspSignupCommonModel BspSignupCommonModel;
BSP许可信号供应({
关键点,
@需要此.bspSignupCommonModel,
}):super(key:key);
@凌驾
_BSPLicensedSignupageState createState()=>\u BSPLicensedSignupageState();
}
类BspLicensedSignupPageState扩展状态{
列表图像=列表();
未来图像文件;
@凌驾
void initState(){
super.initState();
调试打印(
'BSP注册数据:${widget.bspSignupCommonModel.businessLegalAddress}');
设置状态(){
图像。添加(“添加图像”);
图像。添加(“添加图像”);
图像。添加(“添加图像”);
图像。添加(“添加图像”);
图像。添加(“添加图像”);
});
}
最终的GlobalKey _formKey=GlobalKey();
int radioValue=-1;
文件图像;
未来持牌人形象;
未来概况图;
picklicensepicture(图像源){
设置状态(){
licenceimage=ImagePicker.pickImage(源:源);
});
}
pickprofilepicture(图像源){
设置状态(){
profilepicture=ImagePicker.pickImage(源:源);
});
}
Map BSPSignUpdatea=新映射();
Future getImage()异步{
var image=wait ImagePicker.pickImage(源:ImageSource.gallery);
设置状态(){
_图像=图像;
});
}
@凌驾
Widget_buildbusinesslicensepicture(){
返回GridView.count(
收缩膜:对,
交叉轴计数:5,
儿童方面:1,
子项:List.generate(images.length,(index){
if(图像[索引]为ImageUploadModel){
ImageUploadModel uploadModel=图像[索引];
回程卡(
clipBehavior:Clip.antiAlias,
子:堆栈(
儿童:[
Image.file(
uploadModel.imageFile,
宽度:300,
身高:300,
),
定位(
右:5,,
前五名,
孩子:InkWell(
子:图标(
图标。删除“”圆,
尺码:20,
颜色:颜色,红色,
),
onTap:(){
设置状态(){
replaceRange(索引,索引+1,['addimage']);
});
},
),
),
],
),
);
}否则{
回程卡(
孩子:我的钮扣(
图标:图标(Icons.add),
已按下:(){
_单击图像(索引);
},
),
);
}
}),
);
}
Future\u onAddImageClick(int索引)异步{
设置状态(){
_imageFile=ImagePicker.pickImage(源:ImageSource.gallery);
getFileImage(索引);
});
}
void getFileImage(int索引)异步{
_然后((文件)异步{
设置状态(){
ImageUploadModel imageUpload=新的ImageUploadModel();
imageUpload.isupload=false;
imageUpload.upload=false;
imageUpload.imageFile=文件;
imageUpload.imageUrl='';
replaceRange(索引,索引+1,[imageUpload]);
});
});
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:appBar(
标题:文本(“BSP许可注册”),
领先:IconButton(
图标:图标(图标、箭头、背面),
已按下:(){
NavigationHelper.navigatetoBack(上下文);
},
),
),
底部导航栏:容器(
身高:56,
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
新FlatButton.icon(
图标:图标(FontAwesomeIcons.arrowCirclerRight),
标签:文本(“下一页”),
颜色:颜色样式[“主”],
textColor:Colors.white,
填充:边缘组。对称(垂直:10,水平:30),
形状:圆形矩形边框(
边界半径:边界半径。圆形(7),
),
onPressed:()异步{
if(_formKey.currentState.validate()){
BspSignupCommonModel=widget.BspSignupCommonModel;
导航器。推(
上下文
材料路线(
生成器:(上下文)=>
BusinessProfilePage(bspSignupCommonModel:model),
),
);
}
},
),
],
),
),
主体:容器(
高度:双无限,
宽度:double.infinity,
子:堆栈(
儿童:[
//背景(),
SingleChildScrollView(
儿童:安全区(
顶部:f