Picking an image from gallery responding to weird behavior, return to previous page

14 views Asked by At

I have this weird behavior that, after picking an image from the gallery, it will somehow return to the previous page. Assuming all the permission is allowed. Below is the simple code to pick an image. The version is image_picker: ^1.0.4

import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class PostScreen extends StatefulWidget {
  @override
  _PostScreenState createState() => _PostScreenState();
}

class _PostScreenState extends State<PostScreen> {
  final ImagePicker _picker = ImagePicker();
  File? _selectedImage;

  Future<void> _pickImage() async {
    try {
      final pickedFile = await _picker.pickImage(source: ImageSource.gallery);
      if (pickedFile != null) {
        setState(() {
          _selectedImage = File(pickedFile.path);
        });
      }
    } catch (e) {
      if (kDebugMode) {
        print('Error picking image: $e');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Post Screen'),
        actions: [
          IconButton(
            icon: Icon(Icons.photo_library),
            onPressed: _pickImage,
          ),
        ],
      ),
      body: Center(
        child: _selectedImage == null
            ? Text('No image selected.')
            : Image.file(_selectedImage!),
      ),
    );
  }
}

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

I tried to run it on the main.dart and it work without problem

0

There are 0 answers