【问题标题】:Flutter how to stop scroll when focus TextField into CustomScrollView?Flutter 如何在将 TextField 聚焦到 CustomScrollView 时停止滚动?
【发布时间】:2020-04-25 13:52:06
【问题描述】:

我正在为 CustomScrollView 中的 TextField 苦苦挣扎。

在我的应用程序中,我有一个 CustomScrollView 内容 SliverAppBar 和 SliverList。 Insise SliverAppBar 我放了 TextField。当我关注 TextField 时,SliverList 自动滚动到顶部位置然后我添加属性 showCursor:false,它停止滚动但我想显示光标而不是滚动。这个怎么存档。非常感谢!!

这是我的代码:

import 'package:flutter/material.dart';

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(),
      body: SafeArea(
          child: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            leading: Container(),
            floating: true,
            pinned: false,
            snap: true,
            bottom: PreferredSize(
              preferredSize: Size.fromHeight(20.0),
              child: Container(),
            ), // Add this code
            flexibleSpace: Container(
              padding: EdgeInsets.all(10),
              height: 340,
              width: double.infinity,
              child: Stack(
                children: <Widget>[
                  Positioned.fill(
                      child: Container(
                    color: Colors.white,
                  )),
                  Center(
                    child: TextField(
                      autofocus: false,
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        contentPadding: EdgeInsets.only(left: 8.0),
                        hintText: "Search",
                        hintStyle: TextStyle(fontSize: 20),
                      ),
                      showCursor: true, //not show cursor
                    ),
                  ),
                ],
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                    color: Colors.blue[(index + 1) * 50], height: 150.0);
              },
              childCount: 10,
            ),
          ),
        ],
      )),
    );
  }
}

【问题讨论】:

标签: flutter textfield flutter-sliver


【解决方案1】:

评论中提到的issue 应该由this PR 修复,它似乎首先登陆Flutter 1.22.0。

我已经尝试过您的示例,但它仍然具有相同的行为。

我尝试将 pinned 设置为 true 并且它正在工作。

SliverAppBar(
            leading: Container(),
            floating: true,
            pinned: true,
            snap: true,
            bottom: PreferredSize(
              preferredSize: Size.fromHeight(20.0),
              child: Container(),
            ), // Add this code

另外,发现 this GitHub post 有同样的问题。如果您不想将pinned 属性设置为true,可以查看建议的解决方法in this comment

重复 #25507。一种 解决方法是disable implicitScrolling

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-20
    • 2021-01-02
    • 2022-12-28
    • 2019-12-30
    • 1970-01-01
    • 2023-04-05
    • 2021-08-24
    • 1970-01-01
    相关资源
    最近更新 更多