Я пытаюсь загрузить изображения по переменным, чтобы я мог иметь несколько разных изображений без использования строки кода для каждого изображения.
Часть кода
class _MyHomePageState extends State<MyHomePage> {
getItem(text, check, big, img) {
var con0 = big ? 150.0 : 100.0;
return Container(
width: con0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
InkWell(
child: GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute<Null>(
builder: (BuildContext context) {
return new AlbumPage();
}));
},
child: Container(
margin: EdgeInsets.only(right: 8.0),
width: con0,
height: con0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('$img.jfif'),
fit: BoxFit.cover
)),
))),
Полный код
import 'package:flutter/material.dart';
import 'package:spotify/Albumpage.dart';
import 'package:spotify/Search.dart';
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
getItem(text, check, big, img) {
var con0 = big ? 150.0 : 100.0;
return Container(
width: con0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
InkWell(
child: GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute<Null>(
builder: (BuildContext context) {
return new AlbumPage();
}));
},
child: Container(
margin: EdgeInsets.only(right: 8.0),
width: con0,
height: con0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('$img.jfif'),
fit: BoxFit.cover
)),
))),
SizedBox(
height: 5.0,
),
Text(
"$text",
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
)
],
),
);
}
getList(title, big) {
return Container(
margin: EdgeInsets.only(bottom: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"$title",
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
SizedBox(height: 20.0),
Container(
height: big ? 180.0 : 150.0,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
getItem("Daily Mix 1", true, big, 6),
getItem("Daily Mix 2", true, big, 6),
getItem("Daily Mix 3", true, big, 6),
getItem("Daily Mix 4", true, big, 6),
getItem("Daily Mix 5", true, big, 6),
],
),
)
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.center,
colors: <Color>[Colors.lightBlueAccent, Colors.black],
),
),
child: Center(
child: Container(
margin: EdgeInsets.only(top: 36.0),
padding: EdgeInsets.symmetric(horizontal: 16.0),
child: ListView(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Icon(
Icons.settings,
color: Colors.white,
)
],
),
SizedBox(height: 10.0),
getList("Onlangs afgespeeld", false),
getList("Speciaal voor David", true),
getList("Populaire playlist", true),
getList("Podscasts om te proberen", true),
getList("Gebaseerd op je recent luister...", true),
getList("Blijf op de hoogte!", true),
getList("Nummers die je vaak luistert", true),
getList("Fijne muziek voor deze woensda...", true),
getList("Terugblik", true),
getList("Je 10 topartiesten van de afgelo...", true),
getList("Hiphop", true),
getList("Chill", true),
getList("Vrolijk", true),
getList("Workout", true),
getList("Hip-Hop: All Four Coasts", true),
getList("Dance Legends", true),
getList("Shisha Sounds", true),
getList("Instrumentaal", true),
getList("North African essentials", true),
],
),
),
),
)),
);
}
}