颤振中ListView.builder中的onTap()
早上好, 我编写了一个小部件,借助它我可以从api下载数据,列出它们,并通过适当的单词动态搜索。现在我想应用这个函数,在单击列表中的对象后,转到单个对象的详细信息。我试图使用onTap()函数,但没有成功 下面粘贴代码:颤振中ListView.builder中的onTap(),listview,flutter,widget,Listview,Flutter,Widget,早上好, 我编写了一个小部件,借助它我可以从api下载数据,列出它们,并通过适当的单词动态搜索。现在我想应用这个函数,在单击列表中的对象后,转到单个对象的详细信息。我试图使用onTap()函数,但没有成功 下面粘贴代码: class _HomePageState extends State<HomePage> { List<Order> _notes = List<Order>(); List<Order> _notesForDispla
class _HomePageState extends State<HomePage> {
List<Order> _notes = List<Order>();
List<Order> _notesForDisplay = List<Order>();
Future<List<Order>> fetchNotes() async {
var url = 'http://10.0.2.2:80/order';
var response = await http.get(url);
var notes = List<Order>();
if (response.statusCode == 200) {
var notesJson = json.decode(response.body);
for (var noteJson in notesJson) {
notes.add(Order.fromJson(noteJson));
}
}
return notes;
}
@override
void initState() {
fetchNotes().then((value) {
setState(() {
_notes.addAll(value);
_notesForDisplay = _notes;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home:Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(100.0),
child: AppBar(
iconTheme: IconThemeData(color: Color.fromRGBO(9, 133, 46, 100)),
backgroundColor: Colors.white,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.shopping_cart,
color: Color.fromRGBO(9, 133, 46, 100),
),
onPressed: (){
print('klikniete');
},
),
],
),
),
body: Container(
child: FutureBuilder(
future: fetchNotes(),
builder: (BuildContext context, AsyncSnapshot snapshot){
if(_notesForDisplay.length == null){
return Container(
child: Center(
child:Text("Ładowanie...")
),
);
}else{
return ListView.builder(
itemCount: _notesForDisplay.length+1,
itemBuilder: (BuildContext context, int index) {
return index == 0 ? _searchBar() : _listItem(index-1);
},
);
}
},
),
),
);
}
_searchBar() {
return Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(
hintText: 'Wyszukaj po mieście...'
),
onChanged: (text) {
text = text.toLowerCase();
setState(() {
_notesForDisplay = _notes.where((note) {
var noteTitle = note.firstName.toLowerCase();
return noteTitle.contains(text);
}).toList();
});
},
),
);
}
_listItem(index) {
return Card(
child: Padding(
padding: const EdgeInsets.only(top: 32.0, bottom: 32.0, left: 16.0, right: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_notesForDisplay[index].firstName,
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold
),
),
// Text(
// _notesForDisplay[index].lastName,
// style: TextStyle(
// color: Colors.grey.shade600
// ),
// ),
],
),
),
);
}
}
class\u HomePageState扩展状态{
列表_notes=List();
List _notesForDisplay=List();
Future fetchNotes()异步{
var url='1〕http://10.0.2.2:80/order';
var response=wait http.get(url);
var notes=List();
如果(response.statusCode==200){
var notesJson=json.decode(response.body);
for(notesJson中的var noteJson){
添加(Order.fromJson(noteJson));
}
}
回条;
}
@凌驾
void initState(){
fetchNotes()。然后((值){
设置状态(){
_注:addAll(价值);
_notesForDisplay=\u notes;
});
});
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:首选大小(
首选尺寸:尺寸。从高度(100.0),
孩子:AppBar(
iconTheme:IconThemeData(颜色:color.fromRGBO(913346100)),
背景颜色:Colors.white,
行动:[
图标按钮(
图标:图标(
图标。购物车,
颜色:颜色。来自RGBO(913346100),
),
已按下:(){
印刷品(“klikniete”);
},
),
],
),
),
主体:容器(
孩子:未来建设者(
future:fetchNotes(),
生成器:(BuildContext上下文,异步快照){
if(_notesForDisplay.length==null){
返回容器(
儿童:中心(
儿童:文本(“Ładowanie…”)
),
);
}否则{
返回ListView.builder(
itemCount:_notesForDisplay.length+1,
itemBuilder:(构建上下文,int索引){
返回索引==0?\u搜索栏():\u列表项(索引-1);
},
);
}
},
),
),
);
}
_搜索栏(){
返回填充(
填充:常数边集全部(8.0),
孩子:TextField(
装饰:输入装饰(
hintText:“Wyszukaj po mieście…”
),
一旦更改:(文本){
text=text.toLowerCase();
设置状态(){
_notesForDisplay=\u notes.where((注释){
var notetTitle=note.firstName.toLowerCase();
返回noteTitle.contains(文本);
}).toList();
});
},
),
);
}
_列表项(索引){
回程卡(
孩子:填充(
填充:仅限常量边集(顶部:32.0,底部:32.0,左侧:16.0,右侧:16.0),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
正文(
_notesForDisplay[index].firstName,
样式:TextStyle(
尺寸:22,
fontWeight:fontWeight.bold
),
),
//正文(
//\u notesForDisplay[索引]。姓氏,
//样式:TextStyle(
//颜色:Colors.grey.shade600
// ),
// ),
],
),
),
);
}
}
有人知道我如何在单击后移动到对象的详细信息吗
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeBody(),
);
}
}
类HomeBody扩展StatefulWidget{
@凌驾
_HomeBodyState createState()=>\u HomeBodyState();
}
类_HomeBodyState扩展状态{
列表_notes=List();
List _notesForDisplay=List();
Future fetchNotes()异步{
var url='1〕http://10.0.2.2:80/order';
var response=wait http.get(url);
var notes=List();
如果(response.statusCode==200){
var notesJson=json.decode(response.body);
for(notesJson中的var noteJson){
添加(Order.fromJson(noteJson));
}
}
回条;
}
@凌驾
void initState(){
super.initState();
fetchNotes()。然后((值){
设置状态(){
_注:addAll(价值);
_notesForDisplay=\u notes;
});
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:首选大小(
首选尺寸:尺寸。从高度(100.0),
孩子:AppBar(
iconTheme:IconThemeData(颜色:color.fromRGBO(913346100)),
背景颜色:Colors.white,
行动:[
图标按钮(
图标:图标(
图标。购物车,
颜色:颜色。来自RGBO(913346100),
),
已按下:(){
印刷品(“klikniete”);
},
),
],
),
),
主体:容器(
孩子:未来建设者(
future:fetchNotes(),
生成器:(BuildContext上下文,异步快照){
if(_notesForDisplay.length==null){
返回容器(
子对象:中心(子对象:文本(“Ładowanie…”)),
);
}否则{
返回ListView.builder(
itemCount:_notesForDisplay.length+1,
itemBuilder:(构建上下文,int索引){
返回索引==0?\u搜索栏():\u列表项(索引-1);
},
);
}
},
),
),
);
}
_列表项(索引){
//为卡添加手势检测器
//将_notesForDisplay[索引]传递到DetailPage
返回手势检测器(
onTap:()=>Navigator.of(context.push)(
材料路线(
生成器:(上下文)=>DetailPage(项目编号)
// Make the detail page
class DetailPage extends StatelessWidget {
final Order item;
const DetailPage({this.item});
@override
Widget build(BuildContext context) {
return Center(
child: Text('${item.firstName} - ${item.lastName}')
);
}
}
class HomeBody extends StatefulWidget {
@override
_HomeBodyState createState() => _HomeBodyState();
}
class _HomeBodyState extends State<HomeBody> {
List<Order> _notes = List<Order>();
List<Order> _notesForDisplay = List<Order>();
Future<List<Order>> fetchNotes() async {
var url = 'http://10.0.2.2:80/order';
var response = await http.get(url);
var notes = List<Order>();
if (response.statusCode == 200) {
var notesJson = json.decode(response.body);
for (var noteJson in notesJson) {
notes.add(Order.fromJson(noteJson));
}
}
return notes;
}
@override
void initState() {
super.initState();
fetchNotes().then((value) {
setState(() {
_notes.addAll(value);
_notesForDisplay = _notes;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(100.0),
child: AppBar(
iconTheme: IconThemeData(color: Color.fromRGBO(9, 133, 46, 100)),
backgroundColor: Colors.white,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.shopping_cart,
color: Color.fromRGBO(9, 133, 46, 100),
),
onPressed: () {
print('klikniete');
},
),
],
),
),
body: Container(
child: FutureBuilder(
future: fetchNotes(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (_notesForDisplay.length == null) {
return Container(
child: Center(child: Text("Ładowanie...")),
);
} else {
return ListView.builder(
itemCount: _notesForDisplay.length + 1,
itemBuilder: (BuildContext context, int index) {
return index == 0 ? _searchBar() : _listItem(index - 1);
},
);
}
},
),
),
);
}
_listItem(index) {
// Add gesture detector for the Card
// Pass the _notesForDisplay[index] to the DetailPage
return GestureDetector(
onTap: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DetailPage(item: _notesForDisplay[index])),
),
child: Card(
child: Padding(
padding: const EdgeInsets.only(
top: 32.0, bottom: 32.0, left: 16.0, right: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_notesForDisplay[index].firstName,
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
),
// Text(
// _notesForDisplay[index].lastName,
// style: TextStyle(
// color: Colors.grey.shade600
// ),
// ),
],
),
),
),
);
}
}