flutter 我如何在search方法中传递容器的颜色

x4shl7ld  于 2023-01-09  发布在  Flutter
关注(0)|答案(2)|浏览(152)
? AppBar(
                backgroundColor: Colors.white,
                title: TextField(
                    onChanged: (value) {
                      print("000$value");
                      setState(() {
                        if (value.isNotEmpty) {
                          serchlist = [];
                          for (int u = 0; u < notedata.length; u++) {
                            String title = notedata[u]['TITLE'];
                            String subject = notedata[u]['NOTES'];
                            int color = notedata[u]['COLORS'];
                            if (title
                                    .toLowerCase()
                                    .contains(value.toLowerCase()) ||
                                subject
                                    .toUpperCase()
                                    .contains(value.toUpperCase()) ) {
                              print("111$title");
                              serchlist.add(notedata[u]);
                            } else {}
                          }
                        } else {
                          serchlist = notedata;
                        }
                      });
                    },
                    autofocus: true,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: "🔍 search",
                      suffixIcon: IconButton(
                          onPressed: () {
                            setState(() {
                              issearch = false;
                            });
                          },
                          icon: Icon(
                            Icons.close,
                            color: Colors.black,
                          )),
                    )),
              )
            : AppBar(
                leading: IconButton(onPressed: () {}, icon: Icon(Icons.menu)),
                actions: [
                  IconButton(
                      onPressed: () {
                        setState(() {
                          issearch = true;
                        });
                      },
                      icon: Icon(Icons.search_sharp)),
                  IconButton(onPressed: () {}, icon: Icon(Icons.more_vert)),
                ],
                backgroundColor: Colors.orange.shade500,
                elevation: 5,
                title: Text("Notes",
                    style: TextStyle(fontFamily: "regular", fontSize: 26))),

我是如何在search方法中传递容器的颜色的。

oaxa6hgo

oaxa6hgo1#

编辑你的代码 checkout 以下代码.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool issearch = true;
  List<Map> serchlist = [];
  List<int> colorlist = [];
  List<Map> notedata = [
    {
      'TITLE': "yellow",
      'NOTES': "notes1",
      'COLORS': 4,
    },
    {
      'TITLE': "blue",
      'NOTES': "notes1",
      'COLORS': 3,
    },
    {
      'TITLE': "red",
      'NOTES': "notes1",
      'COLORS': 2,
    }
  ];

  @override
  void initState() {
    super.initState();
    serchlist = notedata;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: issearch
          ? AppBar(
              backgroundColor: Colors.white,
              title: TextField(
                  onChanged: (value) {
                    setState(() {
                      if (value.isNotEmpty) {
                        serchlist = [];
                        for (int u = 0; u < notedata.length; u++) {
                          String title = notedata[u]['TITLE'];
                          String subject = notedata[u]['NOTES'];
                          int color = notedata[u]['COLORS'];
                          if (title
                                  .toLowerCase()
                                  .contains(value.toLowerCase()) ||
                              subject
                                  .toUpperCase()
                                  .contains(value.toUpperCase()) ||
                              color.toString().contains(value.toLowerCase())) {
                            serchlist.add(notedata[u]);
                            colorlist.add(color);
                          }
                        }
                      } else {
                        serchlist = notedata;
                      }
                      setState(() {});
                    });
                  },
                  autofocus: true,
                  decoration: InputDecoration(
                    icon: const Text("🔍"),
                    border: InputBorder.none,
                    hintText: " search",
                    suffixIcon: IconButton(
                        onPressed: () {
                          setState(() {
                            issearch = false;
                          });
                        },
                        icon: const Icon(
                          Icons.close,
                          color: Colors.black,
                        )),
                  )),
            )
          : AppBar(
              leading:
                  IconButton(onPressed: () {}, icon: const Icon(Icons.menu)),
              actions: [
                IconButton(
                    onPressed: () {
                      setState(() {
                        issearch = true;
                      });
                    },
                    icon: const Icon(Icons.search_sharp)),
                IconButton(onPressed: () {}, icon: const Icon(Icons.more_vert)),
              ],
              backgroundColor: Colors.orange.shade500,
              elevation: 5,
              title: const Text("Notes",
                  style: TextStyle(fontFamily: "regular", fontSize: 26))),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          ListView.builder(
            shrinkWrap: true,
            itemCount: serchlist.length,
            itemBuilder: ((context, index) {
              return Card(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text("TITLE ${serchlist[index]['TITLE'] ?? ""}"),
                      Text(
                          "NOTES ${serchlist[index]['NOTES'] as String ?? ""}"),
                      Text("COLORS ${serchlist[index]['COLORS'] as int ?? 0}"),
                    ],
                  ),
                ),
              );
            }),
          )
        ],
      ),
    );
  }
}
4urapxun

4urapxun2#

通过搜索发送三个参数中的任意一个,并从服务器获取结果

从这个问题我知道用户可以通过颜色,主题或标题搜索,只要让用户输入任何东西,你就可以从数据库中搜索它匹配的参数,然后返回给API或用户。但要确保你已经在服务器端实现了这一点。

相关问题