如何使用 sqflite 将 sqlite 数据库的索引传递到 flutter 应用程序中的另一个页面?
How to pass index of sqlite database to another page in flutter app using sqflite?
我正在制作一个应用程序,用户可以在其中保存交易数据,他们可以通过填写以下表格来完成
然后交易将保存在数据库中,并可以在以下屏幕上向用户显示
现在我想要的是,如果我点击其中一个交易,我应该被重定向到另一个页面,为此,我需要在 onTap() 中传递交易索引,以便它可以传递到另一个页面屏幕,我说得对吗?如果是,请帮助我,我是 flutter 的新手,我不知道该怎么做。
我用来创建数据库的代码是:
await database.execute(
"CREATE TABLE $TABLE_EXPENSES ("
"$COLUMN_ID INTEGER PRIMARY KEY,"
"$COLUMN_NAME TEXT,"
"$COLUMN_AMOUNT INTEGER,"
"$COLUMN_UNNECESSARYEXPENSES INTEGER,"
"$COLUMN_CATEGORY TEXT,"
"$COLUMN_DATETIME TEXT,"
"$COLUMN_COMMENTS TEXT"
")",
);
我用于映射的代码:
class expense {
int id;
String name;
int amount;
bool isUnnecessaryExpenses;
String category;
DateTime dateTime;
String pickedDate;
String comments;
expense({this.id, this.name, this.amount, this.isUnnecessaryExpenses, this.category, this.dateTime, this.pickedDate, this.comments});
Map toMap() {
var map = {
DatabaseProvider.COLUMN_NAME: name,
DatabaseProvider.COLUMN_AMOUNT: amount,
DatabaseProvider.COLUMN_UNNECESSARYEXPENSES: isUnnecessaryExpenses ? 1 : 0,
DatabaseProvider.COLUMN_CATEGORY: category,
DatabaseProvider.COLUMN_DATETIME: pickedDate,
DatabaseProvider.COLUMN_COMMENTS: comments,
};
if (id != null) {
map[DatabaseProvider.COLUMN_ID] = id;
}
return map;
}
expense.fromMap(Map map) {
id = map[DatabaseProvider.COLUMN_ID];
name = map[DatabaseProvider.COLUMN_NAME];
amount = map[DatabaseProvider.COLUMN_AMOUNT];
isUnnecessaryExpenses = map[DatabaseProvider.COLUMN_UNNECESSARYEXPENSES] == 1;
category= map[DatabaseProvider.COLUMN_CATEGORY];
pickedDate = map[DatabaseProvider.COLUMN_DATETIME];
comments = map[DatabaseProvider.COLUMN_COMMENTS];
}
}
我正在使用以下代码显示交易:
class ExpenseList extends StatefulWidget {
const ExpenseList({Key key}) : super(key: key);
@override
_ExpenseListState createState() => _ExpenseListState();
}
class _ExpenseListState extends State<ExpenseList> {
@override
void initState() {
super.initState();
DatabaseProvider.db.getExpenses().then(
(ExpenseList) {
BlocProvider.of<ExpenseBloc>(context).add(SetExpenses(ExpenseList));
},
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: BlocConsumer<ExpenseBloc, List<expense>>(
builder: (context, expenseList) {
return ListView.separated(
itemBuilder: (BuildContext context, int index) {
expense expensess = expenseList[index];
return Container(
margin: const EdgeInsets.all(5.0),
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: index % 2 == 0 ? Colors.white : Colors.lightBlueAccent,
border: Border.all(width: 2,color: Colors.white),
borderRadius: BorderRadius.circular(15)
),
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.indigo,
foregroundColor: Colors.white,
child: Text(
expensess.name
.substring(0, 1)
.toUpperCase(),
style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
),
),
title: Text(
expensess.name ?? "Title",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30,
color: Colors.black,
),
),
subtitle: Text(
"Amount: ${expensess.amount.toString()}"
"\nDate: ${expensess.pickedDate}\n"
,style: TextStyle(
fontStyle: FontStyle.italic,
fontSize: 20,
color: Colors.black,
),
),
onTap: () {
var currID= expensess.id; ///// line 1 /////////
Navigator.push(context, MaterialPageRoute(builder: (context)=> ViewTransaction()));
}
));
},
itemCount: expenseList.length,
separatorBuilder: (BuildContext context, int index) => Divider(color: Colors.black),
);
},
listener: (BuildContext context, ExpenseList) {},
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (BuildContext context) => ExpenseForm()),
),
),
);
}
}
我正尝试按照 ///// 第 1 行 ///////// 中所示进行操作,但我不确定,而且我我不知道如何在 viewTransaction 页面中执行此操作。
请帮助我。
感谢您的回复
您可以将 id 作为参数传递给 ViewTransaction Widget
int expenseId = expensess.id;
Navigator.push(context, MaterialPageRoute(builder: (context)=> ViewTransaction(expenseId:expenseId)));
您的 ViewTransaction 小部件应该是这样的。
class ViewTransaction extends StatelessWidget {
final int expenseId;
ViewTransaction({this.expenseId});
@override
Widget build(BuildContext context) {
return Container();
}
}
我正在制作一个应用程序,用户可以在其中保存交易数据,他们可以通过填写以下表格来完成
然后交易将保存在数据库中,并可以在以下屏幕上向用户显示
现在我想要的是,如果我点击其中一个交易,我应该被重定向到另一个页面,为此,我需要在 onTap() 中传递交易索引,以便它可以传递到另一个页面屏幕,我说得对吗?如果是,请帮助我,我是 flutter 的新手,我不知道该怎么做。
我用来创建数据库的代码是:
await database.execute(
"CREATE TABLE $TABLE_EXPENSES ("
"$COLUMN_ID INTEGER PRIMARY KEY,"
"$COLUMN_NAME TEXT,"
"$COLUMN_AMOUNT INTEGER,"
"$COLUMN_UNNECESSARYEXPENSES INTEGER,"
"$COLUMN_CATEGORY TEXT,"
"$COLUMN_DATETIME TEXT,"
"$COLUMN_COMMENTS TEXT"
")",
);
我用于映射的代码:
class expense {
int id;
String name;
int amount;
bool isUnnecessaryExpenses;
String category;
DateTime dateTime;
String pickedDate;
String comments;
expense({this.id, this.name, this.amount, this.isUnnecessaryExpenses, this.category, this.dateTime, this.pickedDate, this.comments});
Map toMap() {
var map = {
DatabaseProvider.COLUMN_NAME: name,
DatabaseProvider.COLUMN_AMOUNT: amount,
DatabaseProvider.COLUMN_UNNECESSARYEXPENSES: isUnnecessaryExpenses ? 1 : 0,
DatabaseProvider.COLUMN_CATEGORY: category,
DatabaseProvider.COLUMN_DATETIME: pickedDate,
DatabaseProvider.COLUMN_COMMENTS: comments,
};
if (id != null) {
map[DatabaseProvider.COLUMN_ID] = id;
}
return map;
}
expense.fromMap(Map map) {
id = map[DatabaseProvider.COLUMN_ID];
name = map[DatabaseProvider.COLUMN_NAME];
amount = map[DatabaseProvider.COLUMN_AMOUNT];
isUnnecessaryExpenses = map[DatabaseProvider.COLUMN_UNNECESSARYEXPENSES] == 1;
category= map[DatabaseProvider.COLUMN_CATEGORY];
pickedDate = map[DatabaseProvider.COLUMN_DATETIME];
comments = map[DatabaseProvider.COLUMN_COMMENTS];
}
}
我正在使用以下代码显示交易:
class ExpenseList extends StatefulWidget {
const ExpenseList({Key key}) : super(key: key);
@override
_ExpenseListState createState() => _ExpenseListState();
}
class _ExpenseListState extends State<ExpenseList> {
@override
void initState() {
super.initState();
DatabaseProvider.db.getExpenses().then(
(ExpenseList) {
BlocProvider.of<ExpenseBloc>(context).add(SetExpenses(ExpenseList));
},
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: BlocConsumer<ExpenseBloc, List<expense>>(
builder: (context, expenseList) {
return ListView.separated(
itemBuilder: (BuildContext context, int index) {
expense expensess = expenseList[index];
return Container(
margin: const EdgeInsets.all(5.0),
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: index % 2 == 0 ? Colors.white : Colors.lightBlueAccent,
border: Border.all(width: 2,color: Colors.white),
borderRadius: BorderRadius.circular(15)
),
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.indigo,
foregroundColor: Colors.white,
child: Text(
expensess.name
.substring(0, 1)
.toUpperCase(),
style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
),
),
title: Text(
expensess.name ?? "Title",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30,
color: Colors.black,
),
),
subtitle: Text(
"Amount: ${expensess.amount.toString()}"
"\nDate: ${expensess.pickedDate}\n"
,style: TextStyle(
fontStyle: FontStyle.italic,
fontSize: 20,
color: Colors.black,
),
),
onTap: () {
var currID= expensess.id; ///// line 1 /////////
Navigator.push(context, MaterialPageRoute(builder: (context)=> ViewTransaction()));
}
));
},
itemCount: expenseList.length,
separatorBuilder: (BuildContext context, int index) => Divider(color: Colors.black),
);
},
listener: (BuildContext context, ExpenseList) {},
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (BuildContext context) => ExpenseForm()),
),
),
);
}
}
我正尝试按照 ///// 第 1 行 ///////// 中所示进行操作,但我不确定,而且我我不知道如何在 viewTransaction 页面中执行此操作。
请帮助我。 感谢您的回复
您可以将 id 作为参数传递给 ViewTransaction Widget
int expenseId = expensess.id;
Navigator.push(context, MaterialPageRoute(builder: (context)=> ViewTransaction(expenseId:expenseId)));
您的 ViewTransaction 小部件应该是这样的。
class ViewTransaction extends StatelessWidget {
final int expenseId;
ViewTransaction({this.expenseId});
@override
Widget build(BuildContext context) {
return Container();
}
}