Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement event listener or delegate on flutter

I have a main dart class in which the app bar is located and the app bar contains a refresh button. I'm using a navigation drawer to populate two other views f1 and f2.

From my main.dart how can I pass the refresh button clicks to the sub fragment kind of f1.dart so that I can refresh my contents on f1.dart

// State of Main
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: new Column(
          children: <Widget>[

////////////////////////////////////////////////////////////
                new FirstFragment(),
                new SecondFragment()
/////////////////////////////////////////////////////////////
          ],
        ),
      ),
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.refresh),
            onPressed: () {
              print("refresh pressed");
/////////////////////////
         How to send this refresh pressed event to my FirstFragment class??
/////////////////////////
            },
            color: Colors.white,
          )
        ],
      ),
      body: _getDrawerItemWidget(_selectedDrawerIndex),
    );
  }

}

In Android, I've been using event listeners and for iOS, I can use delegates for the purpose. How can I achieve this on flutter/dart. ?

like image 865
krishnakumarcn Avatar asked Dec 05 '18 05:12

krishnakumarcn


People also ask

What is a delegate in flutter?

The delegate that provides the children for this widget. The children are constructed lazily using this delegate to avoid creating more children than are visible through the Viewport.

How does listener work in flutter?

It listens to events that can construct gestures, such as when the pointer is pressed, moved, then released or canceled. It does not listen to events that are exclusive to mouse, such as when the mouse enters, exits or hovers a region without pressing any buttons.

What is event handler in flutter?

The event handler object can be used for adding and removing listeners for the following events: create - triggered when an object is saved in the database table represented by the event handler. update - triggered when an object is updated in the database table represented by the event handler.

What is Addlistener flutter?

Adds a listener callback that is called whenever a new concrete ImageInfo object is available or an error is reported. If a concrete image is already available, or if an error has been already reported, this object will notify the listener synchronously.


1 Answers

You can pass a callback, use the VoidCallback and receive the event on your Main widget.

        class MainPage extends StatelessWidget {
          _onTapButton() {
            print("your event here");
          }

          @override
          Widget build(BuildContext context) {
            return Container(
              child: ChildPage(
                onTap: _onTapButton,
              ),
            );
          }
        }

        class ChildPage extends StatelessWidget {
          final VoidCallback onTap;

          const ChildPage({Key key, this.onTap}) : super(key: key);

          @override
          Widget build(BuildContext context) {
            return Container(
              child: RaisedButton(
                child: Text("Click Me"),
                onPressed: () {
                  //call to your callback  here
                  onTap();
                },
              ),
            );
          }
        } 

In case you want the opposite, you can just refresh the state of your parent widget and change the parameter that you pass to your fragments or also you can use GlobalKey, like the example below:

        class MainPage extends StatelessWidget {

          final GlobalKey<ChildPageState> _key = GlobalKey();

          _onTapButton() {
            _key.currentState.myMethod();
          }

          @override
          Widget build(BuildContext context) {
            return Container(
              child: Column(
                children: [
                  ChildPage(
                    key: _key,
                  ),
                  RaisedButton(
                    child: Text("Click me"),
                    onPressed: _onTapButton,
                  )
                ],
              )
            );
          }
        }

        class ChildPage extends StatefulWidget {
          const ChildPage({Key key}) : super(key: key);

          @override
          ChildPageState createState() {
            return new ChildPageState();
          }
        }

        class ChildPageState extends State<ChildPage> {

          myMethod(){
            print("called from parent");
          }

          @override
          Widget build(BuildContext context) {
            return Container(
              child: Text("Click Me"),
            );
          }
        }
like image 165
diegoveloper Avatar answered Sep 16 '22 18:09

diegoveloper