Как отправить параметры для установки в Flutter - PullRequest
1 голос
/ 03 июля 2019

Я не могу установить значение fn updatedDetail

Я хочу установить значение в fn updatedDetail! = Null, когда я щелкаю по значку на кнопке Function (bool) changesDetail;

во втором виджете с состоянием

Function(bool) changedDetail ; 

во втором виджете State

Expanded(
            flex: 1,
            child: InkWell(
              onTap: () {
                setState(() {
                  return this.widget.changedDetail(false);
                });
              },
              child: Container(
                  padding: EdgeInsets.only(top: 12.0),
                  child: Icon(
                    FontAwesomeIcons.thLarge,
                    color: KwanjaiColors.grey3,
                  )),
            ),
          ),
          Expanded(
            flex: 1,
            child: InkWell(
              onTap: () {
                setState(() {
                  return this.widget.changedDetail(true);
                });
              },
              child: Container(
                  padding: EdgeInsets.only(top: 12.0),
                  child: Icon(
                    FontAwesomeIcons.thList,
                    color: KwanjaiColors.grey3,
                  )),
            ),
          ),

в родительском

  bool clicktap = false;
  bool choiceTap = false;

  tapFn(bool choice) {
    setState(() {
      choice == true ? choiceTap = true : choiceTap = false;
    });
  }

в родительском виджете

 body: Column(children: <Widget>[
          Container(
            child: new ProjectIndexTab(changedDetail: tapFn(clicktap)),
          ),
          Expanded(
            child: Container(
              color: KwanjaiColors.bg,
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
                child: Column(children: <Widget>[
                  Container(child: ProjectIndexWorkDaily()),
                  Expanded(
                    // cuz has gridview must have expanded !!!!
                    child: Column(children: <Widget>[
                      Expanded(
                          child: choiceTap == true
                              ? ProjectViewList()
                              : ProjectGridList())
                    ]),
                  ),
                ]),
              ),
            ),
          ),
        ])

Журнал

I / flutter (12900): было сгенерировано другое исключение: NoSuchMethodError: Вызов метода'был вызван на ноль.

Ответы [ 2 ]

1 голос
/ 03 июля 2019

Попробуйте это:

В родительском виджете передайте функцию без каких-либо параметров

Я предполагаю, что вы уже установили конструктор с помощью ключадля вашего второго виджета

В заметке не нужно проверять, если choiceTap == true, по умолчанию, если bool пуст, устанавливается в значение false

body: Column(children: <Widget>[
          Container(
            child: new ProjectIndexTab(changedDetail: tapFn),
          ),
          Expanded(
            child: Container(
              color: KwanjaiColors.bg,
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
                child: Column(children: <Widget>[
                  Container(child: ProjectIndexWorkDaily()),
                  Expanded(
                    // cuz has gridview must have expanded !!!!
                    child: Column(children: <Widget>[
                      Expanded(
                          child: choiceTap
                              ? ProjectViewList()
                              : ProjectGridList())
                    ]),
                  ),
                ]),
              ),
            ),
          ),
        ])

Во втором виджете с состоянием

final void Function(bool value) changedDetail;

Во втором виджете вам не нужно возвращать функцию

Expanded(
            flex: 1,
            child: InkWell(
              onTap: () {
                setState(() {
                  widget.changedDetail(false);
                });
              },
              child: Container(
                  padding: EdgeInsets.only(top: 12.0),
                  child: Icon(
                    FontAwesomeIcons.thLarge,
                    color: KwanjaiColors.grey3,
                  )),
            ),
          ),
          Expanded(
            flex: 1,
            child: InkWell(
              onTap: () {
                setState(() {
                  widget.changedDetail(true);
                });
              },
              child: Container(
                  padding: EdgeInsets.only(top: 12.0),
                  child: Icon(
                    FontAwesomeIcons.thList,
                    color: KwanjaiColors.grey3,
                  )),
            ),
          ),
0 голосов
/ 04 июля 2019

В родительском виджете

