我有一个底部工作表,其中我有一个文本字段的列表视图,不幸的是,我还没有找到一种方法来向上滚动列表视图,直到文本字段是可见的。
当我点击第10个文本字段时,键盘隐藏了该文本字段。有没有办法在列表视图内滚动项目(而不是在整个列表视图中添加插图),使其不被隐藏?
使用“resizeToAvoidBottomInset”添加支架在这种情况下不起作用,因为它会将底部表单缩放到整个屏幕。
谢谢你的帮助!
代码示例:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.light,
),
home: const TextFieldCovered()
// const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class TextFieldCovered extends StatelessWidget {
const TextFieldCovered({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
body: Center(
child: CupertinoButton(
child: const Text('Bottom Sheet'),
onPressed: () => modalBottomSheet(context),
),
),
);
}
}
void modalBottomSheet(BuildContext context) => showModalBottomSheet(
context: context,
builder: (context) => SizedBox(
height: MediaQuery.of(context).size.height * 0.6,
child: ListView.builder(
itemBuilder: (context, index) => Container(
color: index % 2 == 0 ? Colors.amber : Colors.greenAccent,
child: Row(
children: [
Expanded(
flex: 3,
child: Text(
index.toString(),
textAlign: TextAlign.center,
),
),
const Expanded(
child: CupertinoTextField(),
),
],
),
),
),
),
);
1条答案
按热度按时间46qrfjad1#
1.将showModalBottomSheet
isScrollControlled
属性设置为true
1.在
Padding
上 Package ListView.builder,并将padding属性设置为EdgeInsets.only( bottom: MediaQuery.of(context).viewInsets.bottom)
1.将行
mainAxisSize
属性设置为MainAxisSize.min
现在
modalBottomSheet
应该如下所示