如何使用工具栏按钮切换抽屉面板的主抽屉

时间:2014-07-30 23:02:08

标签: dart dart-polymer material-design paper-elements core-elements

我正在尝试按下工具栏上的按钮,单击按钮时切换抽屉面板的主抽屉,但没有太大成功。

代码如下所示 - 方法void menu_icon_handler()中的代码不正确。

    <!DOCTYPE html>
    <link rel="import" href="../../../packages/polymer/polymer.html">
    <link rel="import" href="../../../packages/core_elements/core_drawer_panel.html">
    <link rel="import" href="../../../packages/core_elements/core_icon_button.html">
    <link rel="import" href="../../../packages/core_elements/core_toolbar.html">
    <link rel="import" href="../../../packages/paper_elements/roboto.html">
    <link rel="import" href="../../../packages/core_elements/core_toolbar.html">
    <link rel="import" href="../../../packages/paper_elements/paper_icon_button.html">
    <link rel="import" href="./reg/registrant-view.html">
    <link rel="import" href="./reg/patient-view.html">
    <link rel="import" href="drawer-form.html">

    <polymer-element name="main-form">

      <template>
        <core-drawer-panel id="core_drawer_panel">
          <section id="drawer" drawer>
            <drawer-form on-new-patient-event='{{newPatientHandler}}'></drawer-form>
          </section>

          <section id="main" main>
            <core-toolbar id="core_toolbar">
              <paper-icon-button
                icon="menu"
                id='menu_icon'
                on-click='{{menu_icon_handler}}'></paper-icon-button>
              <div id="div" flex>Toolbar</div>
            </core-toolbar>

            <section id='new-patient'>

            </section>

          </section>
        </core-drawer-panel>

      </template>

      <script type="application/dart">

        import 'package:polymer/polymer.dart';
        import 'dart:html';

        import 'package:core_elements/core_drawer_panel.dart';

        @CustomTag( 'main-form' )
        class MainForm extends PolymerElement
        {
          CoreDrawerPanel drawerPanel;

          MainForm.created() : super.created();

          void toggleDrawer()
          {
            bool state = drawerPanel.narrow;
          }

          void menu_icon_handler()
          {
            bool state =  drawerPanel.narrow;

            print ( state );
          }

          @override
          void attached()
          {
             super.attached();

             drawerPanel = $['drawerPanel']  as CoreDrawerPanel;
          }
        }
      </script>
    </polymer-element>

单击工具栏按钮时,我得到以下stacktrace

异常:null对象没有getter'narrow'。

NoSuchMethodError: method not found: 'narrow'
Receiver: null

我看到null,查看文档'narrow'是CoreDrawerPanel的属性

1 个答案:

答案 0 :(得分:1)

你用

查看元素
drawerPanel = $['drawerPanel']  as CoreDrawerPanel;

但您的HTML不包含ID为drawerPanel的元素,但您有一个ID为core_drawer_panel的元素

更改此行中的ID应解决问题

drawerPanel = $['core_drawer_panel']  as CoreDrawerPanel;