draggable_scrollable_lock_at_top_sheet 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

draggable_scrollable_lock_at_top_sheet #

增强版flutter DraggableScrollableSheet,增强点:

  • 滚动区域可以锁定在maxChildSize位置,例如下载加载上一页的时候。
  • 可拖拽扩大,原始的版本是能在ScrollView上执行拖拽,增强后可以在任意位置上拖拽。

运行Demo #

图片名称
  • 克隆代码到本地: git clone git@github.com:SBDavid/draggable_scrollable_lock_at_top_sheet.git
  • 切换工作路径: cd draggable_scrollable_lock_at_top_sheet/example/
  • 启动模拟器
  • 运行: flutter run

使用文档 #

1. 安装 #

dependencies:
  draggable_scrollable_lock_at_top_sheet: ^1.0.0

2. 引用插件 #

import 'package:draggable_scrollable_lock_at_top_sheet/draggable_scrollable_lock_at_top_sheet.dart';

3. 使用组件 #

在原始的DraggableScrollableSheet的基础增加了shouldLockAtTop接口,用于判断是否需要锁定在顶部。

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: appBarColor,
        title: Text(widget.title),
      ),
      body: Stack(
        children: <Widget>[
          Container(
            child: RaisedButton(
              child: Text("这个区域可以出发点击事件 当前页码:$currentPageNum"),
              onPressed: () {
                print("当前页码:$currentPageNum");
              },
            ),
          ),
          LayoutBuilder(
              builder: (BuildContext context, BoxConstraints constraints) {

                // 计算顶部图片应该保留的空间
                double minChildSize = (constraints.maxHeight - PIC_HEIGHT) / constraints.maxHeight;

                return NotificationListener(
                  onNotification: (Notification notification) {
                    if (notification is DraggableScrollableLockAtTopNotification) {
                      // 调整appbar颜色
                      appBarColor = appBarColor.withAlpha(((notification.extent-notification.minExtent)/(notification.maxExtent - notification.minExtent) * 255).ceil());
                      setState(() {

                      });
                    }
                    return false;
                  },
                  child: DraggableScrollableSheetLockAtTop(
                    minChildSize: minChildSize,
                    maxChildSize: 1,
                    initialChildSize: minChildSize,
                    // 判断是否加载到第一页
                    shouldLockAtTop: () {
                      return true;
                    },
                    builder: (BuildContext context,
                        ScrollController scrollController) {
                      return Column(
                        children: <Widget>[
                          Container(
                            color: Colors.red,
                            height: 80,
                            width: double.infinity,
                            child: Text("这里可以拖动,滑动到最上会触发锁定,刷新到第一页可以解锁,滑动红色区域也可以解锁"),
                          ),
                          Expanded(
                            child: Container(
                              color: Colors.indigo,
                              child: CustomScrollView(
                                physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
                                controller: scrollController,
                                slivers: <Widget>[
                                  // 下拉加载更多,可以使用别的方式加载更多,这里只做演示。
                                  CupertinoSliverRefreshControl(
                                    onRefresh: () async {
                                      Future.delayed(Duration(seconds: 1), () async {
                                        if (currentPageNum > 0) {
                                          currentPageNum--;
                                          for(int i=0; i<pageSize; i++) {
                                            items.insert(0, 1);
                                          }
                                        }
                                        setState(() {

                                        });
                                      });
                                    },
                                  ),
                                  SliverList(
                                    delegate: SliverChildBuilderDelegate(
                                            (BuildContext context, int index) {
                                          return ListTile(title: Text('Item $index'));
                                        },
                                        childCount: items.length
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          )
                        ],
                      );
                    },
                  ),
                );
              }
          )
        ],
      )
    );
  }
}

[0.0.1] - TODO: Add release date. #

[1.0.0] - 增强:可以在顶部锁定,并加载更多。在scrollview以外执行拖拽。执行拖拽时停止scrollview内的滚动。 #

[1.0.1] - 增加readme文档 #

[1.0.2] - bugfix:下拉刷新回弹 #

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:draggable_scrollable_lock_at_top_sheet/draggable_scrollable_lock_at_top_sheet.dart';
import 'package:flutter_sliver_tracker/flutter_sliver_tracker.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  // 导航栏颜色
  Color appBarColor = Colors.blue.withOpacity(0);
  // 当前页码
  int currentPageNumUP = 10;
  int currentPageNumDown = 10;
  int pageSize = 10;
  int pageAmount = 20;
  static const double PIC_HEIGHT = 200;
  // 列表项
  List<int> items;

  @override
  void initState() {
    super.initState();
    items = [1,1,1,1,1,1,1,1,1,1];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: appBarColor,
        title: Text(widget.title),
      ),
      body: Stack(
        children: <Widget>[
          Container(
            child: RaisedButton(
              child: Text("这个区域可以出发点击事件 当前页码:$currentPageNumUP / $currentPageNumDown"),
              onPressed: () {
                print("当前页码:$currentPageNumUP");
              },
            ),
          ),
          LayoutBuilder(
              builder: (BuildContext context, BoxConstraints constraints) {

                // 计算顶部图片应该保留的空间
                double minChildSize = (constraints.maxHeight - PIC_HEIGHT) / constraints.maxHeight;

                return NotificationListener(
                  onNotification: (Notification notification) {
                    print("notification $notification");
                    if (notification is DraggableScrollableLockAtTopNotification) {
                      // 调整appbar颜色
                      appBarColor = appBarColor.withAlpha(((notification.extent-notification.minExtent)/(notification.maxExtent - notification.minExtent) * 255).ceil());
                      setState(() {

                      });
                    }
                    return false;
                  },
                  child: DraggableScrollableSheetLockAtTop(
                    minChildSize: minChildSize,
                    maxChildSize: 1,
                    initialChildSize: minChildSize,
                    // 判断是否加载到第一页
                    shouldLockAtTop: () {
                      return currentPageNumUP != 1;
                    },
                    builder: (BuildContext context,
                        ScrollController scrollController) {
                      return Column(
                        children: <Widget>[
                          Container(
                            color: Colors.red,
                            height: 80,
                            width: double.infinity,
                            child: Text("这里可以拖动,滑动到最上会触发锁定,刷新到第一页可以解锁,滑动红色区域也可以解锁"),
                          ),
                          Expanded(
                            child: Container(
                              color: Colors.indigo,
                              child: ScrollViewListener(
                                child: CustomScrollView(
                                  physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
                                  controller: scrollController,
                                  slivers: <Widget>[
                                    // 下拉加载更多,可以使用别的方式加载更多,这里只做演示。
                                    CupertinoSliverRefreshControl(
                                      onRefresh: () async {
                                        Future.delayed(Duration(seconds: 1), () async {
                                          if (currentPageNumUP > 1) {
                                            currentPageNumUP--;
                                            for(int i=0; i<pageSize; i++) {
                                              items.insert(0, 1);
                                            }
                                          }
                                          setState(() {

                                          });
                                        });
                                      },
                                    ),
                                    SliverList(
                                      delegate: SliverChildBuilderDelegate(
                                              (BuildContext context, int index) {
                                            return ListTile(
                                                title: Text(
                                                    'Item $index',
                                                  style: TextStyle(
                                                    color: Colors.white,
                                                    fontSize: 60
                                                  ),
                                                )
                                            );
                                          },
                                          childCount: items.length
                                      ),
                                    ),
                                    SliverToBoxAdapter(
                                      child: SliverScrollListenerDebounce(
                                        notifyOnce: true,
                                        onScrollEnd: (double percent) {
                                          // 加载下一页
                                          Future.delayed(Duration(seconds: 1), () {
                                            if (currentPageNumDown < pageAmount) {
                                              currentPageNumDown++;
                                              for(int i=0; i<pageSize; i++) {
                                                items.add(1);
                                              }
                                              setState(() {

                                              });
                                            } else {
                                              print("到底了");
                                            }
                                          });
                                        },
                                        child: Container(
                                          color: Colors.lightBlue,
                                          height: 100,
                                          child: Center(
                                            child: Text("$currentPageNumDown / $pageAmount",
                                              style: TextStyle(
                                                color: Colors.white,
                                                fontSize: 60
                                            )),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          )
                        ],
                      );
                    },
                  ),
                );
              }
          )
        ],
      )
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  draggable_scrollable_lock_at_top_sheet: ^1.0.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:draggable_scrollable_lock_at_top_sheet/draggable_scrollable_lock_at_top_sheet.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
40
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [draggable_scrollable_lock_at_top_sheet] that is in a package requiring null.

Health suggestions

Fix lib/widget.dart. (-1 points)

Analysis of lib/widget.dart reported 2 hints:

line 615 col 54: 'inheritFromWidgetOfExactType' is deprecated and shouldn't be used. Use dependOnInheritedWidgetOfExactType instead. This feature was deprecated after v1.12.1..

line 667 col 44: 'inheritFromWidgetOfExactType' is deprecated and shouldn't be used. Use dependOnInheritedWidgetOfExactType instead. This feature was deprecated after v1.12.1..

Format lib/draggable_scrollable_lock_at_top_sheet.dart.

Run flutter format to format lib/draggable_scrollable_lock_at_top_sheet.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test