脚手架抽屉以显示ModalBottomSheet

时间:2019-08-20 05:33:24

标签: flutter

myApp()的主屏幕上,我有一个无状态窗口小部件,它包含一个MaterialApp和一个Scaffold。脚手架的属性为drawer,我通过创建了一个抽屉,抽屉中的其中一个项目需要在关闭showModalBottomSheet的同时打开drawer。我该如何实现?我尝试传递context本身,并传递globalKey.currentContext(在GlobalKey<ScaffoldState> globalKey = GlobalKey();之后),但有时抽屉关闭,其他时间给我NoMethodFoundException(或类似的东西)

简而言之,当点击Scaffold drawerdrawer时,如何拥有一个包含其中一项的showModalBottomSheet

当前代码:

class Timeline extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    GlobalKey<ScaffoldState> homeScaffoldKey = GlobalKey();

    return MaterialApp(
      title: "Test",
      theme: ThemeData(
        appBarTheme: AppBarTheme(iconTheme: IconThemeData(color: Colors.black)),
      ),
      home: Scaffold(
        key: homeScaffoldKey,
        drawer: showDrawer(homeScaffoldKey.currentContext),
        backgroundColor: Colors.grey[100],
        body: Stack(
          children: <Widget>[
            HomePageView(),
            AppBar(
              elevation: 0,
              backgroundColor: Colors.transparent,
            ),
          ],
        ),
      ),
    );
  }
}

Drawer showDrawer(BuildContext context) {
  void showCalendarsModalBottom() {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext builder) {
        return ListView.builder(
          itemCount: repo.calendars.length,
          itemBuilder: (builder, index) {
            return StatefulBuilder(
              builder: (builder, StateSetter setState) => ListTile(
                leading: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Checkbox(
                      value: repo.getIsEnabledCal(repo.getCal(index)),
                      onChanged: (value) {
                        setState(() {
                          repo.toggleCalendar(repo.getCal(index));
                        });
                      },
                    ),
                    Container(
                      height: 14,
                      width: 14,
                      margin: EdgeInsets.only(left: 2, right: 6),
                      decoration: BoxDecoration(
                        color: Colors.redAccent,
                        shape: BoxShape.circle,
                      ),
                    ),
                    Text(
                      repo.getCal(index).name,
                      style: TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ],
                ),
                onTap: () {
                  setState(() {
                    repo.toggleCalendar(repo.getCal(index));
                  });
                },
              ),
            );
          },
        );
      },
    );
  }

  return Drawer(
    child: ListView(
      children: <Widget>[
        DrawerHeader(
          child: Align(
            child: Text('Timeline', textScaleFactor: 2),
            alignment: Alignment.bottomLeft,
          ),
        ),
        ListTile(
          title: Text('Dark Mode'),
          onTap: () => Navigator.pop(context),
        ),
        ListTile(
          title: Text('Calenders'),
          onTap: () {
            Navigator.pop(context);

            showCalendarsModalBottom();
          },
        )
      ],
    ),
  );
}

1 个答案:

答案 0 :(得分:1)

根据您的代码段更新了工作代码:

您将需要有statefulwidget来帮助将上下文从抽屉传递到底页,并将context作为参数传递给showCalendarModalBottomSheet()方法。

void main() {
  runApp(new MaterialApp(home: Timeline(), debugShowCheckedModeBanner: false));
}

class Timeline extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      title: "Test",
      theme: ThemeData(
        appBarTheme: AppBarTheme(iconTheme: IconThemeData(color: Colors.black)),
      ),
     home: MyHomePage()
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        drawer: AppDrawer(),
        backgroundColor: Colors.grey[100],
        body: Stack(
          children: <Widget>[
            //HomePageView(),
            AppBar(
              elevation: 0,
              backgroundColor: Colors.transparent,
            )
          ],
        )
      );
    }

    Widget AppDrawer() {
      return Drawer(
        child: ListView(
          children: <Widget>[
            DrawerHeader(
              child: Align(
                child: Text('Timeline', textScaleFactor: 2),
                alignment: Alignment.bottomLeft,
              ),
            ),
            ListTile(
              title: Text('Dark Mode'),
              onTap: () => Navigator.pop(context),
            ),
            ListTile(
              title: Text('Calenders'),
              onTap: () {
                Navigator.of(context).pop();
                showCalendarsModalBottom(context);
              },
            )
          ],
        ),
      );
    }

    Future<Null> showCalendarsModalBottom(context) {
      return showModalBottomSheet(context: context, builder: (context) => Container(
        color: Colors.red,
        // your code here
      ));
    }
  }

输出为:轻按应用程序抽屉菜单“日历”后,它将无缝关闭并打开底页。如果再次点击应用程序抽屉并重复步骤,您会看到抽屉和底页之间的平滑过渡。希望这能回答您的问题。

enter image description here

enter image description here