Flutterexperts

Crafting Next-Gen Apps with Flutter Power
Draggable GridView In Flutter

Draggable GridView is a regular mobile application connection. As the user presses now and again called touch and holds on a widget, another widget appears under the user’s finger, and the user draggable the widget to the last region and conveyances it. On multitouch devices, different draggable can happen at the same time because that there can be various pointers in touch with the devices immediately.

In this article, we will explore the Draggable GridView In Flutter. We will implement a draggable grid view demo program and create a draggable of the GridViewItems using the flutter_draggable_gridview package in your flutter applications.

Table Of Contents::

Draggable GridView

Constructor

Properties

Implementation

Code Implement

Code File

Conclusion



Draggable GridView:

Draggable GridView expands the convenience of the GridView widget in Flutter and offers you the chance of making a reorder of the GridViewItems clear by Draggable. It is too easy to even consider executing and superb to use.

Demo Module :

This demo video shows how to create a draggable grid view in a flutter. It shows how the draggable grid view will work using the flutter_draggable_gridview package in your flutter applications. It shows draggable interaction where the user long/touch presses on a choice of item and then drags it to the picture using the draggable technique. Something like a grid view with draggable can change the position both horizontally and vertically. It will be shown on your device.

Constructor:

To utilize DraggableGridViewBuilder, you need to call the constructor underneath:

const DraggableGridViewBuilder({
Key? key,
required this.gridDelegate,
required this.listOfWidgets,
required this.dragCompletion,
this.isOnlyLongPress = true,
this.dragFeedback,
this.dragChildWhenDragging,
this.dragPlaceHolder,
this.scrollDirection = Axis.vertical,
this.reverse = false,
this.controller,
this.primary,
this.physics,
this.shrinkWrap = false,
this.padding,
this.addAutomaticKeepAlives = true,
this.addRepaintBoundaries = true,
this.addSemanticIndexes = true,
this.cacheExtent,
this.semanticChildCount,
this.dragStartBehavior = DragStartBehavior.start,
this.keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
this.restorationId,
this.clipBehavior = Clip.hardEdge,
})

In the above Constructor, all fields marked with @required must not be empty.

Properties:

There are some properties of DraggableGridViewBuilder are:

  • > listOfWidgets: This property is used to [listOfWidgets] will show the widgets in Gridview.builder.
  • > isOnlyLongPress: This property is used to accept ‘ true’ and ‘false’.
  • > dragFeedback: This property is used to you can set this to display the widget when the widget is being dragged.
  • > dragChildWhenDragging: This property is used to you can set this to display the widget at dragged widget original place when the widget is being dragged.
  • > dragPlaceHolder: This property is used to you can set this to display the widget at the drag target when the widget is being dragged.
  • > dragCompletion: This property is used to you have to set this callback to get the updated list.

Implementation:

Step 1: Add the dependencies

Add dependencies to pubspec — yaml file.

dependencies:
flutter:
sdk: flutter
flutter_draggable_gridview:

Step 2: Import

import 'package:flutter_draggable_gridview/flutter_draggable_gridview.dart';

Step 3: Add the assets

Add assets to pubspec — yaml file.

assets:
- assets/

Step 4: Run flutter packages get in the root directory of your app.

How to implement code in dart file :

You need to implement it in your code respectively:

Create a new dart file called main.dart inside the lib folder.

We will create a Stateful widget and the respective State class has to use with DragFeedback, DragPlaceHolder, DragCompletion.

class MyHomePageState extends State<MyHomePage>
with DragFeedback, DragPlaceHolder, DragCompletion {
}

We will create a list of strings of an image that is equal to a List. generate().

static List<String> listOfImages =
List.generate(12, (index) => 'assets/${index + 1}.jpeg');

Now, we will create a listOf Widgets that are equal to the List. generate(). In bracket, inside we will add listOfImages.length (index), then return a Container widget. In this widget, we will add padding and its child property, we will add an Image. asset(). In bracket, we will add listOfImages[index] and Boxfit is cover.

List<Widget> listOfWidgets = List.generate(
listOfImages.length,
(index) => Container(
padding: EdgeInsets.only(
left: 4.0,
right: 4.0,
top: 8.0,
),
child: Image.asset(
listOfImages[index],
fit: BoxFit.cover,
),
),
);

In the body, we will add DraggableGridViewBuilder() widget. In this widget, we will add gridDelegate was SliverGridDelegateWithFixedCrossAxisCount(). Also add crossAxisCount was 2 and childAspectRatio. We will add listOfWidgets, dragCompletion, isOnlyLongPress, dragFeedback and dragPlaceHolder.

DraggableGridViewBuilder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 3),
),
listOfWidgets: listOfWidgets,
dragCompletion: this,
isOnlyLongPress: false,
dragFeedback: this,
dragPlaceHolder: this,
),

Now, widget feedback is used to can set this to display the widget when the widget is being dragged. We will return the container, it’s child property we will add items. child with width and height.

@override
Widget feedback(List<Widget> list, int index) {
var item = list[index] as Container;
return Container(
child: item.child,
width: 200,
height: 150,
);
}

Now, PlaceHolderWidget will use to show at drag target, when the widget is being dragged. We will return PlaceHolderWidget, it’s child property we will add a Container with white color.

@override
PlaceHolderWidget placeHolder(List<Widget> list, int index) {
return PlaceHolderWidget(
child: Container(
color: Colors.white,
),
);
}

This method, onDragAccept is used to you have to set this callback to get the updated list.

@override
void onDragAccept(List<Widget> list) {

}

When we run the application, we ought to get the screen’s output like the underneath screen capture.

Final Output

Code File:

import 'package:flutter/material.dart';
import 'package:flutter_draggable_gridview/flutter_draggable_gridview.dart';
import 'package:flutter_draggable_gridview_dem/splash_screen.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
),
home: Splash()
);
}
}

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

final String title;

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

class MyHomePageState extends State<MyHomePage>
with DragFeedback, DragPlaceHolder, DragCompletion {
static List<String> listOfImages =
List.generate(12, (index) => 'assets/${index + 1}.jpeg');
List<Widget> listOfWidgets = List.generate(
listOfImages.length,
(index) => Container(
padding: EdgeInsets.only(
left: 4.0,
right: 4.0,
top: 8.0,
),
child: Image.asset(
listOfImages[index],
fit: BoxFit.cover,
),
),
);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.cyan[200],
automaticallyImplyLeading: false,
title: Text(
widget.title,
),
),
body: DraggableGridViewBuilder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 3),
),
listOfWidgets: listOfWidgets,
dragCompletion: this,
isOnlyLongPress: false,
dragFeedback: this,
dragPlaceHolder: this,
),
);
}

@override
Widget feedback(List<Widget> list, int index) {
var item = list[index] as Container;
return Container(
child: item.child,
width: 200,
height: 150,
);
}

@override
PlaceHolderWidget placeHolder(List<Widget> list, int index) {
return PlaceHolderWidget(
child: Container(
color: Colors.white,
),
);
}

@override
void onDragAccept(List<Widget> list) {
}
}

Conclusion:

In the article, I have explained the basic structure of the Draggable GridView in a flutter; you can modify this code according to your choice. This was a small introduction to Draggable GridView On User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying up the Draggable GridView in your flutter projects. We will show you what the Draggable GridView is?. Show constructor and properties of the Draggable GridView. Make a demo program for working Draggable GridView, and it shows draggable interaction where the user long/touch presses on a choice of item and then drags it to the picture using the draggable technique. Something like a grid view with draggable can change the position both horizontally and vertically in your flutter application. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on FacebookGitHubTwitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Leave comment

Your email address will not be published. Required fields are marked with *.