Android 颤振-将图像从一页推到另一页

Android 颤振-将图像从一页推到另一页,android,flutter,dart,flutter-layout,Android,Flutter,Dart,Flutter Layout,我对颤振是个新手,我不知道如何解决以下问题 ----我想做的事--- 我必须筛选如下内容 屏幕1:有一个按钮,用于打开图库以选择图像,选择后的图像将发送到下一屏幕 屏幕2:一个简单的屏幕,使用它来裁剪图像。裁剪完图像后,我想将其发送回第一个屏幕(放置在按钮的位置),然后在第一个屏幕上访问它 ----我做了什么--- 我创建了第一个和第二个屏幕,我还制作了打开图库并将照片发送到第二个屏幕的按钮。当我试图将裁剪后的图像发送回屏幕1时,问题就出现了,这根本不起作用 屏幕1: class Regis

我对颤振是个新手,我不知道如何解决以下问题


----我想做的事---

我必须筛选如下内容

屏幕1:有一个按钮,用于打开图库以选择图像,选择后的图像将发送到下一屏幕

屏幕2:一个简单的屏幕,使用它来裁剪图像。裁剪完图像后,我想将其发送回第一个屏幕(放置在按钮的位置),然后在第一个屏幕上访问它


----我做了什么---

我创建了第一个和第二个屏幕,我还制作了打开图库并将照片发送到第二个屏幕的按钮。当我试图将裁剪后的图像发送回屏幕1时,问题就出现了,这根本不起作用

屏幕1:

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ProfilePicButton(),
    );
  }
}
class SimpleCropRoute extends StatefulWidget {
  final File imagePath;

  const SimpleCropRoute({Key key, this.imagePath}) : super(key: key);

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

class _SimpleCropRouteState extends State<SimpleCropRoute> {
  final cropKey = GlobalKey<ImgCropState>();

  Future<Null> showImage(BuildContext context, File file) async {
    FileImage(file)
        .resolve(new ImageConfiguration())
        .addListener(ImageStreamListener((ImageInfo info, bool _) {
      print('-------------------------------------------$info');
    }));
    return showDialog<Null>(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
              title: Text(
                'Current screenshot:',
                style: TextStyle(
                    fontFamily: 'Roboto',
                    fontWeight: FontWeight.w300,
                    color: Theme.of(context).primaryColor,
                    letterSpacing: 1.1),
              ),
              content: Image.file(file));
        });
  }

  @override
  Widget build(BuildContext context) {
    final Map args = ModalRoute.of(context).settings.arguments;
    return Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text(
            'Zoom and Crop',
            style: TextStyle(color: Colors.black),
          ),
          backgroundColor: Colors.white,
          leading: new IconButton(
            icon:
                new Icon(Icons.navigate_before, color: Colors.black, size: 40),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),
        body: Center(
          child: ImgCrop(
            key: cropKey,
            // chipRadius: 100,
            // chipShape: 'rect',
            maximumScale: 3,
            image: FileImage(widget.imagePath),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            final crop = cropKey.currentState;
            final croppedFile =
                await crop.cropCompleted(widget.imagePath, pictureQuality: 600);

            Navigator.pop(context);
            Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => ProfilePicButton(
                    image: croppedFile)));

            // showImage(context, croppedFile);
          },
          tooltip: 'Increment',
          child: Text('Crop'),
        ));
  }
}
class ProfilePicButton extends StatefulWidget {
  final File image;

  const ProfilePicButton({Key key, this.image}) : super(key: key);

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

class _ProfilePicButtonState extends State<ProfilePicButton> {
  File _image;
  final picker = ImagePicker();
  final cropKey = GlobalKey<ImgCropState>();

  void getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);
    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);

        Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => SimpleCropRoute(
                      imagePath: _image)));
      }
    });
  }

  void _showActionSheet() {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return SafeArea(
            child: ListTile(
              leading: new Icon(Icons.photo_library),
              title: new Text("Galery"),
              onTap: () async {
                getImage();
              },
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      onPressed: _showActionSheet,
      child: Center(
        // child: _image == null ? Text('No image selected.') : Image.file(_image),
        child: _image == null ? Text('No image selected.') : Image.file(_image),
      ),
    );
  }
}
class RegisterPage扩展StatefulWidget{
@凌驾
_RegisterPageState createState()=>\u RegisterPageState();
}
类_RegisterPageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回中心(
子项:ProfilePicButton(),
);
}
}
屏幕2:

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ProfilePicButton(),
    );
  }
}
class SimpleCropRoute extends StatefulWidget {
  final File imagePath;

