Flutter 我不想在listview中显示表的所有记录,但我只想在Flatter中显示特定客户的记录
我有一个屏幕,显示使用listview的客户列表。接下来,当我单击某个客户时,我希望在listview的下一个屏幕中仅显示该特定客户(customerId)的注释(记录)。我知道如何显示表中的所有记录。但是如何只显示某些记录??没有任何错误,也没有显示listview。我不知道我哪里做错了Flutter 我不想在listview中显示表的所有记录,但我只想在Flatter中显示特定客户的记录,flutter,listview,sqflite,Flutter,Listview,Sqflite,我有一个屏幕,显示使用listview的客户列表。接下来,当我单击某个客户时,我希望在listview的下一个屏幕中仅显示该特定客户(customerId)的注释(记录)。我知道如何显示表中的所有记录。但是如何只显示某些记录??没有任何错误,也没有显示listview。我不知道我哪里做错了 List<CustomerNote> cnoteList; int count = 0; if (cnoteList == null) { cnoteLis
List<CustomerNote> cnoteList;
int count = 0;
if (cnoteList == null) {
cnoteList = List<CustomerNote>();
updateCustomerNotes();
}
列表cnoteList;
整数计数=0;
if(cnoteList==null){
cnoteList=List();
updateCustomerNotes();
}
db_service.dart
Future<List<CustomerNote>> getCustomerNotes(int customer) async {
await DB.init();
var res = await DB.rawQuery("noteDetails WHERE custId = '$customer'");
int count = res.length;
List<CustomerNote> cnotelist = List<CustomerNote>();
for (int i = 0; i < count; i++) {
cnotelist.add(CustomerNote.fromMap(res[i]));
}
return cnotelist;
}
Future-getCustomerNotes(int-customer)异步{
等待DB.init();
var res=await DB.rawQuery(“noteDetails,其中custId='$customer');
int count=res.length;
List cnotelist=List();
for(int i=0;i
People\u List.dart//这是显示客户列表的文件
import 'package:customer/models/Note.dart';
import 'package:customer/models/addCustomer.dart';
import 'package:customer/screens/New_Note.dart';
import 'package:customer/screens/Note_info.dart';
import 'package:customer/screens/User_Settings.dart';
import 'package:customer/screens/add_person.dart';
import 'package:customer/services/db_service.dart';
import 'package:customer/utils/database_helper.dart';
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:sqflite/sqflite.dart';
class People_List extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return People_ListState();
}
}
class People_ListState extends State<People_List> with SingleTickerProviderStateMixin{
DBService dbService = DBService();
List<AddCustomer> customerList;
int count = 0;
static final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
var _isSelectedItemIndex;
TextEditingController _searchQuery;
bool _isSearching = false;
String searchQuery = "Search query";
@override
void initState() {
super.initState();
_searchQuery = new TextEditingController();
}
void _startSearch() {
print("open search box");
ModalRoute
.of(context)
.addLocalHistoryEntry(new LocalHistoryEntry(onRemove: _stopSearching));
setState(() {
_isSearching = true;
});
}
void _stopSearching() {
_clearSearchQuery();
setState(() {
_isSearching = false;
});
}
void _clearSearchQuery() {
print("close search box");
setState(() {
_searchQuery.clear();
updateSearchQuery("Search query");
});
}
Widget _buildTitle(BuildContext context) {
var horizontalTitleAlignment =
Platform.isIOS ? CrossAxisAlignment.center : CrossAxisAlignment.start;
return new InkWell(
onTap: () => scaffoldKey.currentState.openDrawer(),
child: new Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: horizontalTitleAlignment,
children: <Widget>[
const Text(''),
],
),
),
);
}
Widget _buildSearchField() {
return new TextField(
controller: _searchQuery,
autofocus: true,
decoration: const InputDecoration(
hintText: 'Search...',
border: InputBorder.none,
hintStyle: const TextStyle(color: Colors.white30),
),
style: const TextStyle(color: Colors.white, fontSize: 16.0),
onChanged: updateSearchQuery,
);
}
void updateSearchQuery(String newQuery) {
setState(() {
searchQuery = newQuery;
});
print("search query " + newQuery);
}
List<Widget> _buildActions() {
if (_isSearching) {
return <Widget>[
new IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
if (_searchQuery == null || _searchQuery.text.isEmpty) {
Navigator.pop(context);
return;
}
_clearSearchQuery();
},
),
];
}
return <Widget>[
new IconButton(
icon: const Icon(Icons.search),
onPressed: _startSearch,
),
];
}
@override
Widget build(BuildContext context) {
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
if (customerList == null) {
customerList = List<AddCustomer>();
updateListView();
}
return Scaffold(
appBar: new AppBar(
leading: _isSearching ? const BackButton() : null,
title: _isSearching ? _buildSearchField() : _buildTitle(context),
actions: _buildActions(),
),
body:getCustomerListView(),
floatingActionButton: FloatingActionButton(
onPressed: () {
navigateToCustomer(AddCustomer(), 'Add Person');
},
child: const Icon(Icons.add),
),
bottomNavigationBar:Row (
children: <Widget>[
buildNavBarItem(Icons.home,0,"Home"),
buildNavBarItem(Icons.fiber_new_rounded,1,"Upcoming"),
buildNavBarItem(Icons.history,2,"History"),
buildNavBarItem(Icons.account_circle,3,"Account"),
],
),
);
}
ListView getCustomerListView() {
TextStyle titleStyle = Theme.of(context).textTheme.subhead;
return ListView.builder(
itemCount: count,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.white,
elevation: 2.0,
child: ListTile(
/*trailing: CircleAvatar(
backgroundColor: getPriorityColor(this.noteList[position].priority),
child: getPriorityIcon(this.noteList[position].priority),
),*/
title: Text(this.customerList[position].custName, style: titleStyle,),
//subtitle: Text(this.customerList[position].date),
onTap: () {
},
),
);
},
);
}
void navigateToCustomer(AddCustomer customer, String title) async {
bool result = await Navigator.push(context, MaterialPageRoute(builder: (context) {
return AddPerson(customer, title);
}));
if (result == true) {
updateListView();
}
}
void updateListView() {
final Future<Database> dbFuture = DB.init();
dbFuture.then((database) {
Future<List<AddCustomer>> customerListFuture = dbService.getCustomerList();
customerListFuture.then((customerList) {
setState(() {
this.customerList = customerList;
this.count = customerList.length;
});
});
});
}
Widget buildNavBarItem(IconData icon,int index,title) {
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
return GestureDetector(
onTap: () {
setState(() {
_isSelectedItemIndex=index;
if(_isSelectedItemIndex==3) {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => User_Settings()));
}
});
},
child: Container(
height: height * 0.09,
width: width / 4,
decoration: index==_isSelectedItemIndex?BoxDecoration(
border: Border(
bottom: BorderSide(width: 4,color: Theme.of(context).primaryColor),
),
gradient: LinearGradient(colors: [
Theme.of(context).primaryColor.withOpacity(0.3),
Theme.of(context).primaryColor.withOpacity(0.015),
],begin: Alignment.bottomCenter,end: Alignment.topCenter)
):BoxDecoration(),
child: Column(
children: <Widget>[
InkWell(
child: Icon(icon,
color: index==_isSelectedItemIndex? Theme.of(context).primaryColor:Colors.black54,
),
),
Text(title)
],)),
);
}
}
import'包装:客户/型号/备注.dart';
导入“包:customer/models/addCustomer.dart”;
导入“包装:客户/屏幕/新注释.省道”;
导入“包装:客户/屏幕/备注信息.省道”;
导入“package:customer/screens/User_Settings.dart”;
导入“包:客户/屏幕/添加_person.dart”;
导入“包:customer/services/db_service.dart”;
导入“package:customer/utils/database_helper.dart”;
进口“包装:颤振/材料.省道”;
导入“dart:io”;
导入“包:sqflite/sqflite.dart”;
类人员列表扩展StatefulWidget{
@凌驾
状态createState(){
返回人员列表状态();
}
}
类People_ListState使用SingleTickerProviderStateMixin扩展状态{
DBService DBService=DBService();
列出客户名单;
整数计数=0;
静态最终GlobalKey脚手架键=新的GlobalKey();
var_是选择性指数;
TextEditingController\u searchQuery;
bool\u isSearching=false;
String searchQuery=“搜索查询”;
@凌驾
void initState(){
super.initState();
_searchQuery=新文本编辑控制器();
}
void_startSearch(){
打印(“打开搜索框”);
ModalRoute
.of(上下文)
.addLocalHistoryEntry(新的LocalHistoryEntry(onRemove:_stopSearching));
设置状态(){
_isSearching=true;
});
}
void _stopSearching(){
_clearSearchQuery();
设置状态(){
_isSearching=假;
});
}
void _clearSearchQuery(){
打印(“关闭搜索框”);
设置状态(){
_searchQuery.clear();
updateSearchQuery(“搜索查询”);
});
}
小部件_buildTitle(BuildContext上下文){
var水平滴定校准=
Platform.isIOS?CrossAxisAlignment.center:CrossAxisAlignment.start;
返回新墨水池(
onTap:()=>scaffoldKey.currentState.openDrawer(),
孩子:新的填充物(
填充:常量边集。对称(水平:12.0),
子:新列(
mainAxisAlignment:mainAxisAlignment.center,
横轴对齐:水平方向对齐,
儿童:[
常量文本(“”),
],
),
),
);
}
小部件_buildSearchField(){
返回新的文本字段(
控制器:\u searchQuery,
自动对焦:对,
装饰:常量输入装饰(
hintText:“搜索…”,
边框:InputBorder.none,
hintStyle:const TextStyle(颜色:Colors.white30),
),
样式:const TextStyle(颜色:Colors.white,fontSize:16.0),
onChanged:updateSearchQuery,
);
}
void updateSearchQuery(字符串newQuery){
设置状态(){
searchQuery=newQuery;
});
打印(“搜索查询”+新建查询);
}
列表_buildActions(){
如果(正在搜索){
返回[
新图标按钮(
图标:常量图标(图标清除),
已按下:(){
if(_searchQuery==null | |(u searchQuery.text.isEmpty){
Navigator.pop(上下文);
返回;
}
_clearSearchQuery();
},
),
];
}
返回[
新图标按钮(
图标:常量图标(Icons.search),
按下按钮:_startSearch,
),
];
}
@凌驾
小部件构建(构建上下文){
var height=MediaQuery.of(context).size.height;
var width=MediaQuery.of(context).size.width;
如果(customerList==null){
customerList=List();
updateListView();
}
返回脚手架(
appBar:新的appBar(
前导:_isSearching?const BackButton():null,
标题:_isSearching?_buildSearchField():_buildTitle(上下文),
操作:_buildActions(),
),
正文:getCustomerListView(),
浮动操作按钮:浮动操作按钮(
已按下:(){
navigateToCustomer(AddCustomer(),“addPerson”);
},
子:常量图标(Icons.add),
),
底部导航栏:行(
儿童:[
buildNavBarItem(Icons.home,0,“home”),
buildNavBarItem(图标。新的圆形,1,“即将推出”),
buildNavBarItem(图标.历史,2,“历史”),
buildNavBarItem(图标。帐户\圆圈,3,“帐户”),
],
),
);
}
ListView getCustomerListView(){
TextStyle titleStyle=Theme.of(context).textTheme.subhead;
返回ListView.builder(
itemCount:count,
itemBuilder:(构建上下文,int位置){
回程卡(
颜色:颜色,白色,
标高:2.0,
孩子:ListTile(
/*尾随:圆形(
backgroundColor:getPriorityColor(this.noteList[position].priority),
子项:getPriorityIcon(此.noteList[position].priority),
),*/
标题:文本(this.customerList[position].custName,样式:titleStyle,),
//字幕:文本(this.customerList[position].date),
onTap:(){
},
),
class Note_Info extends StatefulWidget{
final String appBarTitle;
final AddCustomer customer;
Note_Info(this. customer, this.appBarTitle);
@override
State<StatefulWidget> createState() {
return Note_InfoState(this.customer,this.appBarTitle);
}
}
class Note_InfoState extends State<Note_Info> {
CustomerNote note=CustomerNote();
DBService dbService = DBService();
List<CustomerNote> cnoteList;
int count = 0;
String appBarTitle;
AddCustomer customer;
Note_InfoState(this.customer, this.appBarTitle);
PickedFile _imageFile;
final ImagePicker _picker = ImagePicker();
bool rememberMe = false;
DateTime _date = DateTime.now();
TextEditingController custNameController = TextEditingController();
void getImage(ImageSource source) async {
final pickedFile = await _picker.getImage(
source: source);
setState(() {
_imageFile = pickedFile;
});
}
@override
Widget build(BuildContext context) {
if (noteList == null) {
noteList = List<CustomerNote>();
updateCustomerNotes();
}
var height = MediaQuery.of(context).size.height;
custNameController.text = customer.custName;
return DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(
Icons.add,
),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => NewNote()));
},
)
],
),
body: Container(
child: Column(
children: <Widget>[
TextField(controller: custNameController,
style: TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold),
textAlign: TextAlign.center),
Padding(
padding: const EdgeInsets.all(15.0),
child: Row(children: [
ImageProfile(),
Padding(
padding: const EdgeInsets.only(left: 30.0),
child: IconButton(
icon: Icon(
Icons.call,
color: Colors.green,
size: 45,
),
onPressed: () {
},
),
),
],),
),
SizedBox(
height: 50,
child: AppBar(
bottom: TabBar(
tabs: [
Tab(
text: "All",
),
Tab(
text: "Pending",
),
Tab(
text: "Cancelled",
),
Tab(
text: "Completed",
),
],
),
),
),
// create widgets for each tab bar here
Expanded(
child: TabBarView(
children: [
// first tab bar view widget
Container(
child: getNoteListView(),
),
// second tab bar view widget
Container(
child: Center(
child: Text(
'Pending Items',
),
),
),
Container(
child: Center(
child: Text(
'Cancelled',
),
),
),
Container(
child: Center(
child: Text(
'Completed',
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 55.0,
width: 200,
child: RaisedButton(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
color: Theme
.of(context)
.primaryColorDark,
textColor: Colors.white,
child: Text('Save', textScaleFactor: 1.5,),
onPressed: () {
setState(() {
//_reset();
});
},
),
),
),
]
),
)
));
}
void updateCustomerNotes() {
final Future<Database> dbFuture = DB.init();
dbFuture.then((database) {
int customerId=customer.custId;
Future<List<CustomerNote>> noteListFuture = dbService.getCustomerNotes(customerId);
noteListFuture.then((cnoteList) {
setState(() {
this.cnoteList = cnoteList;
//this.count = cnoteList.length;
});
});
});
}
ListView getNoteListView() {
TextStyle titleStyle = Theme.of(context).textTheme.subhead;
return ListView.builder(
itemCount: count,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.white,
elevation: 2.0,
child: ListTile(
title: Text(this.cnoteList[position].note, style: titleStyle,),
subtitle: Text(this.cnoteList[position].date),
trailing: GestureDetector(
child: Icon(Icons.delete, color: Colors.grey,),
onTap: () {
},
),
onTap: () {
},
),
);
},
);
}
void _showSnackBar(BuildContext context, String message) {
final snackBar = SnackBar(content: Text(message));
Scaffold.of(context).showSnackBar(snackBar);
}
Widget ImageProfile() {
return Center(
child: CircleAvatar(
radius: 80.0,
backgroundImage: AssetImage('images/person_icon.jpg')
),
);
}
}
List<Map> result = await db.query(
DatabaseHelper.table,
columns: columnsToSelect,
where: whereString,
whereArgs: whereArguments);
// print the results
result.forEach((row) => print(row));
// {_id: 1, name: Bob, age: 23}
}
final outData = responseData.where((i) => i.userId == customerId).toList();