如何使 DraggableScrollableSheet 在拖动手柄时向上滚动,就像谷歌地图一样

dav*_*ang 2 flutter

我是颤振的新手。我正在尝试添加一个小手柄来提示用户像谷歌地图一样拖动。但是,如果我在上面放置一个单独的容器来显示为手柄,则当用户触摸它时将不会有拖动效果。我明白这是因为滚动控制器没有发生滚动。如果我将句柄放在蓝色框中下面的 listView 内,它将按预期向上滚动。然而,在顶部,当 listView 有更多项目时,手柄将向上滚动。

当用户触摸并拖动手柄时,有什么方法可以手动向上滚动工作表吗?

DraggableScrollableSheet 的手柄栏

Kau*_*dru 6

请尝试这个

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool leftClick = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DraggableScrollableSheet(
        minChildSize: 0.1,
        maxChildSize: 0.9,
        initialChildSize: 0.3,
        builder: (BuildContext context, ScrollController scrollController) {
          return Container(
            height: MediaQuery.of(context).size.height * 0.9,
            width: MediaQuery.of(context).size.width,
            color: Colors.green,
            child: Stack(
              children: [
                Container(
                    height: MediaQuery.of(context).size.height * 0.9,
                    width: MediaQuery.of(context).size.width,
                    child: SingleChildScrollView(
                      controller: scrollController,
                      child: Column(
                        children: [
                          const SizedBox(
                            height: 50,
                          ),
                          ...List.generate(
                              50,
                              (index) => Container(
                                  height: 50, child: Text('Container $index')))
                        ],
                      ),
                    )),
                IgnorePointer(
                  child: Container(
                    color: Colors.green,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Container(
                          margin: EdgeInsets.only(top: 20, bottom: 20),
                          height: 10,
                          width: 100,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10),
                              color: Colors.grey),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

预览