flutter 隐藏ListView中的浮动文本字段

xxb16uws  于 2022-11-25  发布在  Flutter
关注(0)|答案(1)|浏览(169)

我有一个底部工作表,其中我有一个文本字段的列表视图,不幸的是,我还没有找到一种方法来向上滚动列表视图,直到文本字段是可见的。

当我点击第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(),
                ),
              ],
            ),
          ),
        ),
      ),
    );
46qrfjad

46qrfjad1#

1.将showModalBottomSheet isScrollControlled属性设置为true
1.在Padding上 Package ListView.builder,并将padding属性设置为
EdgeInsets.only( bottom: MediaQuery.of(context).viewInsets.bottom)
1.将行mainAxisSize属性设置为MainAxisSize.min
现在modalBottomSheet应该如下所示

void modalBottomSheet(BuildContext context) => showModalBottomSheet(
  context: context,
  isScrollControlled: true, //this
  builder: (context) => SizedBox(
    height: MediaQuery.of(context).size.height * 0.6,
    child: Padding( //this
      padding: EdgeInsets.only(
          bottom: MediaQuery.of(context).viewInsets.bottom), //this
      child: ListView.builder(
        itemBuilder: (context, index) => Container(
          color: index % 2 == 0 ? Colors.amber : Colors.greenAccent,
          child: Row(
            mainAxisSize: MainAxisSize.min, //this
            children: [
              Expanded(
                flex: 3,
                child: Text(
                  index.toString(),
                  textAlign: TextAlign.center,
                ),
              ),
              const Expanded(
                child: CupertinoTextField(),
              ),
            ],
          ),
        ),
      ),
    ),
  ),
);

相关问题