  const SimpleCropRoute({Key key, this.imagePath}) : super(key: key);

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

class _SimpleCropRouteState extends State<SimpleCropRoute> {
  final cropKey = GlobalKey<ImgCropState>();

  Future<Null> showImage(BuildContext context, File file) async {
    FileImage(file)
        .resolve(new ImageConfiguration())
        .addListener(ImageStreamListener((ImageInfo info, bool _) {
      print('-------------------------------------------$info');
    }));
    return showDialog<Null>(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
              title: Text(
                'Current screenshot:',
                style: TextStyle(
                    fontFamily: 'Roboto',
                    fontWeight: FontWeight.w300,
                    color: Theme.of(context).primaryColor,
                    letterSpacing: 1.1),
              ),
              content: Image.file(file));
        });
  }

  @override
  Widget build(BuildContext context) {
    final Map args = ModalRoute.of(context).settings.arguments;
    return Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text(
            'Zoom and Crop',
            style: TextStyle(color: Colors.black),
          ),
          backgroundColor: Colors.white,
          leading: new IconButton(
            icon:
                new Icon(Icons.navigate_before, color: Colors.black, size: 40),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),
        body: Center(
          child: ImgCrop(
            key: cropKey,
            // chipRadius: 100,
            // chipShape: 'rect',
            maximumScale: 3,
            image: FileImage(widget.imagePath),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            final crop = cropKey.currentState;
            final croppedFile =
                await crop.cropCompleted(widget.imagePath, pictureQuality: 600);

            Navigator.pop(context);
            Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => ProfilePicButton(
                    image: croppedFile)));

            // showImage(context, croppedFile);
          },
          tooltip: 'Increment',
          child: Text('Crop'),
        ));
  }
}
class ProfilePicButton extends StatefulWidget {
  final File image;

  const ProfilePicButton({Key key, this.image}) : super(key: key);

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

class _ProfilePicButtonState extends State<ProfilePicButton> {
  File _image;
  final picker = ImagePicker();
  final cropKey = GlobalKey<ImgCropState>();

  void getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);
    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);

        Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => SimpleCropRoute(
                      imagePath: _image)));
      }
    });
  }

  void _showActionSheet() {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return SafeArea(
            child: ListTile(
              leading: new Icon(Icons.photo_library),
              title: new Text("Galery"),
              onTap: () async {
                getImage();
              },
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      onPressed: _showActionSheet,
      child: Center(
        // child: _image == null ? Text('No image selected.') : Image.file(_image),
        child: _image == null ? Text('No image selected.') : Image.file(_image),
      ),
    );
  }
}
类SimpleProroute扩展StatefulWidget{
最终文件路径;
const SimpleCropRoute({Key-Key,this.imagePath}):super(Key:Key);
@凌驾
_SimpleCropRouteState createState()=>\u SimpleCropRouteState();
}
类_SimpleCropRouteState扩展了状态{
最终cropKey=GlobalKey();
Future showImage(构建上下文,文件)异步{
文件映像(文件)
.resolve(新的ImageConfiguration())
.addListener(ImageStreamListener((ImageInfo信息,bool){
打印('------------------------------------$info');
}));
返回显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
返回警报对话框(
标题:正文(
'当前屏幕截图:',
样式:TextStyle(
fontFamily:“机器人”,
fontWeight:fontWeight.w300,
颜色:主题。背景。原色,
字母间距:1.1),
),
内容:Image.file(file));
});
}
@凌驾
小部件构建(构建上下文){
final Map args=ModalRoute.of(context.settings.arguments);
返回脚手架(
appBar:appBar(
海拔:0,
标题:正文(
“缩放和裁剪”,
样式:TextStyle(颜色:Colors.black),
),
背景颜色:Colors.white,
领先:新图标按钮(
偶像:
新图标(Icons.navigate_before,颜色:Colors.black,大小:40),
onPressed:()=>Navigator.of(context.pop(),
),
),
正文:中(
儿童:ImgCrop(
钥匙:克罗普基,
//半径:100,
//chipShape:“rect”,
最大规模:3,
image:FileImage(widget.imagePath),
),
),
浮动操作按钮:浮动操作按钮(
onPressed:()异步{
最终作物=cropKey.currentState;
最终裁剪文件=
等待crop.cropCompleted(widget.imagePath,pictureQuality:600);
Navigator.pop(上下文);
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>ProfilePicButton(
图像:裁剪文件);
//showImage(上下文、裁剪文件);
},
工具提示:“增量”,
子项:文本(“裁剪”),
));
}
}
按钮本身:

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ProfilePicButton(),
    );
  }
}
class SimpleCropRoute extends StatefulWidget {
  final File imagePath;

