Skip to content

Commit

Permalink
uses keyboard attachable controller for ios
Browse files Browse the repository at this point in the history
  • Loading branch information
drogel committed Apr 17, 2020
1 parent d0bc928 commit 28ebf47
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
6 changes: 6 additions & 0 deletions lib/src/cupertino_attachable_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ class CupertinoAttachableController implements KeyboardAttachableController {
final reverseSimulation = SpringSimulation(_spring, 1, 0, -_springVelocity);
return _controller.animateWith(reverseSimulation);
}

@override
void dispose() {
_controller.dispose();
_controller = null;
}
}
33 changes: 16 additions & 17 deletions lib/src/keyboard_attachable.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
import 'package:keyboard_attachable/src/cupertino_attachable_controller.dart';
import 'package:keyboard_attachable/src/keyboard_attachable_controller.dart';
import 'package:keyboard_visibility/keyboard_visibility.dart';

class KeyboardAttachable extends StatefulWidget {
Expand All @@ -11,31 +12,23 @@ class KeyboardAttachable extends StatefulWidget {
final Widget child;

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

class _KeyboardAttachableState extends State<KeyboardAttachable>
with SingleTickerProviderStateMixin {
AnimationController _controller;
KeyboardAttachableController _controller;
double _bottomSize;

@override
void initState() {
_bottomSize = 0;
_controller = AnimationController(vsync: this);
final spring = SpringDescription(mass: 8, stiffness: 1, damping: 4.5);
final fwSimulation = SpringSimulation(spring, 0, 1, 10);
final revSimulation = SpringSimulation(spring, 1, 0, -10);
_controller = CupertinoAttachableController(vsync: this);
KeyboardVisibilityNotification().addNewListener(
onShow: () {
WidgetsBinding.instance.addPostFrameCallback((_) {
_controller.animateWith(fwSimulation);
});
},
onHide: () {
_controller.animateWith(revSimulation);
}
onShow: () => WidgetsBinding.instance.addPostFrameCallback(
(_) => _controller.forward(),
),
onHide: () => _controller.reverse(),
);
super.initState();
}
Expand All @@ -48,13 +41,19 @@ class _KeyboardAttachableState extends State<KeyboardAttachable>
children: <Widget>[
widget.child,
SizeTransition(
sizeFactor: _controller,
sizeFactor: _controller.animation,
child: SizedBox(height: _bottomSize),
),
],
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}

void _shouldUpdateBottomSize(BuildContext context) {
final bottomInsets = MediaQuery.of(context).viewInsets.bottom;
if (bottomInsets != 0) {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/keyboard_attachable_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ abstract class KeyboardAttachableController {
TickerFuture forward();

TickerFuture reverse();

void dispose();
}

0 comments on commit 28ebf47

Please sign in to comment.