Harsh Rajpara
Harsh Rajpara

Reputation: 79

image container not move outside of the frame in flutter

I have one image container, and on this image container, there is one frame container with a fixed position. However, when the image container moves, if the boundaries of the frame container and the image container are the same, than not move in that direction.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CropImagePage(),
    );
  }
}

class CropImagePage extends StatefulWidget {
  @override
  _CropImagePageState createState() => _CropImagePageState();
}

class _CropImagePageState extends State<CropImagePage> {
  Offset _position = Offset.zero;
  bool _canDrag = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Crop Image'),
      ),
      body: Center(
        child: Stack(
          children: [
            Positioned(
              left: _position.dx,
              top: _position.dy,
              child: GestureDetector(
                onPanUpdate: (details) {
                  if (!_canDrag) return;

                  setState(() {
                    _position += details.delta;
                  });
                },
                child: Container(
                  width: 300,
                  height: 300,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.black, width: 1.0),
                  ),
                  child: Image.network(
                    'https://via.placeholder.com/300',
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ),
            Positioned(
              left: 50,
              top: 50,
              child: GestureDetector(
                onPanUpdate: (details) {
                  setState(() {
                    _canDrag = false;
                  });
                },
                onPanEnd: (details) {
                  setState(() {
                    _canDrag = true;
                  });
                },
                child: Container(
                  width: 100,
                  height: 100,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.black, width: 1.0),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
} 

I have one image container, and on this image container, there is one frame container with a fixed position. However, when the image container moves, if the boundaries of the frame container and the image container are the same, than not move in that direction.

Upvotes: 0

Views: 71

Answers (1)

SAQIB AMIN
SAQIB AMIN

Reputation: 49

Stack(
clipBehavior: Clip.none, // Set overflow to visible
children: < Widget > [
  Positioned(
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    child: GestureDetector( // Gesture detector is not necessary for overflow visible
      onTap: () {
        Navigator.pop(context);
      },
    ),
  ),
  Column(
    mainAxisSize: MainAxisSize.min,
    children: < Widget > [
      ListTile(
        leading: Icon(Icons.photo),
        title: Text('Photos'),
        onTap: () {},
      ),
    ],
  ),
],
), ),

You just need to set clip behavior to Clip.none

Upvotes: 0

Related Questions