text
stringlengths
1
372
final string name;
final ImageProvider imageProvider;
final List<Item> items;
string get formattedTotalItemPrice {
final totalPriceCents =
items.fold<int>(0, (prev, item) => prev + item.totalPriceCents);
return '\$${(totalpricecents / 100.0).tostringasfixed(2)}';
}
}
<code_end>
the CustomerCart widget displays the customer’s photo,
name, total, and item count based on a customer instance.
to update a customer’s cart when a menu item is dropped,
add the dropped item to the associated customer object.
<code_start>
void _itemDroppedOnCustomerCart({
required item item,
required customer customer,
}) {
setState(() {
customer.items.add(item);
});
}
<code_end>
the _itemDroppedOnCustomerCart method is invoked in
onAcceptWithDetails() when the user drops a menu item on a
CustomerCart widget. by adding the dropped item to the
customer object, and invoking setState() to cause a
layout update, the UI refreshes with the new customer’s
price total and item count.
congratulations! you have a drag-and-drop interaction
that adds food items to a customer’s shopping cart.
<topic_end>
<topic_start>
interactive example
run the app:
<code_start>
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: ExampleDragAndDrop(),
debugShowCheckedModeBanner: false,
),
);
}
const List<Item> _items = [
item(
name: 'spinach pizza',
totalPriceCents: 1299,
uid: '1',
imageProvider: NetworkImage('https://flutter'
'.dev/docs/cookbook/img-files/effects/split-check/food1.jpg'),
),
item(
name: 'veggie delight',
totalPriceCents: 799,
uid: '2',
imageProvider: NetworkImage('https://flutter'
'.dev/docs/cookbook/img-files/effects/split-check/food2.jpg'),
),
item(
name: 'chicken parmesan',
totalPriceCents: 1499,
uid: '3',
imageProvider: NetworkImage('https://flutter'
'.dev/docs/cookbook/img-files/effects/split-check/food3.jpg'),
),
];
@immutable
class ExampleDragAndDrop extends StatefulWidget {
const ExampleDragAndDrop({super.key});
@override
State<ExampleDragAndDrop> createState() => _ExampleDragAndDropState();
}
class _ExampleDragAndDropState extends State<ExampleDragAndDrop>
with TickerProviderStateMixin {
final List<Customer> _people = [
customer(
name: 'makayla',
imageProvider: const NetworkImage('https://flutter'
'.dev/docs/cookbook/img-files/effects/split-check/avatar1.jpg'),
),
customer(
name: 'nathan',
imageProvider: const NetworkImage('https://flutter'
'.dev/docs/cookbook/img-files/effects/split-check/avatar2.jpg'),
),
customer(
name: 'emilio',
imageProvider: const NetworkImage('https://flutter'
'.dev/docs/cookbook/img-files/effects/split-check/avatar3.jpg'),
),
];
final GlobalKey _draggableKey = GlobalKey();
void _itemDroppedOnCustomerCart({
required item item,
required customer customer,
}) {
setState(() {