问题描述
您好,我正在尝试构建一个资金管理应用程序。 我在将所有数字添加到列表中时遇到问题。
我的想法是我有2个按钮(绿色和红色)。用户可以添加一个数字,然后点击绿色或红色以显示收到的钱或已花费的钱。
我无法获得列表总数,也无法对列表进行数学运算。
我可以使用其他替代方法吗?
import 'package:Flutter/material.dart';
import 'package:list_ext/list_ext.dart';
class HomeScreen extends StatefulWidget {
static final String routeName = "/home";
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final myController = TextEditingController();
List<String> in_out = ["One","Two","Three","Four"];
List<int> amount = [1,2,3,4];
// int sum = amount.reduce((a,b) => a + b);
var money = 0;
var newMoney = 0;
var cash = 0;
var income = 0;
var spent = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
resizetoAvoidBottomPadding: false,appBar: buildAppBar(),body: buildBody(),);
}
Widget buildAppBar(){
return AppBar(
title: Text('Money Managment App'),);
}
Widget buildTotal(){
return Text('$newMoney');// res is 15
}
Widget buildBody(){
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
GestureDetector(
child: Icon(Icons.chevron_left,color: Colors.red,size: 40,),onTap: (){
addExpense();
},Container(
width: 150,child: TextField(
controller: myController,keyboardType: TextInputType.number,autocorrect: true,GestureDetector(
child: Icon(Icons.chevron_right,color: Colors.green,onTap: () {
addIncome();
},],buildTotal(),SizedBox(height: 20,Container(
height: 350,child: ListView.builder
(
itemCount: in_out.length,itemBuilder: (BuildContext ctxt,int index) {
return Container(
height: 30,margin: EdgeInsets.all(2),color: Colors.white,child: Center(
child: Text('${in_out[index]} (${amount[index]})',style: TextStyle(fontSize: 18),)
),);
}
)
),);
}
void total(){
income = cash + int.parse(myController.text);
newMoney = money + income;
}
void addIncome(){
setState(() {
in_out.add('Income',);
amount.add(int.parse(myController.text));
total();
});
}
void addExpense(){
setState(() {
in_out.add('Expense');
amount.add(int.parse(myController.text));
});
}
}
我是新来的人。
任何有关学习的技巧或视频建议将不胜感激。 预先谢谢你。
解决方法
您可以在下面复制粘贴运行完整代码
步骤1:您可以使用class Money
和List<Money>
来记录历史记录
步骤2:您可以将Income
和Expense
字符串传递给total
函数,然后计算
代码段
class Money {
String in_out;
int amount;
Money({this.in_out,this.amount});
}
void total(String in_out) {
//income = cash + int.parse(myController.text);
if (in_out == 'Income') {
newMoney = newMoney + int.parse(myController.text);
} else {
newMoney = newMoney - int.parse(myController.text);
}
}
void addIncome() {
setState(() {
moneyList
.add(Money(in_out: 'Income',amount: int.parse(myController.text)));
/* in_out.add('Income',);
amount.add(int.parse(myController.text));*/
total('Income');
});
}
void addExpense() {
setState(() {
moneyList
.add(Money(in_out: 'Expense',amount: int.parse(myController.text)));
/*in_out.add('Expense');
amount.add(int.parse(myController.text));*/
total('Expense');
});
}
工作演示
完整代码
import 'package:flutter/material.dart';
class Money {
String in_out;
int amount;
Money({this.in_out,this.amount});
}
class HomeScreen extends StatefulWidget {
static final String routeName = "/home";
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final myController = TextEditingController();
List<Money> moneyList = [];
// List<String> in_out = ["One","Two","Three","Four"];
//List<int> amount = [1,2,3,4];
// int sum = amount.reduce((a,b) => a + b);
var money = 0;
var newMoney = 0;
var cash = 0;
var income = 0;
var spent = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,appBar: buildAppBar(),body: buildBody(),);
}
Widget buildAppBar() {
return AppBar(
title: Text('Money Managment App'),);
}
Widget buildTotal() {
return Text('$newMoney'); // res is 15
}
Widget buildBody() {
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
GestureDetector(
child: Icon(
Icons.chevron_left,color: Colors.red,size: 40,),onTap: () {
addExpense();
},Container(
width: 150,child: TextField(
controller: myController,keyboardType: TextInputType.number,autocorrect: true,GestureDetector(
child: Icon(
Icons.chevron_right,color: Colors.green,onTap: () {
addIncome();
},],buildTotal(),SizedBox(
height: 20,Container(
height: 350,child: ListView.builder(
itemCount: moneyList.length,itemBuilder: (BuildContext ctxt,int index) {
return Container(
height: 30,margin: EdgeInsets.all(2),color: Colors.white,child: Center(
child: Text(
'${moneyList[index].in_out} (${moneyList[index].amount})',style: TextStyle(fontSize: 18),)),);
})),);
}
void total(String in_out) {
//income = cash + int.parse(myController.text);
if (in_out == 'Income') {
newMoney = newMoney + int.parse(myController.text);
} else {
newMoney = newMoney - int.parse(myController.text);
}
}
void addIncome() {
setState(() {
moneyList
.add(Money(in_out: 'Income',amount: int.parse(myController.text)));
/*in_out.add('Expense');
amount.add(int.parse(myController.text));*/
total('Expense');
});
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',theme: ThemeData(
primarySwatch: Colors.blue,visualDensity: VisualDensity.adaptivePlatformDensity,home: HomeScreen(),);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key,this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
Text(
'You have pushed the button this many times:',Text(
'$_counter',style: Theme.of(context).textTheme.headline4,floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,tooltip: 'Increment',child: Icon(Icons.add),);
}
}