带有搜索过滤器的Flutter应用网格视图?

马祖克

在这个颤动的代码中,我在AppBar中实现了带有搜索过滤器的Gridview。目前仅显示建筑物名称,但我也想显示建筑物地名吗?有什么办法可以同时过滤建筑物名称和地名?

  1. 我也想在gridview中显示建筑物的地名-值
  2. 当单击网格项目时,如何获取项目ID?

    import 'package:flutter/material.dart';
    import 'package:realpro/SizeConfig.dart';
    import 'package:realpro/root/dashscreen.dart';
    
    class SearchList extends StatefulWidget {
      SearchList({ Key key }) : super(key: key);
      @override
      _SearchListState createState() => new _SearchListState();
       }
      class _SearchListState extends State<SearchList>
      {
      Widget appBarTitle = new Text("My Properties", style: new TextStyle(color: 
      Colors.white),);
      Icon actionIcon = new Icon(Icons.search, color: Colors.orange,);
      final key = new GlobalKey<ScaffoldState>();
      final TextEditingController _searchQuery = new TextEditingController();
      List<String>  _list;
      bool _IsSearching;
      String _searchText = "";
      _SearchListState() {
      _searchQuery.addListener(() {
      if (_searchQuery.text.isEmpty) {
      setState(() {
       _IsSearching = false;
       _searchText = "";
       });
       }
     else {
    setState(() {
      _IsSearching = true;
      _searchText = _searchQuery.text;
       });
      }
     });
     }
    @override
    void initState() {
    super.initState();
    _IsSearching = false;
    init();
    }
    void init() {
    _list = List();
    _list.add("building 1",);
    _list.add("building 2");
    _list.add("building 3");
    _list.add("building 4");
    _list.add("building 5");
    _list.add("building 6");
    _list.add("building 7");
    _list.add("building 8");
    _list.add("building 9");
    _list.add("building 10");
    }  
    @override
    Widget build(BuildContext context) {
    SizeConfig().init(context);
    return new Scaffold(
     key: key,
     appBar: buildBar(context),
     body: new GridView.count(
      crossAxisCount: 3,
      padding: EdgeInsets.fromLTRB(10,10,10,10),
      childAspectRatio: 8.0 / 9.0,
      children: _IsSearching ? _buildSearchList() : _buildList(),
       ),
       drawer: Navigationdrawer(),
       );
        }
        List<Uiitem> _buildList() {
        return _list.map((contact) => new Uiitem(contact)).toList();
        }
        List<Uiitem> _buildSearchList() {
        if (_searchText.isEmpty) {
        return _list.map((contact) => new Uiitem(contact))
        .toList();
         }
        else {
        List<String> _searchList = List();
         for (int i = 0; i < _list.length; i++) {
         String  name = _list.elementAt(i);
         if (name.toLowerCase().contains(_searchText.toLowerCase())) {
          _searchList.add(name);
          }
         }
        return _searchList.map((contact) => new Uiitem(contact))
        .toList();
        }
      }
      Widget buildBar(BuildContext context) {
       return new AppBar(
        centerTitle: true,
        title: appBarTitle,
        iconTheme: new IconThemeData(color:Colors.orange),
        backgroundColor: Colors.black,
        actions: <Widget>[
      new IconButton(icon: actionIcon, onPressed: () {
        setState(() {
          if (this.actionIcon.icon == Icons.search) {
            this.actionIcon = new Icon(Icons.close, color: Colors.orange,);
            this.appBarTitle = new TextField(
              controller: _searchQuery,
              style: new TextStyle(
                color: Colors.orange,
              ),
              decoration: new InputDecoration(
                  hintText: "Search here..",
                  hintStyle: new TextStyle(color: Colors.white)
              ),
            );
            _handleSearchStart();
          }
          else {
            _handleSearchEnd();
             }
            });
          },),
         ]
       );
      }
       void _handleSearchStart() {
       setState(() {
       _IsSearching = true;
        });
       }
       void _handleSearchEnd() {
       setState(() {
       this.actionIcon = new Icon(Icons.search, color: Colors.orange,);
       this.appBarTitle =
       new Text("My Properties", style: new TextStyle(color: Colors.white),);
       _IsSearching = false;
       _searchQuery.clear();
        });
       }
      }
     class Uiitem extends StatelessWidget{
     final String name;
     Uiitem(this.name);
     Widget build(BuildContext context){
     return new Card(
      margin: EdgeInsets.fromLTRB(5,5,5,7),
      elevation: 10.0,
      child: InkWell(
      splashColor: Colors.orange,
      onTap: (){
      },
     child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
      AspectRatio(
        aspectRatio: 18.0 / 11.0,
        child: Image.asset('assets/images/build.jpeg',fit: BoxFit.scaleDown,),
      ),
      Padding(
        padding: EdgeInsets.fromLTRB(10.0, 15.0, 0.0,0.0),
        child: Column(crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(this.name,style: new TextStyle(fontFamily: 'Raleway',fontWeight: 
       FontWeight.bold,fontSize: 14.0),
                maxLines: 1,
                ),
            SizedBox(height: 0.0),
            Text('Place',style: new TextStyle(fontFamily: 'Roboto'),),
          ],
           ),
          ),
        ],
       ),
       ),   
      );
      }
     }
    

    在此处输入图片说明

