【问题标题】:Filtering JSON snapshot using textfield text使用文本字段文本过滤 JSON 快照
【发布时间】:2021-06-10 08:02:27
【问题描述】:

我创建了一个列表视图,该列表视图由从 Web 服务接收到的 JSON 字符串填充。 我想根据插入到 textField 小部件中的搜索字符串过滤数据。

这个未来接收到的json字符串:

future: fetchPacientes(value)

我应该如何根据插入到 textField 中的字符串从 snapShot 中获取过滤后的值?

body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Center(
              child: Text(
                'Patients'.tr().toString(),
                style: TextStyle(fontSize: 24.0, color: Colors.black45),
              ),
            ),
            SizedBox(
              height: 8,
            ),
            // in the build method above list
            TextField(
              decoration: InputDecoration(
                contentPadding: EdgeInsets.all(15.0),
                hintText: 'Filter by name or NHC',
              ),
              onChanged: (string) {
                print("filtering string->:"+ string);

              },
            ),

            Expanded(
              child: Consumer(
                builder: (context, watch, child) {
                  var value = watch(clinicaIdStateProvider).state;

                  return Container(
                    child: FutureBuilder(
                      future: fetchPacientes(value),
                      builder: (context, snapshot) {
                      
                        if (snapshot.hasData) {
                         
                          return ListView.builder(
                            itemCount: snapshot.data.length,
                            shrinkWrap: true,
                            itemBuilder: (BuildContext context, index) {
                              Paciente paciente = snapshot.data[index];
                              return new GestureDetector(
                                onTap: () {
                                  print("paciente seleccionada nbre: " +
                                      '${paciente.nombre_completo}');
                                },
                                child: new Card(

                                  elevation: 6,
                                  child: new Container(

                                    child: Column(
                                      children: [
                                        Padding(
                                          padding: const EdgeInsets.all(8.0),
                                          child: Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Padding(
                                                padding:
                                                    const EdgeInsets.all(8.0),
                                                child: Text(
                                                  "${paciente.apellidos}" +
                                                      ", " +
                                                      "${paciente.nombre_completo}",
                                                  style:
                                                      TextStyle(fontSize: 18),
                                                ),
                                              ),
                                              Column(
                                                children: [
                                                  Container(
                                                    decoration: BoxDecoration(
                                                        color:
                                                            AppColors.azulCapenergy,
                                                        border: Border.all(
                                                          color: AppColors
                                                              .azulCapenergy,
                                                        ),
                                                        borderRadius:
                                                            BorderRadius.all(
                                                                Radius.circular(
                                                                    20))),
                                                    child: Padding(
                                                      padding:
                                                          const EdgeInsets.all(8.0),
                                                      child: Text(
                                                        "NHC: ${paciente.NHC}",
                                                        style: TextStyle(
                                                            color: AppColors
                                                                .blancoCapenergy,
                                                            fontSize: 16),
                                                      ),
                                                    ),

                                                  ),
                                                  SizedBox(height: 3,),
                                                  FlatButton(
                                                    onPressed: (){
                                                      print("boton agenda pulsado de ${paciente.nombre_completo}");
                                                    },
                                                    child: Container(
                                                      decoration: BoxDecoration(
                                                          color:
                                                          Colors.transparent,
                                                          border: Border.all(
                                                            color: AppColors
                                                                .azulCapenergy,
                                                          ),
                                                          borderRadius:
                                                          BorderRadius.all(
                                                              Radius.circular(
                                                                  15))),
                                                      child: Padding(
                                                        padding:
                                                        const EdgeInsets.all(8.0),
                                                        child: Image.network(
                                                          'https://...iconos_app/agenda.png',
                                                          height: 25,
                                                        ),
                                                      ),

                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ),
                                        ),

                                      ],
                                    ),
                                  ),
                                ),
                              );
                            },
                          );
                        }

                        return Image.asset(
                          "assets/images/vacio.png",
                          fit: BoxFit.contain,
                        );
                      },
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),

【问题讨论】:

    标签: flutter


    【解决方案1】:

    使用 TextEditingController 并将其添加到 textField 控制器,然后您可以在任何地方访问 textField 文本。

    你可以用它来为snapshot.data写一个过滤方法,如果状态数量有限的话,你可以创建一个地图

    样本

     final TextEditingController _controller = TextEditingController();
    
    @override
      void initState() {
        _controller.addListener(_filterList);
        super.initState();
      }
    
    body: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Center(
                  child: Text(
                    'Patients'.tr().toString(),
                    style: TextStyle(fontSize: 24.0, color: Colors.black45),
                  ),
                ),
                SizedBox(
                  height: 8,
                ),
                // in the build method above list
                TextField(
                  controller: _controller,
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(15.0),
                    hintText: 'Filter by name or NHC',
                  ),
                  onChanged: (string) {
                    print("filtering string->:"+ string);
    
                  },
                ),
    
                Expanded(
                  child: Consumer(
                    builder: (context, watch, child) {
                      var value = watch(clinicaIdStateProvider).state;
    
                      return Container(
                        child: FutureBuilder(
                          future: fetchPacientes(value),
                          builder: (context, snapshot) {
                          
                            if (snapshot.hasData) {
                             
                              return ListView.builder(
                                itemCount: snapshot.data.length,
                                shrinkWrap: true,
                                itemBuilder: (BuildContext context, index) {
                                  Paciente paciente = snapshot.data[index];
                                  return new GestureDetector(
                                    onTap: () {
                                      print("paciente seleccionada nbre: " +
                                          '${paciente.nombre_completo}');
                                    },
                                    child: new Card(
    
                                      elevation: 6,
                                      child: new Container(
    
                                        child: Column(
                                          children: [
                                            Padding(
                                              padding: const EdgeInsets.all(8.0),
                                              child: Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.spaceBetween,
                                                children: [
                                                  Padding(
                                                    padding:
                                                        const EdgeInsets.all(8.0),
                                                    child: Text(
                                                      "${paciente.apellidos}" +
                                                          ", " +
                                                          "${paciente.nombre_completo}",
                                                      style:
                                                          TextStyle(fontSize: 18),
                                                    ),
                                                  ),
                                                  Column(
                                                    children: [
                                                      Container(
                                                        decoration: BoxDecoration(
                                                            color:
                                                                AppColors.azulCapenergy,
                                                            border: Border.all(
                                                              color: AppColors
                                                                  .azulCapenergy,
                                                            ),
                                                            borderRadius:
                                                                BorderRadius.all(
                                                                    Radius.circular(
                                                                        20))),
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets.all(8.0),
                                                          child: Text(
                                                            "NHC: ${paciente.NHC}",
                                                            style: TextStyle(
                                                                color: AppColors
                                                                    .blancoCapenergy,
                                                                fontSize: 16),
                                                          ),
                                                        ),
    
                                                      ),
                                                      SizedBox(height: 3,),
                                                      FlatButton(
                                                        onPressed: (){
                                                          print("boton agenda pulsado de ${paciente.nombre_completo}");
                                                        },
                                                        child: Container(
                                                          decoration: BoxDecoration(
                                                              color:
                                                              Colors.transparent,
                                                              border: Border.all(
                                                                color: AppColors
                                                                    .azulCapenergy,
                                                              ),
                                                              borderRadius:
                                                              BorderRadius.all(
                                                                  Radius.circular(
                                                                      15))),
                                                          child: Padding(
                                                            padding:
                                                            const EdgeInsets.all(8.0),
                                                            child: Image.network(
                                                              'https://...iconos_app/agenda.png',
                                                              height: 25,
                                                            ),
                                                          ),
    
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                ],
                                              ),
                                            ),
    
                                          ],
                                        ),
                                      ),
                                    ),
                                  );
                                },
                              );
                            }
    
                            return Image.asset(
                              "assets/images/vacio.png",
                              fit: BoxFit.contain,
                            );
                          },
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
    
    

    【讨论】:

    • 谢谢@DNS,但这只是我的问题,如何使用textField编写快照的过滤方法来获取搜索字符串
    • 这意味着您的问题是我的代码中的 _filterList 正文,对吗?
    • 我在你的代码中找不到_filterList?
    • 在 initState() 中,第 4 行
    • 是的,但我的意思是你在哪里声明 _filterList?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2018-03-06
    相关资源
    最近更新 更多