Flutter-如何在搜索时将搜索文本从一个窗口小部件传递到另一类窗口小部件?

问题描述 投票:0回答:1

我在主要的全状态小部件类中有一个搜索视图,在另一个小部件类中有一个列表视图。现在,当用户搜索某些内容时,我想将搜索文本传递给listview小部件以调用api。

所以我该怎么办?

当第一次打开列表时,它将起作用,但是当我搜索某些内容时,如何再次调用它?

下面是我的代码段。

enter image description here

Stack(
      children: <Widget>[
        Flex(
          direction: Axis.vertical,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Divider(
              color: ColorsApp.gray,
              height: 1,
            ),
            searchView(),
            Expanded(
              child: OrderList(
                isSearch: isSearch,
                textSearch: textSearch,
                onCallBack: widget.onCalBack,
              ),
            ),
          ],
        ),
      ],
    );

现在更改搜索文字

 void _onTextChange(String value) {
    print(tag + "....value........" + value);

    if (value == "") {
      _writtingStatusController.add(true);
      _customSearchViewController.add(false);
      setState(() {
        textSearch = "";
        isSearch = true;
      });
    } else {
      _writtingStatusController.add(false);
      _customSearchViewController.add(true);
      setState(() {
        textSearch = value;
        isSearch = true;
      });
    }
  }

订单列表类别

class OrderList extends StatefulWidget {
  final String textSearch;
  final bool isSearch;
  final Function onCallBack;

  const OrderList({
    Key key,
    this.textSearch,
    this.onCallBack,
    this.isSearch,
  }) : super(key: key);

  @override
  _OrderListState createState() => _OrderListState();
}
flutter flutter-layout
1个回答
0
投票

您应该从父级传递onChanged方法。

class SearchView extends StatelessWidget {
  final void Function(String) onChanged;

  const SearchView({Key key, this.onChanged}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return 
        ...
        TextField(
          ...
          onChanged: this.onChanged,
          ...
        ),
        ...
  }
}

然后在父项中:

Stack(
  children: <Widget>[
    Flex(
      direction: Axis.vertical,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Divider(
          color: ColorsApp.gray,
          height: 1,
        ),
        SearchView(
            onChanged: _onChanged,
        ),
        Expanded(
          child: OrderList(
            isSearch: isSearch,
            textSearch: textSearch,
            onCallBack: widget.onCalBack,
          ),
        ),
      ],
    ),
  ],
);
© www.soinside.com 2019 - 2024. All rights reserved.