class ProjectIndexScreen extends StatefulWidget {
      ProjectIndexScreen({Key key, this.title}) : super(key: key);
      final String title;
      @override
      ProjectIndexScreenState createState() => ProjectIndexScreenState();
    }

    class ProjectIndexScreenState extends State<ProjectIndexScreen> {
      void _incrementCounter() {
        setState(() {
          // This call to setState tells the Flutter framework that something has
          // changed in this State, which causes it to rerun the build method below
          // so that the display can reflect the updated values. If we changed
          // _counter without calling setState(), then the build method would not be
        });
      }

      void initState() {
        super.initState();
      }

      bool clicktap = false;
      bool choiceTap = false;

      tapFn(bool choice) {
        setState(() {
          choice == true ? choiceTap = true : choiceTap = false;
        });
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              backgroundColor: KwanjaiColors.title,
              title: Container(
                child: Row(
                  children: <Widget>[
                    Expanded(
                      flex: 7,
                      child: Container(
                        padding: EdgeInsets.only(right: 50.0),
                        child: Text(
                          "Choose Project",
                          style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                              color: KwanjaiColors.white),
                          textAlign: TextAlign.end,
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Icon(
                        Icons.notifications,
                        color: KwanjaiColors.white,
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Icon(
                        Icons.menu,
                        color: KwanjaiColors.white,
                      ),
                    )
                  ],
                ),
              ),
            ),
            body: Column(children: <Widget>[
              Container(
                child: new ProjectIndexTab(changedDetail: tapFn(clicktap)),
              ),
              Expanded(
                child: Container(
                  color: KwanjaiColors.bg,
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
                    child: Column(children: <Widget>[
                      Container(child: ProjectIndexWorkDaily()),
                      Expanded(
                        // cuz has gridview must have expanded !!!!
                        child: Column(children: <Widget>[
                          Expanded(
                              child:
                                  choiceTap ? ProjectViewList() : ProjectGridList())
                        ]),
                      ),
                    ]),
                  ),
                ),
              ),
            ]));
      }
    }

во втором виджете

class ProjectIndexTab extends StatefulWidget {
  final void Function(bool value) changedDetail;
  ProjectIndexTab({Key key, this.changedDetail}) : super(key: key);
  @override
  ProjectIndexTabState createState() => new ProjectIndexTabState();
}

class ProjectIndexTabState extends State<ProjectIndexTab> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
      color: KwanjaiColors.white,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Expanded(
            flex: 6,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 8.0),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(22.0))),
              child: TextFormField(
                decoration: InputDecoration(
                    border: InputBorder.none,
                    contentPadding: const EdgeInsets.symmetric(
                        vertical: 10.0, horizontal: 10.0),
                    hintText: "Search",
                    enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: KwanjaiColors.grey2),
                        borderRadius: BorderRadius.all(Radius.circular(4.0)),
                        gapPadding: 0.0),
                    fillColor: KwanjaiColors.grey2,
                    filled: true,
                    hintStyle: TextStyle(color: KwanjaiColors.grey3),
                    prefixIcon: Icon(
                      Icons.search,
                      color: KwanjaiColors.grey3,
                    )),
              ),
            ),
          ),
          Expanded(
            flex: 1,
            child: InkWell(
              onTap: () {
                setState(() {
                  widget.changedDetail(false);
                });
              },
              child: Container(
                  padding: EdgeInsets.only(top: 12.0),
                  child: Icon(
                    FontAwesomeIcons.thLarge,
                    color: KwanjaiColors.grey3,
                  )),
            ),
          ),
          Expanded(
            flex: 1,
            child: InkWell(
              onTap: () {
                setState(() {
                  widget.changedDetail(true);
                });
              },
              child: Container(
                  padding: EdgeInsets.only(top: 12.0),
                  child: Icon(
                    FontAwesomeIcons.thList,
                    color: KwanjaiColors.grey3,
                  )),
            ),
          ),
        ],
      ),
    );
  }
}
...