显示后退按钮而不是抽屉按钮-颤振

时间:2019-08-12 21:59:29

标签: flutter flutter-layout

我有一个Flutter项目,该项目使用了材质设计,当我浏览路线时,应用程序栏将显示后退按钮。最近,我刚刚在项目中实现了一个抽屉,并且抽屉图标覆盖了后退图标。我本质上是想撤消此操作,在某些屏幕上显示后退按钮,在其他屏幕上显示菜单按钮,就像在定义具有showIcon: false属性的抽屉时一样?我知道这个post是一个类似的问题,但是没有显示该问题或解决方案的代码...我的抽屉看起来像这样:

return Scaffold(
  //appbar is here
  appBar: AppBar(
    title: Text("Title"),
  ),
  drawer: drawer,
  body: _buildBody(),
);

我在这里定义抽屉:

var drawer = Drawer(
  child: ListView(
    padding: EdgeInsets.zero,
    children: <Widget>[
      //My listTiles and UserAccountsDrawerHeader are removed for simplicity
    ],
  ),
);

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

我不确定是否能正确处理您的问题,这是输出,下面是完整的代码。

enter image description here

void main() => runApp(MaterialApp(home: MyPage()));

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (_) => Page1())),
              child: Text("Go to Drawer Page"),
            ),
            RaisedButton(
              onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (_) => Page2())),
              child: Text("Go to Back button Page"),
            ),
          ],
        ),
      ),
    );
  }
}

// this has drawer
class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(),
      drawer: Drawer(),
    );
  }
}


// this has back button and drawer
class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () => Navigator.pop(context),
        ),
      ),
      body: Center(),
      drawer: Drawer(),
    );
  }
}

答案 1 :(得分:0)

AppBar(
leading: Builder(
builder: (BuildContext context) {
  return IconButton(
    icon: const Icon(Icons.arrow_back),
    onPressed: () { Navigator.pop(context); },

  );
},
),
)