簡體   English   中英

Flutter - 如何在搜索時將搜索文本從一個小部件傳遞到另一個 class 小部件?

[英]Flutter - How to pass search text from one widget to its another class widget while searching?

我在主狀態小部件 class 中有一個搜索視圖,在另一個小部件 class 中有一個列表視圖。 現在,當用戶搜索某些內容時,我想將搜索文本傳遞給 listview 小部件以調用 api。

那么我該怎么做呢?

第一次打開列表時會起作用,但是當我搜索某些內容時,我該如何再次調用它?

我的代碼片段如下。

在此處輸入圖像描述

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

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();
}

您應該從父級傳遞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,
          ),
        ),
      ],
    ),
  ],
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM