如何在 Flutter 中添加 Appbar Actions

Kar*_*han 6 android flutter

在android中,每个fragment都覆盖了onCreateOptionsMenu,这提供了在每个fragment中添加单独的选项菜单的可能性。当应用程序栏对于应用程序是通用的时,如何在从抽屉中更改页面时使用 flutter 来实现这一点

Div*_*hta 8

flutter 的丰富之处在于,您不必编写大量属性,例如 html、css 等大量内容,
flutter 提供了使用一个属性的简单方法,如下例所示

在脚手架下只需添加此代码

appBar: AppBar(
      title: Text("Action Demo"),
      actions: <Widget>[
        IconButton(
          icon: Icon(
            Icons.settings,
            color: Colors.white,
          ),
          onPressed: () {
            // do something
          },
        )
      ],
    ),
Run Code Online (Sandbox Code Playgroud)


Lak*_*ngh 5

您可以在每个屏幕中添加 AppBar。

 class SDF extends StatefulWidget {
  @override
  _SDFState createState() => _SDFState();
}

class _SDFState extends State<SDF> {
  int body = 0;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("I Am AppBar"),
        actions: getActions(body),
      ),
      body: getBody(body), //here you can add your body
      drawer: Drawer(
        child: Column(
          children: <Widget>[
             RaisedButton(
          onPressed: () {
            setState(() {
              body = 0; 
            });

          },
          child: Text("Home"),
        ),
        RaisedButton(
          onPressed: () {
            setState(() {
              body = 1;
            });

          },
          child: Text("Settings"),
        ),
          ],
        ),
      ),
    );
  }

  getBody(int body) {
    switch (body) {
      case 0:
        return Container(); // Home Screen

      case 1:
        return Container(); // Settings Screen
    }
  }

  getActions(int body) {
    switch (body) {
      case 0:
        return [
          Icon(Icons.settings),
          Icon(Icons.home),
        ]; // Home Screen

      case 1:
        return [
          Icon(Icons.home),
          Icon(Icons.settings),
        ]; // Settings Screen
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我们如何添加 onpressed 或 on Taped 功能,以便在点击操作按钮后可能转到不同的页面 (2认同)