chunhunghan

您可以在下面的
步骤中粘贴粘贴运行完整的代码。步骤1:您可以定义Building具有属性类。id, name, place
步骤2:返回搜索列表而不返回List<Uiitem>
步骤3:使用GridView.builder并返回Uiitem(_searchList[index]);
步骤4:Uiitem接受,Building以便您InkWell可以获取id

工作演示

在此处输入图片说明

完整的代码

import 'package:flutter/material.dart';

class Building {
  String id;
  String name;
  String place;

  Building({this.id, this.name, this.place});
}

class SearchList extends StatefulWidget {
  SearchList({Key key}) : super(key: key);
  @override
  _SearchListState createState() => _SearchListState();
}

class _SearchListState extends State<SearchList> {
  Widget appBarTitle = Text(
    "My Properties",
    style: TextStyle(color: Colors.white),
  );
  Icon actionIcon = Icon(
    Icons.search,
    color: Colors.orange,
  );
  final key = GlobalKey<ScaffoldState>();
  final TextEditingController _searchQuery = TextEditingController();
  List<Building> _list;
  List<Building> _searchList = List();

  bool _IsSearching;
  String _searchText = "";
  _SearchListState() {
    _searchQuery.addListener(() {
      if (_searchQuery.text.isEmpty) {
        setState(() {
          _IsSearching = false;
          _searchText = "";
          _buildSearchList();
        });
      } else {
        setState(() {
          _IsSearching = true;
          _searchText = _searchQuery.text;
          _buildSearchList();
        });
      }
    });
  }
  @override
  void initState() {
    super.initState();
    _IsSearching = false;
    init();
  }

  void init() {
    _list = List();
    _list.add(
      Building(id:"1", name: "A 1", place: "google"),
    );
    _list.add(
      Building(id:"2", name: "A 2", place: "google"),
    );
    _list.add(
      Building(id:"3",name: "B 3", place: "facebook"),
    );
    _list.add(
      Building(id:"4",name: "B 4", place: "facebook"),
    );
    _list.add(
      Building(id:"5",name: "C 5", place: "flutter"),
    );
    _searchList = _list;
  }

  @override
  Widget build(BuildContext context) {
    //SizeConfig().init(context);
    return Scaffold(
        key: key,
        appBar: buildBar(context),
        body: GridView.builder(
            itemCount: _searchList.length,
            itemBuilder: (context, index) {
              return Uiitem(_searchList[index]);
            },
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
            )

            /* GridView.count(
        crossAxisCount: 3,
        padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
        childAspectRatio: 8.0 / 9.0,
        children: _IsSearching ? _buildSearchList() : _buildList(),
      ),*/
            //drawer: Navigationdrawer(),
            ));
  }

  List<Building> _buildList() {
    return _list; //_list.map((contact) =>  Uiitem(contact)).toList();
  }

  List<Building> _buildSearchList() {
    if (_searchText.isEmpty) {
      return _searchList =
          _list; //_list.map((contact) =>  Uiitem(contact)).toList();
    } else {
      /*for (int i = 0; i < _list.length; i++) {
        String name = _list.elementAt(i);
        if (name.toLowerCase().contains(_searchText.toLowerCase())) {
          _searchList.add(name);
        }
      }*/

      _searchList = _list
          .where((element) =>
              element.name.toLowerCase().contains(_searchText.toLowerCase()) ||
              element.place.toLowerCase().contains(_searchText.toLowerCase()))
          .toList();
      print('${_searchList.length}');
      return _searchList; //_searchList.map((contact) =>  Uiitem(contact)).toList();
    }
  }

  Widget buildBar(BuildContext context) {
    return AppBar(
        centerTitle: true,
        title: appBarTitle,
        iconTheme: IconThemeData(color: Colors.orange),
        backgroundColor: Colors.black,
        actions: <Widget>[
          IconButton(
            icon: actionIcon,
            onPressed: () {
              setState(() {
                if (this.actionIcon.icon == Icons.search) {
                  this.actionIcon = Icon(
                    Icons.close,
                    color: Colors.orange,
                  );
                  this.appBarTitle = TextField(
                    controller: _searchQuery,
                    style: TextStyle(
                      color: Colors.orange,
                    ),
                    decoration: InputDecoration(
                        hintText: "Search here..",
                        hintStyle: TextStyle(color: Colors.white)),
                  );
                  _handleSearchStart();
                } else {
                  _handleSearchEnd();
                }
              });
            },
          ),
        ]);
  }

  void _handleSearchStart() {
    setState(() {
      _IsSearching = true;
    });
  }

  void _handleSearchEnd() {
    setState(() {
      this.actionIcon = Icon(
        Icons.search,
        color: Colors.orange,
      );
      this.appBarTitle = Text(
        "My Properties",
        style: TextStyle(color: Colors.white),
      );
      _IsSearching = false;
      _searchQuery.clear();
    });
  }
}

