我正在开发一个在线项目,我想将列表的元素“品牌”调用到AppBar,但是我做不到,它运行为空。我将ListView.builder用作列表,当我单击列表元素时,它将打开新页面,我想将我从另一个类中单击的“品牌”元素称为AppBar。下面的代码,请帮助我:
final HAVUZ_DATA = [
HavuzData(
id: 'User1',
imageUrl: 'assets/images/arac1.jpeg',
brand: 'Volkswagen',
model: 'MULTIVAN 2.5 TDI COMFORTLINE',
licensePlate: '34AAA3434',
year: '2011',
city: 'İstanbul',
district: 'Bahçelievler',
authorities: '20040601419',
status: 'Ekspertiz yapıldı, Dosya sorumlusuna gönderildi.',
),
HavuzData(
id: 'User2',
imageUrl: 'assets/images/arac2.jpg',
brand: 'BMW',
model: '420',
licensePlate: '34ABC3434',
year: '2018',
city: 'İstanbul',
district: 'Bakırköy',
authorities: '20040601419',
status: 'Ekspertiz yapıldı, Dosya sorumlusuna gönderildi.',
)
];
class HavuzData {
final String id;
final String imageUrl;
final String brand;
final String model;
final String licensePlate;
final String year;
final String city;
final String district;
final String authorities;
final String status;
const HavuzData({
this.id,
this.imageUrl,
this.brand,
this.model,
this.licensePlate,
this.year,
this.city,
this.district,
this.authorities,
this.status,
});
}
我想从这个班级打电话:
class HavuzDetay extends StatefulWidget {
@override
_HavuzDetayState createState() => _HavuzDetayState();
}
class _HavuzDetayState extends State<HavuzDetay> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Detay ' + HavuzData().brand,
),
),
body: Container(
child: Center(
child: Text('Hello Detay'),
),
),
);
}
}
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
您可以使用参数HAVUZ_DATA[index]
进行推送,然后处理HavuzData
代码段
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HavuzDetay(HAVUZ_DATA[index])),
);
...
class HavuzDetay extends StatefulWidget {
final HavuzData havuzData;
HavuzDetay(this.havuzData);
...
appBar: AppBar(
title: Text(
'Detay ' + widget.havuzData.brand,
工作演示
完整代码
import 'package:flutter/material.dart';
final HAVUZ_DATA = [
HavuzData(
id: 'User1',
imageUrl: 'assets/images/arac1.jpeg',
brand: 'Volkswagen',
model: 'MULTIVAN 2.5 TDI COMFORTLINE',
licensePlate: '34AAA3434',
year: '2011',
city: 'İstanbul',
district: 'Bahçelievler',
authorities: '20040601419',
status: 'Ekspertiz yapıldı, Dosya sorumlusuna gönderildi.',
),
HavuzData(
id: 'User2',
imageUrl: 'assets/images/arac2.jpg',
brand: 'BMW',
model: '420',
licensePlate: '34ABC3434',
year: '2018',
city: 'İstanbul',
district: 'Bakırköy',
authorities: '20040601419',
status: 'Ekspertiz yapıldı, Dosya sorumlusuna gönderildi.',
)
];
class HavuzData {
final String id;
final String imageUrl;
final String brand;
final String model;
final String licensePlate;
final String year;
final String city;
final String district;
final String authorities;
final String status;
const HavuzData({
this.id,
this.imageUrl,
this.brand,
this.model,
this.licensePlate,
this.year,
this.city,
this.district,
this.authorities,
this.status,
});
}
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: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.builder(
padding: EdgeInsets.only(left: 20, top: 10, right: 20, bottom: 10),
itemCount: HAVUZ_DATA.length,
itemBuilder: (BuildContext ctxt, int index) {
return InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HavuzDetay(HAVUZ_DATA[index])),
);
},
child: Card(
child: ListTile(
title: Text(HAVUZ_DATA[index].brand),
subtitle: Text(
HAVUZ_DATA[index].city,
),
),
));
}));
}
}
class HavuzDetay extends StatefulWidget {
final HavuzData havuzData;
HavuzDetay(this.havuzData);
@override
_HavuzDetayState createState() => _HavuzDetayState();
}
class _HavuzDetayState extends State<HavuzDetay> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Detay ' + widget.havuzData.brand,
),
),
body: Container(
child: Center(
child: Text('Hello Detay'),
),
),
);
}
}