Flutter:我想在裁剪后将图像从相机或图库保存到下一页,这是我的代码

问题描述

我想将图像保存到下一页,但在裁剪图像后会打开下一页但图像未保存,这是我的代码,它打开相机拍摄图片或打开图库,但裁剪后仅拍摄到下一页我应该怎么做才能将裁剪的图像保存到下一页。我尝试了一种方法,但它在 UIpage 上返回了图像。

import 'package:Flutter/material.dart'; 
import 'package:fyp/ui.dart'; 
import 'package:fyp/Analyze.dart';

void main() {
  runApp(MaterialApp(home: MyApp())); 
}

class MyApp extends StatefulWidget {   
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(  
      body: SafeArea(    
        child: Ui(),),);
  }
}

import 'package:Flutter/material.dart';
import 'package:fyp/Analyze.dart';
import 'dart:io';
import 'package:image_picker/image_picker.dart';
import 'package:image_cropper/image_cropper.dart';

class Ui extends StatefulWidget {
  @override
  _UiState createState() => _UiState();
}

 class _UiState extends State<Ui> {
  String path;
  File _selectedFile;
  bool _inProcess1 = false;

  @override
  void initState() {
    super.initState();
  }

  getimage(ImageSource source) async {
    this.setState((){
      _inProcess1 = true;
    });
    File image = await ImagePicker.pickImage(source: source);
    if(image != null){
      File cropped = await ImageCropper.cropImage(
          sourcePath: image.path,aspectRatio: CropAspectRatio(
              ratioX: 1,ratioY: 1),compressQuality: 100,maxWidth: 700,maxHeight: 700,compressFormat: ImageCompressFormat.jpg,androidUiSettings: AndroidUiSettings(
            toolbarColor: Colors.deepOrange,toolbarTitle: "RPS Cropper",statusBarColor: Colors.deepOrange.shade900,backgroundColor: Colors.white,)

      );
      this.setState((){
        _selectedFile = cropped;
        return Analyze();
        _inProcess1 = false;
         });
    } else {
      this.setState((){
        _inProcess1 = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {

    return SafeArea(
      child: Scaffold(
        appBar: AppBar(  
          elevation: 0,toolbarHeight: 100,leading:Padding( 
            padding: const EdgeInsets.fromLTRB(15,20,0),child: Column(
              children: [
                IconButton(icon: Icon(Icons.info_outlined,color: Colors.white,size: 30,onpressed: ()=>null,Text(" Help",style: TextStyle(
                  color: Colors.white,fontSize: 16,],actions: [ 
            Padding(
              padding: const EdgeInsets.fromLTRB(0,child: Column(
                children: [
                  IconButton(icon: Icon(Icons.upload_outlined,onpressed: ()
                    { getimage(ImageSource.gallery); }
                    ),Text("Upload",style: TextStyle(
                    color: Colors.white,)
          ],backgroundColor: Colors.blue,body: Column(
          mainAxisAlignment: MainAxisAlignment.end,children: [
         Padding(
              padding: const EdgeInsets.fromLTRB(0,90,60),child: Row(
                mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [
                  Stack( 
                    alignment: Alignment.center,children: [
                      Opacity( 
                        opacity:0.6,child: CircleAvatar(  // use to create circle
                          backgroundColor: Colors.white,radius: 80,Opacity(
                        opacity:0.5,child: CircleAvatar(
                          backgroundColor: Colors.white,radius: 120,Opacity(
                        opacity:0.3,radius: 150,Opacity(
                        opacity:0.7,child: CircleAvatar(
                          backgroundColor: Colors.blue,radius: 180,IconButton(
                        iconSize: 80,icon: Icon(Icons.camera_alt,onpressed: () {
                            getimage(ImageSource.camera);
                            Navigator.push(
                              context,MaterialPageRoute(
                                builder: (context) => Analyze(
                                ),);
                          },Text("Tap to scan",style: TextStyle(
              color: Colors.white,fontSize: 20,SizedBox(   
              height: 20,width: 150,child: Divider(
                color: Colors.white,thickness: 3,);
  }
}

import 'package:Flutter/material.dart';
import 'dart:io';

class Analyze extends StatefulWidget {
  @override
  _AnalyzeState createState() => _AnalyzeState();
}

class _AnalyzeState extends State<Analyze> {
  File _selectedFile;
  bool _inProcess = false;

  @override
  Widget build(BuildContext context) {
    Widget getimageWidget(File selectedFile) {
      if (_selectedFile != null) {
        return Image.file(
          _selectedFile,width: 250,height: 250,fit: BoxFit.cover,);
      } else {
        return Image.asset(
          "assets/placeholder.jpg",);
      }
    }

    (_inProcess)
        ? Container(
            color: Colors.white,height: MediaQuery.of(context).size.height * 0.95,child: Center(
              child: CircularProgressIndicator(),)
        : Center();

    return SafeArea(
      child: Scaffold(
        body: Column(
          children: [
            getimageWidget(_selectedFile),);
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)