반응형
To make a TextField
in a showModalBottomSheet
go up above the keyboard when it is focused, you can use the Padding
widget with MediaQuery.of(context).viewInsets
to dynamically adjust the padding based on the keyboard's visibility. Here's how you can achieve this:
- Wrap the content of the
showModalBottomSheet
in aPadding
widget. - Set the padding using
MediaQuery.of(context).viewInsets
to ensure it adjusts when the keyboard is shown.
Here is a complete example:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Modal Bottom Sheet Example')),
body: Center(
child: ElevatedButton(
onPressed: () {
showModalBottomSheet(
context: context,
isScrollControlled: true, // This makes the bottom sheet take full height when the keyboard is open
builder: (BuildContext context) {
return Padding(
padding: MediaQuery.of(context).viewInsets, // Adjust the padding based on the keyboard visibility
child: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.min, // Make the bottom sheet size to its content
children: [
TextField(
decoration: InputDecoration(labelText: 'Enter text'),
),
SizedBox(height: 16.0),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Close'),
),
],
),
),
);
},
);
},
child: Text('Show Modal Bottom Sheet'),
),
),
),
);
}
}
In this example:
isScrollControlled: true
makes the bottom sheet take the full height when the keyboard is open.Padding
withMediaQuery.of(context).viewInsets
ensures the bottom sheet adjusts its padding based on the keyboard visibility, so theTextField
moves up above the keyboard when focused.
This approach ensures a smooth experience for users when interacting with TextField
inside a showModalBottomSheet
.
반응형
'[====== Development ======] > Flutter' 카테고리의 다른 글
[Flutter] DefaultTabController 에 대해서.. (0) | 2024.06.20 |
---|---|
[Flutter] FirebaseAuth 상태에 따라 자동으로 Screen 이동하기 (0) | 2024.06.19 |
[Flutter] Json Serialization (0) | 2024.03.03 |
Flutter App Architecture: The Repository Pattern (0) | 2024.03.02 |
[Flutter] MVVM 아키텍처 (0) | 2024.03.02 |