首页 > 解决方案 > 有没有办法在列表视图构建器中为每个容器添加不同的图像?

问题描述

列表视图生成器的图像强文本

当按下添加图像按钮到相应的容器时,我不想通过打开相机来添加图像。如何做到这一点?

标签: flutterflutter-layout

解决方案


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);
}

截屏


推荐阅读