Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a item in List<S2Choice<String>> not refresh the view #47

Open
mnba3223 opened this issue Dec 28, 2022 · 1 comment
Open

Add a item in List<S2Choice<String>> not refresh the view #47

mnba3223 opened this issue Dec 28, 2022 · 1 comment

Comments

@mnba3223
Copy link

HI, thanks for good package.
I have some problems here.
I use a ListView.builder to render a three List<S2Choice> , and it's worked nice.
Now I need to add the new S2Choice in List. It has been added in list when I print List.
But the page not refresh the view When I use setState like this .

1672216319096

And I saw @absar solved this on #12 .
I follow his commit modify s2_state.dart like this ,

if (oldWidget.choiceItems != widget.choiceItems) {
      resolveChoices();
      WidgetsBinding.instance?.addPostFrameCallback((_) {
        choices?.reload();
        modalSetState?.call(() {});
      });
    }

but still not work.

Does anyone can help me?

here is my code.

return Expanded(
     child: ListView.builder(
       itemCount: test5.length,
       itemBuilder: (BuildContext context, int index) {
         return Container(
           decoration: const BoxDecoration(
               border: Border(
                   bottom:
                       BorderSide(width: 1.0, color: AppColors.lightGrey))),
           child: SmartSelect<String?>.single(
             modalHeaderBuilder: (context, value) {
               return SafeArea(
                 child: Row(
                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                     children: [
                       Text(options2[index]),
                       ElevatedButton(
                         style: ElevatedButton.styleFrom(
                             textStyle: const TextStyle(fontSize: 20)),
                         onPressed: () {
                           showEditAlertDialog(context, title: "add item name")
                               .then((value) {
                             if (value != null && value.isNotEmpty) {
                               setState(() {
                                 heroes.add(S2Choice<String>(
                                     value: value, title: value));
                                 test5[index] = heroes;
                   
                               });
                             }
                           });
                         },
                         child: const Text('add item'),
                       ),
                     ]),
               );
             },
             title: options2[index],
             selectedValue: _hero?[index],
             choiceItems: test5[index],
             modalType: S2ModalType.bottomSheet,
             onChange: (selected) => setState(() {
               _hero?[index] = selected.value ?? '';
               debugPrint(selected.title);
               returnValue["title"] = selected.value;
               debugPrint(returnValue["title"]);
             }),
             tileBuilder: (context, state) {
               return S2Tile.fromState(state,
                   isTwoLine: true,
                   leading: userIcon == ""
                       ? const Icon(Icons.person_outline_sharp,
                           color: Colors.grey, size: 100)
                       : CircleAvatar(
                           backgroundImage: NetworkImage(userIcon),
                         ));
             },
           ),
         );
       },
     ),
   );

flutter version : 3.3.9

@mnba3223
Copy link
Author

after i do some tests,
thanks for @absar code, I found the oldWidget.choiceItems is show updated items when I add a S2Choice

in s2_state.dart

void didUpdateWidget(SmartSelect<T> oldWidget) {
    super.didUpdateWidget(oldWidget);

    print("oldWidget is ${oldWidget.choiceItems}");
    print("Widget is ${widget.choiceItems}");

  }

It will print 4 items when I add one to a three items list.
oldWidget is [S2Choice#f52ad, S2Choice#8de44, S2Choice#192d6, S2Choice#5ccc1]
Widget is [S2Choice#f52ad, S2Choice#8de44, S2Choice#192d6, S2Choice#5ccc1]

I don't know why.

for now, my solution is just reload choices directly like this


  @override
  void didUpdateWidget(SmartSelect<T> oldWidget) {
    super.didUpdateWidget(oldWidget);
    WidgetsBinding.instance?.addPostFrameCallback((_) {
      choices?.reload(query: filter?.value);
      modalSetState?.call(() {});
    });

    if (!const ListEquality()
        .equals(oldWidget.choiceItems, widget.choiceItems)) {
      resolveChoices();
    }
  }

Maybe will cause some problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant