Reputation: 71
How to make the AnimatedContainer return to its previous state (1) when the keyboard is closed? at the moment changing the location of the container happens via onTap
Widget _formAuth(String labelForm, String labelButton, void func()) {
return AnimatedContainer(
duration: Duration(seconds: 1),
curve: Curves.ease,
margin: EdgeInsets.fromLTRB(13, _marginFormAuth, 13, 50),
Widget _inputLoginOrPassword(
Icon icon, String hint, TextEditingController controller, bool obscure) {
return Container(
margin: EdgeInsets.fromLTRB(10, 35, 10, 0),
child: TextField(
onTap: () {
setState(() {
_marginFormAuth = 20;
});
},
Upvotes: 0
Views: 205
Reputation: 12818
The way I found someone solved it by changing the duration
to zero then change the settings to whatever you like, then reset the duration.
AnimatedContainer(
duration: _duration,
width: _isAutoClicking ? _size!.width : 0,
height: 20,
onEnd: () async {
if (_isAutoClicking) {
_duration = Duration.zero;
setState(() {
_isAutoClicking = false;
});
await Future.delayed(const Duration(milliseconds: 1));
_duration = autoNextDuration;
}
},
alignment: Alignment.bottomLeft,
color: const Color.fromARGB(120, 120, 120, 120),
)
Upvotes: 0
Reputation: 1
To detect keyboard state use: https://github.com/adee42/flutter_keyboard_visibility
import 'package:keyboard_visibility/keyboard_visibility.dart';
@protected
void initState() {
super.initState();
KeyboardVisibilityNotification().addNewListener(
onChange: (bool visible) {
print(visible);
},
);
}
To animate container use AnimationController instead of AnimatedContainer: https://flutter.dev/docs/development/ui/animations/tutorial
Upvotes: 0
Reputation: 969
You can define a boolean variable in your state class and assign its value in the build method like following:
bool isKeyboardOpened;
build () {
isKeyboardOpened = MediaQuery.of(context).viewInsets.bottom > 0;
_marginFormAuth = isKeyboardOpened ? 20 : whateverYouHadBefore;
...
}
This solution to move containers when you open the keyboard isn't the best approach however, you should modify the whole concept behind your code and use scrollables that by nature scroll when you focus a field :)
Upvotes: 1