flutter - 有没有办法在列表视图构建器中为每个容器添加不同的图像?
解决方案
import 'dart:io';
import 'package:image_picker/image_picker.dart';
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: HomeScreen());
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
List<Model> list = [];
@override
void initState() {
super.initState();
list.add(Model([null,null,null]));
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Track Assets'),
),
//use ListView.builder for dynamic content
body: ListView.builder(
padding: EdgeInsets.all(15),
itemCount: list.length,
shrinkWrap: true,
itemBuilder: (con, ind) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.grey.withAlpha(70),
),
padding: EdgeInsets.all(10),
child: Column(
children: <Widget>[
Row(children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Freezer',
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.bold)),
Text('Lorem Ipsum',
style: TextStyle(
color: Colors.black, fontSize: 15))
]),
SizedBox(width: 25),
Icon(Icons.check_circle, color: Colors.black),
Expanded(child: SizedBox()),
Column(children: [
SizedBox(
width: 150,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7),
),
color: Colors.grey,
onPressed: () {
//todo
},
child: Text('Track',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.bold))),
),
SizedBox(
width: 150,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7),
),
color: Colors.grey,
onPressed: () {
//todo
},
child: Text('Relocate',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.bold))),
),
SizedBox(
width: 150,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7),
),
color: Colors.grey,
onPressed: () {
//todo
},
child: Text('Add Images',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.bold))),
),
]),
]),
Divider(
color: Colors.grey,
thickness: 0.7,
indent: 25,
endIndent: 25,
),
ListView.builder(
shrinkWrap:true,
scrollDirection:Axis.horizontal,
itemCount:list[ind].images==null?1: list[ind].images.length+1,
itemBuilder:(con2,ind2){
return list[ind].images.length==ind ?
FlatButton(
child: Icon(Icons.add_circle, color: Colors.grey),
onPressed:(){
getImage(ind);
}): Image.file(list[ind].images[ind2]);
}
),
],
));
}),
);
}
Future getImage(int listIndex) async {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
if(image!=null)
setState(() {
if(list[listIndex].images == null)
list[listIndex].images = [];
list[listIndex].images.add(image);
});
}
}
class Model {
List<File> images;
//or use network path
//String path;
//other properties
Model(this.images);
}
推荐阅读
- javascript - 无法从出现 cors 错误的 ajax api 调用中获得响应,在邮递员中工作正常。我在请求中添加了 cors 标头
- google-cloud-platform - 云 DNS 域配置
- javascript - Django/plotly.js:将 Pandas Dataframe 列传递给模板图表
- racket - 在 DrRacket 中将字符串列表转换为字符串
- c++ - 最大回文积(Project Euler)——C++
- c# - 如何在运行时将字符串作为代码插入?
- python - 将列表更改为具有随机值的字典
- c - 为什么这个cpu运行得更快?
- rcpp - 如何在 Rcpp 中创建它
- c - 为什么并行合并排序使用单线程运行得更快?