【发布时间】:2021-04-27 15:49:53
【问题描述】:
我希望在单击其中一个徽标时显示该品牌的型号,目前我只能显示[第一个品牌的型号]。2 因此,无论您单击哪个屏幕,都会出现。如何更改代码以显示特定品牌型号?
我只发布了关键代码,这里我导航到页面,模型页面”(但我认为它不相关,因为我猜测问题在于正文的显示(第二个代码)
import 'package:flutter/material.dart';
import 'package:flutter_meineapp/Indexes/Marken.dart';
import 'package:flutter_meineapp/Screen/Home/BodyHomescreen.dart';
import 'package:flutter_meineapp/Screen/Kontakt/Kontakt.dart';
import 'package:flutter_meineapp/Screen/Modelle/Modelle.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int _selectedIndex = 0;
List<Widget> _widgetOptions = <Widget> [
Container(
color: Color(0xffDDBEA9),
child: GridView.builder(
itemCount: modelle.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemBuilder: (context,index) =>
BodyHomescreen(
modell: modelle[index], press: () => Navigator.push(
context, MaterialPageRoute(
builder: (context) => Modelle( modell: modelle[index],)
,)
),
),
),
),
Text("Text2"),
Kontakt()
];
这是模型页面,我猜我需要根据索引更改body,但经过研究我找不到解决方案,有什么建议吗?
class Modelle extends StatelessWidget {
final Modell modell;
const Modelle({Key key, this.modell}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: [
Container(
width: 50,
child: Image.asset(modell.image),
),
],
title: Text(modell.title),
centerTitle: true,
backgroundColor: Color(0xffCB997E),
),
body: ListView.builder(
itemCount: ModelleAlfaRomeo.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(ModelleAlfaRomeo[index].title),
);
},
),
);
}
Class ,,Modell 的代码
class Modell {
final String image, title;
final int id;
Modell({
this.image,
this.title,
this.id,
});
}
List<Modell> modelle = [
Modell(
id: 1,
title: "Alfa Romeo",
image: "Assets/Images/AlfaRomeo.png"
),
Modell(
id: 2,
title: "Audi",
image: "Assets/Images/Audi.png"
),
Modell(
id: 3,
title: "BMW",
image: "Assets/Images/BMW.png"
),
Modell(
id: 4,
title: "Chevrolet",
image: "Assets/Images/Chevrolet.png"
),
Modell(
id: 5,
title: "Citroen",
image: "Assets/Images/Citroen.png"
),
Modell(
id: 6,
title: "Dacia",
image: "Assets/Images/Dacia.png"
),
Modell(
id: 7,
title: "Fiat",
image: "Assets/Images/Fiat.png"
),
Modell(
id: 8,
title: "Ford",
image: "Assets/Images/Ford.png"
),
Modell(
id: 9,
title: "Honda",
image: "Assets/Images/Honda.png"
),
Modell(
id: 10,
title: "Hyundai",
image: "Assets/Images/Hyundai.png"
),
Modell(
id: 11,
title: "Jaguar",
image: "Assets/Images/Jaguar.png"
),
Modell(
id: 12,
title: "Jeep",
image: "Assets/Images/Jeep.png"
),
Modell(
id: 13,
title: "KIA",
image: "Assets/Images/KIA.png"
),
Modell(
id: 14,
title: "LandRover",
image: "Assets/Images/LandRover.png"
),
Modell(
id: 15,
title: "Lexus",
image: "Assets/Images/Lexus.png"
),
Modell(
id: 16,
title: "Mazda",
image: "Assets/Images/Mazda.png"
),
Modell(
id: 17,
title: "Mercedes",
image: "Assets/Images/Mercedes.png"
),
Modell(
id: 18,
title: "Mini",
image: "Assets/Images/Mini.png"
),
Modell(
id: 19,
title: "Mitsubishi",
image: "Assets/Images/Mitsubishi.png"
),
Modell(
id: 20,
title: "Nissan",
image: "Assets/Images/Nissan.png"
),
Modell(
id: 21,
title: "Opel",
image: "Assets/Images/Opel.png"
),
Modell(
id: 22,
title: "Peugeot",
image: "Assets/Images/Peugeot.png"
),
Modell(
id: 23,
title: "Porsche",
image: "Assets/Images/Porsche.png"
),
Modell(
id: 24,
title: "Renault",
image: "Assets/Images/Renault.png"
),
Modell(
id: 25,
title: "Saab",
image: "Assets/Images/Saab.png"
),
Modell(
id: 26,
title: "Seat",
image: "Assets/Images/Seat.png"
),
Modell(
id: 27,
title: "Skoda",
image: "Assets/Images/Skoda.png"
),
Modell(
id: 28,
title: "Smart",
image: "Assets/Images/Smart.png"
),
Modell(
id: 29,
title: "Subaru",
image: "Assets/Images/Subaru.png"
),
Modell(
id: 30,
title: "Suzuki",
image: "Assets/Images/Suzuki.png"
),
Modell(
id: 31,
title: "Toyota",
image: "Assets/Images/Toyota.png"
),
Modell(
id: 32,
title: "Volvo",
image: "Assets/Images/Volvo.png"
),
Modell(
id: 33,
title: "VW",
image: "Assets/Images/VW.png"
),
];
ModelleAlfaRomeo 类的代码 -> 与描述汽车品牌模型的所有其他类的结构完全相同
class ModellAlfaRomeo {
final String title;
final int id;
ModellAlfaRomeo({
this.title,
this.id,
});
}
List<ModellAlfaRomeo> ModelleAlfaRomeo = [
ModellAlfaRomeo(
id: 1,
title: "147" ,
),
ModellAlfaRomeo(
id: 2,
title: "156 / Crosswagon Q4" ,
),
ModellAlfaRomeo(
id: 3,
title: "159" ,
),
ModellAlfaRomeo(
id: 4,
title: "166" ,
),
ModellAlfaRomeo(
id: 5,
title: "4c" ,
),
ModellAlfaRomeo(
id: 6,
title: "Brera" ,
),
ModellAlfaRomeo(
id: 7,
title: "GT" ,
),
ModellAlfaRomeo(
id: 8,
title: "Giulia (Typ 952)" ,
),
ModellAlfaRomeo(
id: 9,
title: "Giulietta (Typ 940)" ,
),
ModellAlfaRomeo(
id: 10,
title: "MiTo" ,
),
ModellAlfaRomeo(
id: 11,
title: "Spider" ,
),
ModellAlfaRomeo(
id: 12,
title: "Stelvio" ,
),
];
今天尝试将 ModelleAlfaRomeo、ModelleChevrolet 等所有类的代码嵌入到这个 ,,Modell" 类中,但还不知道该怎么做
【问题讨论】:
-
什么是模型?你在哪里定义的?
-
它是一个类,包含一个包含所有汽车品牌的所有型号的列表(带有 ID、照片和名称)。我现在正在尝试获取此列表中每个品牌的所有型号详细信息,现在所有汽车品牌型号都在单独的类中,例如包含 ModelleAlfaRomeo 类所有阿尔法罗密欧汽车型号的标题列表。你能听懂我的解释吗?
标签: flutter dart flutter-layout