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

How to use SliverReorderableGrid? #4

Closed
anjinwang opened this issue Dec 30, 2021 · 1 comment
Closed

How to use SliverReorderableGrid? #4

anjinwang opened this issue Dec 30, 2021 · 1 comment
Labels
question Further information is requested

Comments

@anjinwang
Copy link

Regarding the use of SliverReorderableGrid, can you provide a code demo? I have a problem when I use it, I can’t drag, the code is as follows

import 'package:flutter/material.dart';
import 'package:reorderable_grid/reorderable_grid.dart';

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

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

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

class _MyAppState extends State<MyApp> {
  final items = List<int>.generate(40, (index) => index);

  void _onReorder(int oldIndex, int newIndex) {
    setState(() {
      final item = items.removeAt(oldIndex);
      items.insert(newIndex, item);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: CustomScrollView(
        slivers: [
          SliverReorderableGrid(
              itemCount: items.length,
              itemBuilder: (context, index) => Card(
                    key: ValueKey(index),
                    child: Center(
                      child: Text(index.toString()),
                    ),
                  ),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                childAspectRatio: 3 / 5,
                mainAxisSpacing: 10.0,
                crossAxisSpacing: 10.0,
              ),
              onReorder: _onReorder)
        ],
      )),
    );
  }
}
@casvanluijtelaar
Copy link
Owner

you'll need to wrap your children in a ReorderableGridDragStartListener. You can find more info in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants