颤振回调未触发

时间:2019-08-04 01:51:15

标签: flutter dart

我试图在TextEditingController addListener中创建一个回调,但是该回调不会触发

我想在应用程序栏中创建搜索,但是我的应用程序栏类和GridView类是不同的。因此,我想为TextEditingController addListener创建一个回调。

class Home extends StatefulWidget {
  .....

  void Function(String filterText) onSearchEntered = (filterText){};
  Home.search({this.onSearchEntered});

  ....
}

class MyHomePage extends State<Home> {
   .....

  TextEditingController filter = TextEditingController();

  MyHomePage() {
    filter.addListener(() {
      widget.onSearchEntered(filter.text);
    });
  }

   .....
}

class SeafoodPage extends StatefulWidget {
  SeafoodPage({Key key}) : super(key: key);

  @override
  _SeafoodPageState createState() => _SeafoodPageState();
}

class _SeafoodPageState extends State<SeafoodPage> {
   ....

  _SeafoodPageState() {
    Home.search(
      onSearchEntered: (String filterText) {
        print("Log: Hello");
        if (filterText.isEmpty) {
          setState(() {
            searchText = "";
            filteredFoods = foods;
            print("LogSearchText 1: $searchText");
          });
        } else {
          setState(() {
            searchText = filterText;
            print("LogSearchText 2: $searchText");
            print("LogFilterText 1: $filterText");
          });
        }
      },
    );
  }

  ....
}

我希望用户在搜索栏中进行搜索时,会触发_SeafoodPageState中的回调。

0 个答案:

没有答案