列表视图构建器中是否有任何方法可以为每个容器添加不同的图像?

问题描述 投票:0回答:1

Image of listview builderstrong text

当按下“添加图像”按钮到相应的容器时,我不会通过打开相机来添加图像。如何实现?

flutter flutter-layout
1个回答
0
投票
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,
                    ),
                    Row(children: [
                      Expanded(
                        //Use place holder/other image if image not selected
                        //I used dartpad to create this, so I used icons
                        child: list[ind].images[0] == null
                            ? FlatButton(
                              child: Icon(Icons.add_circle, color: Colors.grey),
                            onPressed:(){
                              getImage(ind,0);
                            })
                            : Image.file(list[ind].images[0]),
                      ),
                      Expanded(
                        //Use place holder/other image if image not selected
                        //I used dartpad to create this, so I used icons
                        child: list[ind].images[1] == null
                            ? FlatButton(
                              child: Icon(Icons.add_circle, color: Colors.grey),
                            onPressed:(){
                              getImage(ind,1);
                            })
                            : Image.file(list[ind].images[1]),
                      ),
                      Expanded(
                        //Use place holder/other image if image not selected
                        //I used dartpad to create this, so I used icons
                        child: list[ind].images[2] == null
                            ? FlatButton(
                              child: Icon(Icons.add_circle, color: Colors.grey),
                            onPressed:(){
                              getImage(ind,2);
                            })
                            : Image.file(list[ind].images[2]),
                      )
                    ])
                  ],
                ));
          }),
    );
  }
  Future getImage(int listIndex,int imageIndex) async {
    var image = await ImagePicker.pickImage(source: ImageSource.camera);

    if(image!=null)
    setState(() {
      list[listIndex].images[imageIndex]=image;
    });
  }
}

class Model {
  List<File> images;
  //or use network path
  //String path;
  //other properties

  Model(this.images);
}

Screenshot

© www.soinside.com 2019 - 2024. All rights reserved.