Flutter - 我希望图像在离开该区域时消失
Flutter - I want the image to disappear when it goes out of that area
Draggable 允许拖动图像。
当图像离开橙色容器区域时,我希望它消失而不是隐藏。
我使用了偏移量,但我不确定如何使图像在该区域之外消失。
如果您能给出一个代码示例,将不胜感激。
这是我的试用版。
- 更改了 Stack 小部件参数的 clipBehavior
=> 它允许上层堆栈项目溢出。
- 如果位置更改为超出区域,请将不透明度更改为 0 并带有动画。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// Remove the debug banner
debugShowCheckedModeBanner: false,
title: 'Kindacode.com',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _started = false;
bool _isDisappeared = false;
Offset _position = Offset(10, 220);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(
clipBehavior: Clip.none,
children: <Widget>[
Container(
width: 300,
height: 400,
color: Colors.orange,
),
Positioned(
top: _position.dy - 30, // Size of widget to reposition correctly
left: _position.dx,
child: AnimatedOpacity(
duration: const Duration(milliseconds: 500),
opacity: _isDisappeared ? 0.0 : 1.0,
child: Draggable(
child: Icon(Icons.home),
feedback: Icon(Icons.home),
onDragEnd: (details) {
print(details.offset);
setState(() {
if (!_started) _started = true;
_position = details.offset;
if (_position.dy - 30 > 400 || _position.dx > 300) {
setState(() {
_isDisappeared = true;
});
}
});
},
),
),
),
],
),
),
);
}
}
[![enter image description here][1]][1]
Draggable 允许拖动图像。 当图像离开橙色容器区域时,我希望它消失而不是隐藏。 我使用了偏移量,但我不确定如何使图像在该区域之外消失。 如果您能给出一个代码示例,将不胜感激。
这是我的试用版。
- 更改了 Stack 小部件参数的 clipBehavior
=> 它允许上层堆栈项目溢出。 - 如果位置更改为超出区域,请将不透明度更改为 0 并带有动画。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// Remove the debug banner
debugShowCheckedModeBanner: false,
title: 'Kindacode.com',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _started = false;
bool _isDisappeared = false;
Offset _position = Offset(10, 220);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(
clipBehavior: Clip.none,
children: <Widget>[
Container(
width: 300,
height: 400,
color: Colors.orange,
),
Positioned(
top: _position.dy - 30, // Size of widget to reposition correctly
left: _position.dx,
child: AnimatedOpacity(
duration: const Duration(milliseconds: 500),
opacity: _isDisappeared ? 0.0 : 1.0,
child: Draggable(
child: Icon(Icons.home),
feedback: Icon(Icons.home),
onDragEnd: (details) {
print(details.offset);
setState(() {
if (!_started) _started = true;
_position = details.offset;
if (_position.dy - 30 > 400 || _position.dx > 300) {
setState(() {
_isDisappeared = true;
});
}
});
},
),
),
),
],
),
),
);
}
}
[![enter image description here][1]][1]