应用栏中发生更改时,请在statefullwidget中重新加载数据

时间:2019-10-01 14:17:16

标签: search flutter widget bloc

在我的应用栏中,我有一个用于在statefullwidget中搜索数据的文本字段。但是,当用户搜索数据时,如何重新加载statefullwidget?

class HorseFilterPage extends StatelessWidget {
  final _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          brightness: Brightness.light,
          backgroundColor: new Color(0xfafafa),
          iconTheme: IconThemeData(
            color: Colors.black, //change your color here
          ),
          elevation: 0.0,
          title: new TextField(
            style: new TextStyle(color: Colors.black87),
            controller: _controller,
            onChanged: (text) {
              _sendDataToSecondScreen(context, text);
            },
            decoration: new InputDecoration(
              prefixIcon: new Icon(Icons.search, color: Colors.black87),
              hintText: 'Search...',
              hintStyle: TextStyle(color: Colors.black87),
              focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.black87)),
            ),
          ),
          actions: <Widget>[
            IconButton(
              icon: Icon(
                Icons.close,
                color: Colors.black87, // Here
              ),
              onPressed: () {
                _controller.clear();
              },
            ),
          ],
        ),
        /*
      body: Container(child: HorseFilterForm()),
      */
        body: BlocProvider(
            builder: (context) {
              return HorseOverviewBloc(httpClient: http.Client())
                ..dispatch(
                  Fetch(),
                );
            },
            child: HorseFilterForm(_controller.text)));
  }

  void _sendDataToSecondScreen(BuildContext context, String text) {
     print('Text ===>' + text);
  }
}

我的StatefullWidget只有一小段代码:

class HorseFilterForm extends StatefulWidget {

   final String searchValue;
   HorseFilterForm(this.searchValue);

  @override
  State<HorseFilterForm> createState() => _HorseFilterFormState(searchValue);


}

class _HorseFilterFormState extends State<HorseFilterForm> {
   String value;

  _HorseFilterFormState(searchValue) {
    this.value = searchValue;
  }

  final _scrollController = ScrollController();
  final _scrollThreshold = 100.0;
  HorseOverviewBloc _homeBloc;

<the rest of my code>

}

如何将数据发送到另一个小部件?在我的horseFilterForm中,我使用Bloc来处理状态。获取数据的事件之一是:

_homeBloc.dispatch(Search(
        queryParameters: getQueryParameters(_archivedFilter, _ageFilter)));

其中_archivedFilter和_ageFiltere是我的参数。

0 个答案:

没有答案