Flutter calling child class function from parent class

前端 未结 4 1385
生来不讨喜
生来不讨喜 2021-01-04 04:01

Code:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 04:52

    I think the best practice in this case is using the way that the programmers of the Flutter used themselves!

    Bellow code is an example from Flutter source. As you can see there in MyCustomForm widget if you want to access the text property of TextField widget (which is the child of MyCustomForm widget in this example) you need to use its controller like this...

    class _MyCustomFormState extends State {
      ...
      final myController = TextEditingController();
      ...
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          ...
          body: TextField(
            controller: myController,
          ),// TextField
          ...
          floatingActionButton: FloatingActionButton(
            ...
            onPressed: () {
              print(myController.text);
            },
            ...
          ),// FloatingActionButton
        );
      }
    }
    

    Now be inspired by this and also knowing the fact that Flutter pass its object parameters by reference so we need a controller class for our child class to be able to access child's fields and functions through the controller object (like the TextField widget in previous example)...in your case we can do it like this:

    class HomePageController {
      void Function() methodA;
    }
    
    class MyApp extends StatelessWidget {
    
      final HomePageController myController = HomePageController();
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              leading: IconButton(
                icon: Icon(Icons.help),
                onPressed: () {
                  myController.methodA();
                },
              ),// IconButton
            ),// AppBar
            body: HomePage(
              controller: myController,
            ),// HomePage
          ),
        );
      }
    }
    
    class HomePage extends StatefulWidget {
    
      final HomePageController controller;
    
      HomePage({ this.controller });
    
      @override
      _HomePageState createState() => _HomePageState(controller);
    }
    
    class _HomePageState extends State {
    
      _HomePageState(HomePageController _controller) {
        _controller.methodA = methodA;
      }
    
      @override
      Widget build(BuildContext context) {
        return Container();
      }
    
      void methodA() {}
    }
    

    As can be seen in this code myController's reference passes through HomePage to reach the _HomePageState's constructor and then gets the reference of methodA function... and now methodA is accessible in MyApp class via myController!

    Hope it Helps! :))

提交回复
热议问题