嗨,我正在做一个食品订购应用程序,当用户从购物车中删除商品时,我需要一些帮助,然后如果您有任何建议,我应该自动计算价格,请让我知道谢谢 当单击删除的按钮时,我已经尝试过刷新页面,但是我认为我可以执行推键功能
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:resat/BurgerListView/const/themeColor.dart';
import 'dart:convert';
import 'DataTableDemo.dart';
import 'Employee.dart';
import 'services.dart';
class FavoritesPage extends StatefulWidget {
@override
FavoritesPageState createState() => FavoritesPageState();
}
class FavoritesPageState extends State<FavoritesPage> {
List<Employee> _employees;
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.deepOrange,
),
home: new Scaffold(
body: new Center(
//FutureBuilder is a widget that builds itself based on the latest snapshot
// of interaction with a Future.
child: new FutureBuilder<List<Employee>>(
future: Services.getEmployees(),
//we pass a BuildContext and an AsyncSnapshot object which is an
//Immutable representation of the most recent interaction with
//an asynchronous computation.
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Employee> _employee = snapshot.data;
Services.getEmployees();
return Scaffold(
body: SafeArea(
child: CustomListView(_employee),
),
bottomNavigationBar: BottomBar(_employee),
);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
//return a circular progress indicator.
return new CircularProgressIndicator();
},
),
),
),
);
}
}
class CustomListView extends StatefulWidget {
List<Employee> _employee;
CustomListViewState createState() => CustomListViewState();
CustomListView(this._employee);
}
class CustomListViewState extends State<CustomListView> {
List<Employee> _employee;
Widget build(context) {
return ListView.builder(
itemCount: _employee.length,
itemBuilder: (context, int currentIndex) {
return createViewItem(_employee[currentIndex], context);
},
);
}
@override
void initState() {
_employee = [];
_getEmployees();
}
Widget createViewItem(Employee _employee, BuildContext context) {
return new Card(
child: new Column(
children: <Widget>[
new ListTile(
leading: new Image.asset(_employee.path, fit: BoxFit.cover),
title: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_employee.firstName,
style: new TextStyle(
fontSize: 19.0,
fontWeight: FontWeight.bold,
),
),
Row(
children: <Widget>[
FlatButton(
child: Text('-', style: TextStyle(fontSize: 28.0)),
onPressed: () {},
),
Text(_employee.quantity),
FlatButton(
child: Text(
'+',
style: TextStyle(fontSize: 28.0),
),
onPressed: () {},
),
],
)
],
),
subtitle: new Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
IconButton(
onPressed: () {
Services.deleteEmployee(_employee.id);
_getEmployees();
/*
Navigator.of(context).push(
new MaterialPageRoute(
builder: (BuildContext context){
return new FavoritesPage();
}
)
);
*/
},
icon: Icon(Icons.delete),
),
new Text('\$' + _employee.price.toString(),
style: new TextStyle(
fontSize: 15.0, fontWeight: FontWeight.normal)),
]),
//trailing: ,
onTap: () {},
)
],
));
}
void _getEmployees() {
Services.getEmployees().then((employees) {
setState(() {
_employee = employees;
});
print("Length ${employees.length}");
});
}
}
class BottomBar extends StatefulWidget {
@override
BottomBarState createState() => BottomBarState(this._employee);
final List<Employee> _employee;
BottomBar(this._employee);
}
class BottomBarState extends State<BottomBar>{
final List<Employee> _employee;
BottomBarState(this._employee);
@override
void initState() {
returnTotalAmount(_employee);
}
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(left: 35, bottom: 25),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
totalAmount(_employee),
nextButtonBar(),
],
),
);
}
Container totalAmount(List<Employee> _employee) {
return Container(
margin: EdgeInsets.only(right: 10),
padding: EdgeInsets.all(5),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Total:",
style: TextStyle(fontSize: 25, fontWeight: FontWeight.w300),
),
Text(
"\$${returnTotalAmount(_employee)}",
style: TextStyle(fontWeight: FontWeight.w700, fontSize: 28),
),
],
),
);
}
String returnTotalAmount(List<Employee> _employee) {
double totalAmount = 0.0;
for (int i = 0; i < _employee.length; i++) {
totalAmount = totalAmount + (double.parse(_employee[i].price)*double.parse(_employee[i].quantity));
}
return totalAmount.toString();
}
Container nextButtonBar() {
return Container(
margin: EdgeInsets.only(right: 25),
padding: EdgeInsets.all(25),
decoration: BoxDecoration(
color: Themes.color, borderRadius: BorderRadius.circular(15)),
child: Row(
children: <Widget>[
Text(
"15-25 min",
style: TextStyle(
fontWeight: FontWeight.w800,
fontSize: 14,
),
),
Spacer(),
Text(
"Next",
style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: 16,
),
),
],
),
);
}
}
答案 0 :(得分:0)
老实说,将代码复制到编辑器是一个挑战,需要小部件和类的数量是另一个挑战,所以我做了最少的工作,希望它可以解决您的问题。
在这里,我有一个双精度数列表和两个Future函数,一个返回所有项目,另一个则从列表中删除一个项目。
我为每个项目制作了ListTile
,如果用户点击删除图标,它将从列表中删除,并且将重新计算总和。
import 'package:flutter/material.dart';
void main() {
runApp(Home());
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Future<List<double>> getData() async {
return items;
}
Future<double> removeIt(int index) async {
return items.removeAt(index);
}
List<double> items = [100.0, 200.0, 300.0, 400.0];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: FutureBuilder<List<double>>(
future: getData(),
builder: (BuildContext context, AsyncSnapshot<List<double>> snapshot) {
if (snapshot.hasData)
return Column(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) => ListTile(
title: Text('Items : ${snapshot.data[index]}'),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () async {
await removeIt(index);
setState(() {});
}),
),
),
Container(
child:
Text('Sum is: ${snapshot.data.fold(0, (a, b) => a + b)}'),
)
],
);
return Center(child: CircularProgressIndicator());
},
),
));
}
}