dropdown_button2 icon indicating copy to clipboard operation
dropdown_button2 copied to clipboard

Widget doesn't update when items change after valueListenable

Open refi64 opened this issue 5 months ago • 0 comments

Sample reproducer (in a new flutter create project, with dropdown_button2: 3.0.0-beta.22):

import 'package:flutter/material.dart';
import 'package:dropdown_button2/dropdown_button2.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var selected = ValueNotifier<String?>(null);
  final allThings = ['abc', 'def'];
  List<String>? things;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          spacing: 32,
          children: <Widget>[
            DropdownButton2(
              isExpanded: true,
              valueListenable: selected,
              items: things != null
                  ? [
                      for (var thing in things!)
                        DropdownItem(value: thing, child: Text(thing)),
                    ]
                  : null,
            ),
            for (var thing in allThings)
              FilledButton(
                child: Text("Set valueListenable to '$thing'"),
                onPressed: () async {
                  selected.value = thing;
                },
              ),
            FilledButton(
              child: Text('Set items to $allThings'),
              onPressed: () {
                setState(() {
                  things = allThings;
                });
              },
            ),
            FilledButton(
              child: const Text('Clear items'),
              onPressed: () async {
                setState(() {
                  things = null;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

https://github.com/user-attachments/assets/b96f953c-0a23-42e5-9a26-71d34ae9efa2

After I set the selected and then later set the items, the box is still empty until the listenable is updated again.

refi64 avatar Jul 21 '25 00:07 refi64