我尝试使用 info 类作为多个类的示例,但我的问题是当我运行应用程序时,我只能看到白色背景。抱歉,代码太长了,我在编程方面很新,并尝试尽快变得更好:)
class kaminfeuerinfo extends StatefulWidget {
@override
_kaminfeuerinfoState createState() => _kaminfeuerinfoState();
}
class _kaminfeuerinfoState extends State<kaminfeuerinfo> {
List <Info> inhalt = [
Info(name: 'hallo', beschreibung: '1', preis: '2', kilo: 'f', details: 'f,', artikelnummer: 'd',zutaten: 'f',
verkehrsbezeichnung: 'z', inverkehrbringer: 'h', kj: 'h', kcal: 'h',fett: 'h',
saeure: 'g', kohlenhydrate: 'h',zucker: 'k', eiweiss: 't', salz: 't')
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
children:
inhalt.map((inhalt) => info (inhalt: inhalt)).toList(),
),
),
);
}
}
class Info {
String name;
String beschreibung;
String preis;
String kilo;
String details;
String artikelnummer;
String zutaten;
String verkehrsbezeichnung;
String inverkehrbringer;
String kj;
String kcal;
String fett;
String saeure;
String kohlenhydrate;
String zucker;
String eiweiss;
String salz;
Info({this.name,this.beschreibung, this.preis, this.kilo, this.details, this.artikelnummer, this.zutaten, this.verkehrsbezeichnung,
this.inverkehrbringer, this.kj, this.kcal, this.fett, this.saeure, this.kohlenhydrate, this.zucker, this.eiweiss, this.salz,});
}
这不是全部代码,因为代码需要超过 500 行,但我希望这对你来说已经足够了,也能帮助我解决我的问题:)
class info extends StatelessWidget {
final Info inhalt;
info ({this.inhalt});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
onPressed: () {
showSearch(context: context, delegate: suchen());
},
icon: Icon(Icons.search))
],
title: Image.asset(
"assets/images/logo_465x320.png",
width: 150,
),
centerTitle: true,
backgroundColor: Colors.brown,
),
body: SingleChildScrollView(
child: Container(
alignment: Alignment.center,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(
height: 10.0,
),
Container(
alignment: Alignment.center,
child: Text(
inhalt.name,
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
SizedBox(
height: 7.0,
),
Container(
alignment: Alignment.center,
margin: EdgeInsets.only(left: 20, right: 20),
child: AutoSizeText(
inhalt.beschreibung,
textAlign: TextAlign.center,
maxLines: 15,
style: TextStyle(fontSize: 10),
),
width: 130,
),
SizedBox(
height: 7.0,
),
Container(
child: Carousel(
images: [
Image.asset(
"assets/images/kenia.jpg",
),
Image.asset(
"assets/images/tee2.jpg",
),
Image.asset(
"assets/images/tee.jpg",
),
],
autoplay: false,
dotBgColor: Colors.transparent,
indicatorBgPadding: 5.0,
dotPosition: DotPosition.bottomRight,
dotColor: Colors.white38,
dotIncreasedColor: Colors.white60,
dotIncreaseSize: 3,
dotSize: 3,
dotSpacing: 12,
),
height: 200.0,
),
SizedBox(
height: 10.0,
),
Container(
child: SizedBox(
width: 200.0,
height: 250.0,
child: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
automaticallyImplyLeading: false,
title: Column(
children: [
Container(
child: Text(
inhalt.preis,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black),
textAlign: TextAlign.center,
),
),
Container(
child: Text(
inhalt.kilo,
style: TextStyle(
fontSize: 10.0, color: Colors.black),
textAlign: TextAlign.center,
),
),
Container(
alignment: Alignment.bottomRight,
child: Text(
"inkl. 7% MwSt.",
style: TextStyle(
fontSize: 10.0, color: Colors.black),
textAlign: TextAlign.right,
),
)
],
),
centerTitle: true,
bottom: PreferredSize(
preferredSize: Size.fromHeight(50),
child: TabBar(
tabs: [
Tab(
text: "Details",
),
Tab(
text: "Informationen",
),
],
isScrollable: true,
unselectedLabelColor: Colors.black,
indicatorColor: Colors.black,
labelColor: Colors.black,
),
)),
body: TabBarView(
children: [
Container(
padding: EdgeInsets.all(20),
child: Text(
inhalt.details,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
),
),
),
],
),
),
)),
color: Colors.white,
),
],
),
),
),
);
}
}
答案 0 :(得分:0)
要在屏幕上以列表形式查看列表,您必须在 State 类中使用 ListView.builder:
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
children:
inhalt.map((inhalt) => info (inhalt: inhalt)).toList(),
),
),
);
}
}
这里有详细的文档,我建议您熟悉: https://api.flutter.dev/flutter/widgets/ListView-class.html