  const SimpleCropRoute({Key key, this.imagePath}) : super(key: key);

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

class _SimpleCropRouteState extends State<SimpleCropRoute> {
  final cropKey = GlobalKey<ImgCropState>();

  Future<Null> showImage(BuildContext context, File file) async {
    FileImage(file)
        .resolve(new ImageConfiguration())
        .addListener(ImageStreamListener((ImageInfo info, bool _) {
      print('-------------------------------------------$info');
    }));
    return showDialog<Null>(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
              title: Text(
                'Current screenshot:',
                style: TextStyle(
                    fontFamily: 'Roboto',
                    fontWeight: FontWeight.w300,
                    color: Theme.of(context).primaryColor,
                    letterSpacing: 1.1),
              ),
              content: Image.file(file));
        });
  }

  @override
  Widget build(BuildContext context) {
    final Map args = ModalRoute.of(context).settings.arguments;
    return Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text(
            'Zoom and Crop',
            style: TextStyle(color: Colors.black),
          ),
          backgroundColor: Colors.white,
          leading: new IconButton(
            icon:
                new Icon(Icons.navigate_before, color: Colors.black, size: 40),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),
        body: Center(
          child: ImgCrop(
            key: cropKey,
            // chipRadius: 100,
            // chipShape: 'rect',
            maximumScale: 3,
            image: FileImage(widget.imagePath),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            final crop = cropKey.currentState;
            final croppedFile =
                await crop.cropCompleted(widget.imagePath, pictureQuality: 600);

            Navigator.pop(context);
            Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => ProfilePicButton(
                    image: croppedFile)));

            // showImage(context, croppedFile);
          },
          tooltip: 'Increment',
          child: Text('Crop'),
        ));
  }
}
class ProfilePicButton extends StatefulWidget {
  final File image;

  const ProfilePicButton({Key key, this.image}) : super(key: key);

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

class _ProfilePicButtonState extends State<ProfilePicButton> {
  File _image;
  final picker = ImagePicker();
  final cropKey = GlobalKey<ImgCropState>();

  void getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);
    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);

        Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => SimpleCropRoute(
                      imagePath: _image)));
      }
    });
  }

  void _showActionSheet() {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return SafeArea(
            child: ListTile(
              leading: new Icon(Icons.photo_library),
              title: new Text("Galery"),
              onTap: () async {
                getImage();
              },
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      onPressed: _showActionSheet,
      child: Center(
        // child: _image == null ? Text('No image selected.') : Image.file(_image),
        child: _image == null ? Text('No image selected.') : Image.file(_image),
      ),
    );
  }
}
class ProfilePicButton扩展StatefulWidget{
最终文件图像;
constprofilepicbutton({Key-Key,this.image}):super(Key:Key);
@凌驾
_ProfilePicButtonState createState()=>ProfilePicButtonState();
}
类_ProfilePicButtonState扩展状态{
文件图像;
最终选择器=图像选择器();
最终cropKey=GlobalKey();
void getImage()异步{
final pickedFile=wait picker.getImage(来源:ImageSource.gallery);
设置状态(){
if(pickedFile!=null){
_image=文件(pickedFile.path);
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>SimplePro路由(
图像路径:_image));
}
});
}
void_showActionSheet(){
showModalBottomSheet(
上下文:上下文,
生成器:(BuildContext上下文){
返回安全区(
孩子:ListTile(
领先:新图标(图标。照片库),
标题:新文本(“Galery”),
onTap:()异步{
getImage();
},
),
);
});
}
@凌驾
小部件构建(构建上下文){
返回按钮(
按下按钮:\u显示操作表,
儿童:中心(
//子项:_image==null?Text('未选择图像'):image.file(_image),
子项:_image==null?Text('未选择图像'):image.file(_image),
),
);
}
}


有人能帮我理解如何将图像发送回第一个屏幕吗?

我在代码中观察到的一件事是,您正在运行pop和push。这意味着永远无法达到推力。现在,您的代码正在加载上一个屏幕

因此,删除这个Navigator.pop(context);从浮动操作按钮到按下事件


如果要获取更新的内容,请使用Navigator.push以新值加载页面。这就像是向前移动而不是向后移动。

通过屏幕传递数据非常简单,但我建议使用您选择的状态管理(有很多好的理由)