class Uiitem extends StatelessWidget {
  final Building building;
  Uiitem(this.building);

  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.fromLTRB(5, 5, 5, 7),
      elevation: 10.0,
      child: InkWell(
        splashColor: Colors.orange,
        onTap: () {
          print(building.id);
        },
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            AspectRatio(
              aspectRatio: 18.0 / 11.0,
              child: Image.asset(
                'assets/images/build.jpeg',
                fit: BoxFit.scaleDown,
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(10.0, 15.0, 0.0, 0.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    this.building.name,
                    style: TextStyle(
                        fontFamily: 'Raleway',
                        fontWeight: FontWeight.bold,
                        fontSize: 14.0),
                    maxLines: 1,
                  ),
                  SizedBox(height: 0.0),
                  Text(
                    building.place,
                    style: TextStyle(fontFamily: 'Roboto'),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SearchList(),
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

返回页面时,带有过滤器搜索页面的Flutter不断从查询中添加相同的列表视图

来自分类Dev

带有搜索输入的jQuery Json过滤器,无需使用任何过滤器插件

来自分类Dev

在带有Cards的RecyclerView上添加搜索过滤器?

来自分类Dev

带有搜索过滤器的角度自动完成

来自分类Dev

带有替换和搜索功能的熊猫过滤器

来自分类Dev

带有多个过滤器的Elasticsearch搜索查询

来自分类Dev

网格视图外过滤器通配符

来自分类Dev

VBA - 应用带有部分标题匹配的过滤器

来自分类Dev

Android搜索视图过滤器

来自分类Dev

带有列表的Django过滤器过滤器

来自分类Dev

Google Play应用搜索过滤器

来自分类Dev

ElasticSearch-带有过滤器的自定义分析器-未应用过滤器

来自分类Dev

带有网格刷新的jqgrid多选过滤器问题

来自分类Dev

导出 CSV - 带有过滤器的 Kendo UI 网格

来自分类Dev

带有多个过滤器的ElasticSearch

来自分类Dev

带有过滤器的GetPivotData

来自分类Dev

带有过滤器的mdChips

来自分类Dev

带有lodash的过滤器数组

来自分类Dev

带有 manytomanyfield 的 Django 过滤器

来自分类Dev

网格视图索引随过滤器而变化

来自分类Dev

Android 搜索过滤器在没有结果时显示文本视图

来自分类Dev

带有过滤器的搜索栏,以及使用Ionic 2的JSON数据

来自分类Dev

PHP MYSQL HTML搜索带有多个过滤器/字段

来自分类Dev

带有过滤器的高级搜索-Angular.js

来自分类Dev

带有复选框和搜索框的引导表过滤器-javascript

来自分类Dev

带有布尔的弹性搜索过滤器查询返回无效结果

来自分类Dev

如何使用带有Google Maps Android API的过滤器搜索餐厅?

来自分类Dev

因为带有mybatis和spring的搜索过滤器会返回整个表

来自分类Dev

带有复选框PHP的实时搜索过滤器

Related 相关文章

  1. 1

    返回页面时,带有过滤器搜索页面的Flutter不断从查询中添加相同的列表视图

  2. 2

    带有搜索输入的jQuery Json过滤器,无需使用任何过滤器插件

  3. 3

    在带有Cards的RecyclerView上添加搜索过滤器?

  4. 4

    带有搜索过滤器的角度自动完成

  5. 5

    带有替换和搜索功能的熊猫过滤器

  6. 6

    带有多个过滤器的Elasticsearch搜索查询

  7. 7

    网格视图外过滤器通配符

  8. 8

    VBA - 应用带有部分标题匹配的过滤器

  9. 9

    Android搜索视图过滤器

  10. 10

    带有列表的Django过滤器过滤器

  11. 11

    Google Play应用搜索过滤器

  12. 12

    ElasticSearch-带有过滤器的自定义分析器-未应用过滤器

  13. 13

    带有网格刷新的jqgrid多选过滤器问题

  14. 14

    导出 CSV - 带有过滤器的 Kendo UI 网格

  15. 15

    带有多个过滤器的ElasticSearch

  16. 16

    带有过滤器的GetPivotData

  17. 17

    带有过滤器的mdChips

  18. 18

    带有lodash的过滤器数组

  19. 19

    带有 manytomanyfield 的 Django 过滤器

  20. 20

    网格视图索引随过滤器而变化

  21. 21

    Android 搜索过滤器在没有结果时显示文本视图

  22. 22

    带有过滤器的搜索栏,以及使用Ionic 2的JSON数据

  23. 23

    PHP MYSQL HTML搜索带有多个过滤器/字段

  24. 24

    带有过滤器的高级搜索-Angular.js

  25. 25

    带有复选框和搜索框的引导表过滤器-javascript

  26. 26

    带有布尔的弹性搜索过滤器查询返回无效结果

  27. 27

    如何使用带有Google Maps Android API的过滤器搜索餐厅?

  28. 28

    因为带有mybatis和spring的搜索过滤器会返回整个表

  29. 29

    带有复选框PHP的实时搜索过滤器

热门标签

归档