从自己类中的appbar调用body函数

时间:2019-03-27 00:48:55

标签: dart flutter

我的应用程序每个“页面”都有一个类,并且它们都共享相同的应用程序栏,这是它自己的一个类。我在应用栏上有一个按钮,我希望它从打开的页面中调用一个函数。该功能在每个页面上,并且在每个页面上具有相同的名称。

在下面的我缩短的代码中,您看到共享的MyAppBar,并且看到2页。每个页面使用MyAppBar,每个页面使用_myFunction()

如何为_myFunction()中的每个当前页面调用MyAppBar

class MyAppBar {
  setAppBar(context, String title) {
    return new AppBar(
      backgroundColor: Colors.blue,
      title: Text(
        title,
        style: TextStyle(color: Colors.white),
      ),
      actions: <Widget>[
          child: IconButton(myIcon),
          onPressed: () => { this should call the current pages _myFunction},),
    ],
  }
}


class _Page1State extends State<Page1>
{
@override
Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar().setAppBar(context, 'Page 1'),
      body: Container(some content here)
      )
      }

    _myFunction()
    {
    do some stuff;
    }
}

class _Page2State extends State<Page2>
{
@override
Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar().setAppBar(context, 'Page 2'),
      body: Container(some content here)
      )
      }

    _myFunction()
    {
    do some stuff;
    }
}

1 个答案:

答案 0 :(得分:1)

您只需传递这些功能即可。

import 'package:flutter/material.dart';

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  MyAppBar({this.pageInstanceFunction});
  var pageInstanceFunction;

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: Colors.orange,
      title: Text('My Custom AppBar for #page'),
      actions: <Widget>[
        IconButton(
          icon: Icon(Icons.ac_unit),
          onPressed: () {
            pageInstanceFunction();
          },
        ),
      ],
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}

这是我的第一页

import 'package:flutter/material.dart';
import 'package:stackoverflow/MyAppBar.dart';
import 'package:stackoverflow/PageTwo.dart';

class PageOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        pageInstanceFunction: sayHello,
      ),
      body: Container(
        color: Colors.deepOrange,
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Center(
          child: RaisedButton(
            onPressed: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) => PageTwo()));
            },
            child: Text('Page Two'),
          ),
        ),
      ),
    );
  }

  void sayHello() {
    print('Hello from PageOne');
  }
}

第二页

import 'package:flutter/material.dart';
import 'package:stackoverflow/MyAppBar.dart';

class PageTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        pageInstanceFunction: sayHello,
      ),
      body: Container(
        color: Colors.lightBlueAccent,
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Text('Page Two'),
      ),
    );
  }

  void sayHello() {
    print('Hello from PageTwo');
  }
}