refact: mobile actions (#8236)

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou
2024-06-01 20:23:58 +08:00
committed by GitHub
parent df36580451
commit ed5487a1fc
4 changed files with 109 additions and 59 deletions

View File

@@ -45,7 +45,7 @@ class DraggableChatWindow extends StatelessWidget {
)
: Draggable(
checkKeyboard: true,
position: position,
position: SimpleWrapper(position),
width: width,
height: height,
chatModel: chatModel,
@@ -166,15 +166,17 @@ class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
/// floating buttons of back/home/recent actions for android
class DraggableMobileActions extends StatelessWidget {
DraggableMobileActions(
{this.position = Offset.zero,
this.onBackPressed,
{this.onBackPressed,
this.onRecentPressed,
this.onHomePressed,
this.onHidePressed,
required this.position,
required this.width,
required this.height});
required this.height,
required this.scale});
final Offset position;
final double scale;
final SimpleWrapper<Offset> position;
final double width;
final double height;
final VoidCallback? onBackPressed;
@@ -186,8 +188,8 @@ class DraggableMobileActions extends StatelessWidget {
Widget build(BuildContext context) {
return Draggable(
position: position,
width: width,
height: height,
width: scale * width,
height: scale * height,
builder: (_, onPanUpdate) {
return GestureDetector(
onPanUpdate: onPanUpdate,
@@ -197,7 +199,8 @@ class DraggableMobileActions extends StatelessWidget {
child: Container(
decoration: BoxDecoration(
color: MyTheme.accent.withOpacity(0.4),
borderRadius: BorderRadius.all(Radius.circular(15))),
borderRadius:
BorderRadius.all(Radius.circular(15 * scale))),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
@@ -205,17 +208,20 @@ class DraggableMobileActions extends StatelessWidget {
color: Colors.white,
onPressed: onBackPressed,
splashRadius: kDesktopIconButtonSplashRadius,
icon: const Icon(Icons.arrow_back)),
icon: const Icon(Icons.arrow_back),
iconSize: 24 * scale),
IconButton(
color: Colors.white,
onPressed: onHomePressed,
splashRadius: kDesktopIconButtonSplashRadius,
icon: const Icon(Icons.home)),
icon: const Icon(Icons.home),
iconSize: 24 * scale),
IconButton(
color: Colors.white,
onPressed: onRecentPressed,
splashRadius: kDesktopIconButtonSplashRadius,
icon: const Icon(Icons.more_horiz)),
icon: const Icon(Icons.more_horiz),
iconSize: 24 * scale),
const VerticalDivider(
width: 0,
thickness: 2,
@@ -226,7 +232,8 @@ class DraggableMobileActions extends StatelessWidget {
color: Colors.white,
onPressed: onHidePressed,
splashRadius: kDesktopIconButtonSplashRadius,
icon: const Icon(Icons.keyboard_arrow_down)),
icon: const Icon(Icons.keyboard_arrow_down),
iconSize: 24 * scale),
],
),
)));
@@ -235,11 +242,11 @@ class DraggableMobileActions extends StatelessWidget {
}
class Draggable extends StatefulWidget {
const Draggable(
Draggable(
{Key? key,
this.checkKeyboard = false,
this.checkScreenSize = false,
this.position = Offset.zero,
required this.position,
required this.width,
required this.height,
this.chatModel,
@@ -248,7 +255,7 @@ class Draggable extends StatefulWidget {
final bool checkKeyboard;
final bool checkScreenSize;
final Offset position;
final SimpleWrapper<Offset> position;
final double width;
final double height;
final ChatModel? chatModel;
@@ -259,7 +266,6 @@ class Draggable extends StatefulWidget {
}
class _DraggableState extends State<Draggable> {
late Offset _position;
late ChatModel? _chatModel;
bool _keyboardVisible = false;
double _saveHeight = 0;
@@ -268,35 +274,36 @@ class _DraggableState extends State<Draggable> {
@override
void initState() {
super.initState();
_position = widget.position;
_chatModel = widget.chatModel;
}
get position => widget.position.value;
void onPanUpdate(DragUpdateDetails d) {
final offset = d.delta;
final size = MediaQuery.of(context).size;
double x = 0;
double y = 0;
if (_position.dx + offset.dx + widget.width > size.width) {
if (position.dx + offset.dx + widget.width > size.width) {
x = size.width - widget.width;
} else if (_position.dx + offset.dx < 0) {
} else if (position.dx + offset.dx < 0) {
x = 0;
} else {
x = _position.dx + offset.dx;
x = position.dx + offset.dx;
}
if (_position.dy + offset.dy + widget.height > size.height) {
if (position.dy + offset.dy + widget.height > size.height) {
y = size.height - widget.height;
} else if (_position.dy + offset.dy < 0) {
} else if (position.dy + offset.dy < 0) {
y = 0;
} else {
y = _position.dy + offset.dy;
y = position.dy + offset.dy;
}
setState(() {
_position = Offset(x, y);
widget.position.value = Offset(x, y);
});
_chatModel?.setChatWindowPosition(_position);
_chatModel?.setChatWindowPosition(position);
}
checkScreenSize() {}
@@ -307,13 +314,13 @@ class _DraggableState extends State<Draggable> {
// save
if (!_keyboardVisible && currentVisible) {
_saveHeight = _position.dy;
_saveHeight = position.dy;
}
// reset
if (_lastBottomHeight > 0 && bottomHeight == 0) {
setState(() {
_position = Offset(_position.dx, _saveHeight);
widget.position.value = Offset(position.dx, _saveHeight);
});
}
@@ -321,10 +328,10 @@ class _DraggableState extends State<Draggable> {
if (_keyboardVisible && currentVisible) {
final sumHeight = bottomHeight + widget.height;
final contextHeight = MediaQuery.of(context).size.height;
if (sumHeight + _position.dy > contextHeight) {
if (sumHeight + position.dy > contextHeight) {
final y = contextHeight - sumHeight;
setState(() {
_position = Offset(_position.dx, y);
widget.position.value = Offset(position.dx, y);
});
}
}
@@ -343,8 +350,8 @@ class _DraggableState extends State<Draggable> {
}
return Stack(children: [
Positioned(
top: _position.dy,
left: _position.dx,
top: position.dy,
left: position.dx,
width: widget.width,
height: widget.height,
child: widget.builder(context, onPanUpdate))