繁体   English   中英

如何在 flutter 的应用栏中添加抽屉?

[英]How can I add a drawer in the app bar in flutter?

我正在尝试在我的应用栏中添加一个抽屉,但我无法修复它。 我的代码看起来像这样,请帮忙!

Widget appBarMain(BuildContext context) {
  return AppBar(
    centerTitle: false,
    title: Image.asset(
      'assets/images/logox.png',
      height: 45,
    ),
    flexibleSpace: Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: <Color>[Colors.pink[400], Colors.blue[400]]),
      ),
    ),
  );
}

阅读https://flutter.dev/docs/cookbook/design/drawer

这样做

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: appBarMain(context),  // => appbar is an attribute in Scaffold
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

抽屉被添加到Scaffold而不是AppBar 只有它的图标会自动显示在 AppBar 中。

参见: https://flutter.dev/docs/cookbook/design/drawer

添加带脚手架的抽屉:

return Scaffold(
      appBar: appBarMain(context),
      body: Center(child: Text('My Page!')),
      drawer: Drawer(
        // Add a ListView to the drawer. This ensures the user can scroll
        // through the options in the drawer if there isn't enough vertical
        // space to fit everything.
        child: ListView(
          // Important: Remove any padding from the ListView.
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
              child: Text('Drawer Header'),
              decoration: BoxDecoration(
                color: Colors.blue,
              ),
            ),
            ListTile(
              title: Text('Item 1'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: Text('Item 2'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
    );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM