Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After dragging, scroll to the top, #34

Open
MiaoShichang opened this issue May 19, 2024 · 0 comments
Open

After dragging, scroll to the top, #34

MiaoShichang opened this issue May 19, 2024 · 0 comments

Comments

@MiaoShichang
Copy link

Problem: After dragging, scroll to the top

want : After dragging, don't scroll to the top

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_draggable_gridview/flutter_draggable_gridview.dart';

class TPPinJieHomePage extends StatefulWidget {
  const TPPinJieHomePage({super.key});

  @override
  State<TPPinJieHomePage> createState() => _TPPinJieHomePageState();
}

class _TPPinJieHomePageState extends State<TPPinJieHomePage> {
  final List<int> _datalist = [];

  @override
  void initState() {
    super.initState();
    for (int i = 0; i < 50; i++) {
      _datalist.add(i);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: const CupertinoNavigationBar(
          middle: Text("drag"),
        ),
        body: DraggableGridViewBuilder(
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
            crossAxisSpacing: 1,
            mainAxisSpacing: 1,
          ),
          dragCompletion: (List<DraggableGridItem> list, int beforeIndex, int afterIndex) {
            print('onDragAccept: $beforeIndex -> $afterIndex');
            int one = _datalist[beforeIndex];
            if (afterIndex > beforeIndex) {
              _datalist.insert(afterIndex + 1, one);
              _datalist.removeAt(beforeIndex);
            } else if (afterIndex < beforeIndex) {
              _datalist.insert(afterIndex, _datalist[beforeIndex]);
              _datalist.removeAt(beforeIndex + 1);
            }
          },
          children: _getItems(),
          dragFeedback: (List<DraggableGridItem> list, int index) {
            return SizedBox(
              width: 100,
              height: 100,
              child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.red, width: 2),
                  ),
                  child: list[index].child),
            );
          },
        ));
  }

  List<DraggableGridItem> _getItems() {
    List<DraggableGridItem> items = [];
    for (var one in _datalist) {
      items.add(DraggableGridItem(
        isDraggable: true,
        child: Container(
          color: Colors.orange,
          padding: const EdgeInsets.all(10),
          child: Center(child: Text("$one")),
        ),
      ));
    }

    items.add(DraggableGridItem(
      child: Container(
        color: Colors.orange,
        padding: const EdgeInsets.all(10),
        child: const Center(child: Text("picker")),
      ),
    ));
    return items;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant