Issue
I am trying to implement a function to delete the selected item from the list on long press after confirmation from the user to delete ('Yes' button and 'No' button). I am using Inkwell.
I tried a few various ways but couldn't find anything.
One similar post I found but it was not useful.
Note: I don't want to use Dismissable
Here is the code: -
import 'package:flutter/material.dart';
import 'package:smoorapplication/src/services/api.dart';
import 'package:percent_indicator/percent_indicator.dart';
class BodyFunctionCard extends StatefulWidget{
const BodyFunctionCard({Key key}) : super(key: key);
@override
_BodyFunctionCardHPState createState() => _BodyFunctionCardHPState();
}
class _BodyFunctionCardHPState extends State<BodyFunctionCard>{
List<bool> isExpanded = List.generate(100, (index) => false);
List<bool> isAccepted = List.generate(100,(index) => false);
List<bool> isPicked = List.generate(100,(index) => false);
List<bool> isArrived = List.generate(100,(index) => false);
List<bool> isDelRet = List.generate(100,(index) => false);
List<bool> isFinished = List.generate(100,(index) => false);
List<bool> countfin = List.generate(100,(index) => false);
List<double> lpind = List.generate(100, (index) => 0.0);
@override
Widget build(BuildContext context){
return Card(
child: FutureBuilder(
future: apiGetOrder(),
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.connectionState == ConnectionState.none || snapshot.hasData == null){
return const Center(child:
Text('Loading data . . . !'),);
}return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Column(
children: <Widget>[
InkWell(child:
Container(
height: 200,
width: 380,
margin: const EdgeInsets.all(0.0),
decoration: BoxDecoration(
border: Border.all(width: 1.5, color: Colors.grey),
),
child: Column(children: <Widget>[
const SizedBox(height: 3.5,),
Container(
height: 30,
width: 370,
color: const Color.fromRGBO(220,220,220,2.0),
child: Row(children: <Widget>[
Text(' ${snapshot.data[index].orderNumber}', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),),
const SizedBox(width: 130,),
LinearPercentIndicator(
width: 100,
lineHeight: 15,
percent: lpind[index],
backgroundColor: Colors.orange,
progressColor: Colors.lightGreen,
),
const SizedBox(width: 10,),
Text(' ${snapshot.data[index].orderNumber}', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),)
],),
),
const SizedBox(height: 10,),
Row(children: <Widget>[
RichText(text: const TextSpan(
children: [
WidgetSpan(
child: Padding(padding: EdgeInsets.symmetric(horizontal: 10.0),child: Icon(Icons.circle, color: Colors.orange, size: 18,),)
)
]
)),
const Text('PICKUP LOCATION', style: TextStyle(fontSize: 16),)
],),
const SizedBox(height: 5,),
Row(children: <Widget>[
Text(' ${snapshot.data[index].lineItems[0].originLocation.name} ${snapshot.data[index].lineItems[0].originLocation.address1}',
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
)
],),
Row(children: <Widget>[
Text(' ${snapshot.data[index].lineItems[0].originLocation.address2}',
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
)
],),
const SizedBox(height: 10,),
Row(children: <Widget>[
RichText(text: const TextSpan(
children: [
WidgetSpan(
child: Padding(padding: EdgeInsets.symmetric(horizontal: 10.0),child: Icon(Icons.circle, color: Colors.lightGreen, size: 18,),)
)
]
)),
const Text('DELIVERY LOCATION', style: TextStyle(fontSize: 16),)
],),
const SizedBox(height: 5,),
Row(children: <Widget>[
Text(' ${snapshot.data[index].lineItems[0].originLocation.name} ${snapshot.data[index].lineItems[0].originLocation.address1}',
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
)
],),
Row(children: <Widget>[
Text(' ${snapshot.data[index].lineItems[0].originLocation.address2}',
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
)
],)
],),
),
onTap: (){
setState(() {
isExpanded[index] = !isExpanded[index];
});
},
onLongPress: (){
setState(() {
// I want to delete the item at the current index.
});
},
),
const SizedBox(height: 10,),
],
);
}
);
}
),
);
}
}
I am new to Flutter, I will help others when I gain some knowledge.
Solution
STEP 1:FIND THE INDEX Of PARTICULAR OBJECT(element) VIA
Your_List_Name.indexOf(Your_List_Name[index]);
ex:_userData.indexOf(_userData[index])
Step 2:REMOVE THE OBJECT AT PARTICULAR INDEX
Your_LIST_Name.removeAt(index);
ex: _userData.removeAt(index);
According your code data is your list name
Answered By - Anmol Mishra