Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

cannot dismiss Flushbar on button tap (Flushbar is a package for Flutter)

According to the documentation it shall work like this:

flush = Flushbar<List<String>>(
  userInputForm = Form(
          key: _formKey,
          child: Column(
            children: [
              getTextFormField("Initial Value"),
            ]
           MaterialButton(
                  child: Text("SUBMIT"),
                  onPressed: () {
                    flush.dismiss([_controller1.value.text]);
                  },
                ),
          ],),),
)..show(context).then((result) {
        if (result != null) {
          String userInput1 = result[0];
        }
      });

Not sure, how this example is embedded in the surrounding code. In my case, I have a separate file with the different flushbars. I call them by

CupertinoButton(
                        onPressed: () => flush = newArea(context, _formKey, flush),
                        child: Text('add')
                    ),

My initial attempt did not work and since in my set-up flush.dismiss did not work, because I was lacking flush in the context of my "stand-alone-flush-function", I added the argument Flushbar<List<String>> flush; to the function call, but that didn't help either.

The error shown is: (and it vanishes as soon as I make the dismiss line a comment)

======== Exception caught by gesture library =======================================================
The following RangeError was thrown while dispatching a pointer event:
RangeError (index): Invalid value: Only valid value is 0: 1

When the exception was thrown, this was the stack: 
#0      List.[] (dart:core-patch/growable_array.dart:177:60)
#1      StackFrame.fromStackTraceLine (package:flutter/src/foundation/stack_frame.dart:214:36)
#2      MappedIterator.moveNext (dart:_internal/iterable.dart:392:20)
#3      WhereTypeIterator.moveNext (dart:_internal/iterable.dart:875:20)
#4      new List.from (dart:core-patch/array_patch.dart:50:19)

My Flush function looks like this:

Flushbar<List<String>> newArea(BuildContext context,
    GlobalKey _formKey, Flushbar flush) {
  ProjectKanbanBloc kanbanBloc = BlocProvider.of<ProjectKanbanBloc>(context);

  TextEditingController inputTec = TextEditingController();
  String areaName;
  
  return Flushbar(
      userInputForm: Form(
        key: _formKey,
        child: Expanded(
          child: StatefulBuilder(
            builder: (BuildContext contextFlush, StateSetter setState) {
            return Column(
              children: <Widget>[
                  child: CupertinoTextField(
                    controller: inputTec,
                    onSubmitted: (val) {
                      setState(() {
                        areaName = val;
                      });
                    },
                )),
                CupertinoButton(
                  child: Text('save'),
                  onPressed: () {
                    BlocProvider.of<ProjectKanbanBloc>(context).add(
                        ProjectKanbanAreaAdded(areaName: inputTec.text));
                    flush.dismiss();    // <<<<<<<<<<<<<<<<-------------- DISMISS
                  },
                )
          ]);
        })),

      ))..show(context);
}
question from:https://stackoverflow.com/questions/65886705/cannot-dismiss-flushbar-on-button-tap-flushbar-is-a-package-for-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Got it! You can simply use Navigator.pop(context); within onPressed:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...