1

I've been trying to make a content group (image + text) Dismissible as a single unit (swipe-to-delete/remove), but seems as though I can't assign a Widget list as the child parameter to a Dismissible() object. Looking for possible work-arounds or a solution to the problem.

CODE:

class _PhotosListState extends State<PhotosList> {
  @override
  Widget build(BuildContext context) {
    return _buildBody();
  }
  Widget _buildBody() {
    return SizedBox(
        height: 485,
        child: ListView.builder(
          //scrollDirection: Axis.vertical,
          //shrinkWrap: true,
            itemCount: Photos.length,
            itemBuilder: (context,i){
              return Dismissible(
                  background: Container(
                    color: Colors.green,
                  ),
                  key: ValueKey<Object>(Photos.items[i]),
                  onDismissed: (DismissDirection direction) {
                    setState(() {
                      Photos.items.removeAt(i);
                    });
                  },
                  child: SizedBox(
                    child: <Widget> [
                      Image.asset(Photos.items[i].image),
                      Text(Photos.items[i].task,
                          textAlign: TextAlign.center,
                          style: const TextStyle(color: Colors.grey, fontWeight: FontWeight.bold, fontSize: 19.5)
                      ),
                    ]
                  )
              );
            }
        )
    );
  }
}

RESOURCES:

MendelG
  • 14,885
  • 4
  • 25
  • 52

1 Answers1

0

In your code of SizedBox:

SizedBox(
                    child: <Widget> [
                      Image.asset(Photos.items[i].image),
                      Text(Photos.items[i].task,
                          textAlign: TextAlign.center,
                          style: const TextStyle(color: Colors.grey, fontWeight: FontWeight.bold, fontSize: 19.5)
                      ),

you are passing multiple widgets as a child, but child only takes one argument (i.e, one widget).

So, if you want multiple widgets, you should use a Column widget instead - which can accept a List:

SizedBox(
                      child: Column(children: <Widget>[
                    Image.asset(Photos.items[i].image),
                    Text(Photos.items[i].task,
                        textAlign: TextAlign.center,
                        style: const TextStyle(
                            color: Colors.grey,
                            fontWeight: FontWeight.bold,
                            fontSize: 19.5)),
                  ]))

Hence the names - child vs children - it's good to keep this in mind as there can be widgets the accept multiple widgets, i.e. children or a widget that can only accept one child.

MendelG
  • 14,885
  • 4
  • 25
  • 52
  • Thanks for the explanation! The solution almost worked a charm, except when I try to delete more than one `Column()` object it throws a `external T operator [](int index)` error in `growable_array.dart`. Trying to figure it out :| – Harshal Janjani Jan 02 '23 at 06:13
  • Nevermind, I had a `length` attribute in the class `Photos` which had to be decremented by 1 every time a `Column()` object was removed. Thanks for the help! – Harshal Janjani Jan 02 '23 at 06:25