【问题标题】:how to detect if the DraggableScrollableSheet is dropped down?如何检测 DraggableScrollableSheet 是否被下拉?
【发布时间】:2022-07-05 23:28:14
【问题描述】:

我向 DraggableScrollableSheet 添加了一个监听器。当我对函数 DraggableScrollableNotification DSNotification 上的 DSNotification.extent 进行打印时,它可以工作并且它向我显示最大值为 0.49 但是当我添加 if 测试和 setState 时,我没有得到正确的价值。它不会与 setState 一起使用,当我删除它时它总是给我错误的!​​

   NotificationListener<DraggableScrollableNotification>(
        onNotification:
            (DraggableScrollableNotification DSNotification) {
          if (DSNotification.extent == 0.49) {
           // setState(() {
              //print(DSNotification.extent);
              scrolable = true;
          //  });
          } 
          if (DSNotification.extent <= 0.49) {
            //setState(() {
             scrolable = false;
              //print(DSNotification.extent);
            //});
          }
          print(scrolable);
          return true;
          
        },
        child: DraggableScrollableSheet(
          controller: controller,
          initialChildSize: 0,
          minChildSize: 0.0,
          maxChildSize: 0.49,
          builder: (_, scrollController) {
            return Container(
              decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: Colors.transparent),
                  borderRadius: const BorderRadius.only(
                      topLeft: Radius.circular(15),
                      topRight: Radius.circular(15))),
              child: ListView(controller: scrollController, children: [
                nfController.myWidget,
              ]),
            );
          },
        ),
      ),

【问题讨论】:

标签: flutter


【解决方案1】:

要检测DraggableScrollableSheet 何时被删除,您可以使用Dismissable 包装小部件:

 Dismissible(
          direction: DismissDirection.vertical,
          key: Key('my_item'),
          onDismissed: (direction) {
            print('onDismissed');
          },
          child: DraggableScrollableSheet(
            // controller: controller,
            initialChildSize: 0.5,
            minChildSize: 0.0,
            maxChildSize: 1,
            builder: (_, scrollController) {
              return Container(
                decoration: BoxDecoration(
                    color: Colors.red,
                    border: Border.all(color: Colors.transparent),
                    borderRadius: const BorderRadius.only(
                        topLeft: Radius.circular(15),
                        topRight: Radius.circular(15))),
                child: ListView(controller: scrollController, children: [
                  // nfController.myWidget,
                ]),
              );
            },
          )),

完整的可运行示例:

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 MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: MyHomePage(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Dismissible(
          direction: DismissDirection.vertical,
          key: Key('my_item_'),
          onDismissed: (direction) {
            print('onDismissed');
          },
          child: DraggableScrollableSheet(
            // controller: controller,
            initialChildSize: 0.5,
            minChildSize: 0.0,
            maxChildSize: 1,
            builder: (_, scrollController) {
              return Container(
                decoration: BoxDecoration(
                    color: Colors.red,
                    border: Border.all(color: Colors.transparent),
                    borderRadius: const BorderRadius.only(
                        topLeft: Radius.circular(15),
                        topRight: Radius.circular(15))),
                child: ListView(controller: scrollController, children: [
                  // nfController.myWidget,
                ]),
              );
            },
          )),
    );
  }
}

【讨论】:

  • 我收到此错误以下断言在构建 Dismissible-[](dirty, state: _DismissibleState#aa0a7(tickers: tracking 2 tickers)) 时被抛出:已关闭的 Dismissible 小部件仍然存在树的一部分。
猜你喜欢
  • 2017-02-11
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 2020-07-25
相关资源
最近更新 更多