Flutter:如何在列表中添加几个过滤器并显示新结果

时间:2019-01-27 14:28:02

标签: dart flutter

我开始学习Flutter,尝试制作我的第一个应用程序。我没有开发人员的背景,所以我尝试通过实践来学习一切。 我的应用程序正在从json文件接收一些用户数据(名称,姓氏,国家/地区,...),并显示用户名的完整列表,然后通过点击名称打开第二页,您可以在其中获取所有详细信息。 我现在想做的是添加一个“设置页面”,用户可以在其中使用两个下拉框(国家和/或级别)进行过滤。

如果未选择任何下拉框,则第一页应显示每个国家和各个级别的人员的整个列表(如现在这样),否则应过滤列表以仅显示所选国家和地区的人员。仅针对所选级别。

我只需要暗示一些寻找和学习的知识即可实现。我对应用程序的实际做法可以吗?

非常感谢您提供任何帮助。 迭戈

main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';

//import pages
import './contactdetails.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'USDDN EU Judges',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'USDDN EU Judges'),
    );
  }
}

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

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

class _MyHomePageState extends State<MyHomePage> {
  Future<List<User>> _getUser() async {
    var data = await http.get(
        "https://www.disconnecteddog.com/home/json/usddneujudgesdatabase.json");
    var jsonData = json.decode(data.body);

    List<User> users = [];

    for (var u in jsonData) {
      User user = User(
          u["Index"],
          u["Name"],
          u["Country"],
          u["Level"],
          u["Inthesportsince"],
          u["Active"],
          u["English"],
          u["Email"],
          u["Picture"]);
      users.add(user);
    }

    print(users.length);
    return users;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          //
          IconButton(icon: new Icon(Icons.filter_list, color: Colors.white,), onPressed: null)
        ],
      ),
      body: Container(
        child: FutureBuilder(
          future: _getUser(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.data == null) {
              return Container(
                  child: Center(child: Text("Loading judges database...")));
            } else {
              return ListView.builder(
                itemCount: snapshot.data.length,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    leading: CircleAvatar(
                      backgroundImage:
                          NetworkImage(snapshot.data[index].picture),
                    ),
                    title: Text(snapshot.data[index].name),
                    subtitle: Row(
                      children: <Widget>[
                        Text("Level: "),
                        Text(snapshot.data[index].level),
                      ],
                    ),
                    onTap: () {
                      Navigator.push(
                          context,
                          new MaterialPageRoute(
                              builder: (context) =>
                                  DetailPage(snapshot.data[index])));
                    },
                  );
                },
              );
            }
          },
        ),
      ),
    );
  }
}

class User {
  final int index;
  final String name;
  final String country;
  final String level;
  final String inthesportsince;
  final String active;
  final String english;
  final String email;
  final String picture;

  User(this.index, this.name, this.country, this.level, this.inthesportsince,
      this.active, this.english, this.email, this.picture);
}

Contactdetails.dart

import 'package:flutter/material.dart';
import 'package:usddn_judges/main.dart';

class DetailPage extends StatelessWidget {
  final User user;

  DetailPage(this.user);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(user.name),
      ),
      body: Container(
        //height: 120.0,
        child: Padding(
          padding: const EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 5.0),
          child: Card(
            margin: EdgeInsets.all(10.0),
            elevation: 2.0,
            child: new Column(
              children: <Widget>[
                new ListTile(
                  leading: new Icon(
                    Icons.account_box,
                    color: Colors.blue,
                    size: 26.0,
                  ),
                  title: new Text(
                    user.name,
                    style: new TextStyle(fontWeight: FontWeight.w400),
                  ),
                ),
                new Divider(color: Colors.blue),
                new ListTile(
                  leading: new Icon(
                    Icons.map,
                    color: Colors.blue,
                    size: 26.0,
                  ),
                  title: Row(
                    children: <Widget>[
                      new Text("Country:  "),
                      new Text(
                        user.country,
                        style: new TextStyle(fontWeight: FontWeight.w400),
                      ),
                    ],
                  ),
                ),
                new Divider(color: Colors.blue),
                new ListTile(
                  leading: new Icon(
                    Icons.multiline_chart,
                    color: Colors.blue,
                    size: 26.0,
                  ),
                  title: Row(
                    children: <Widget>[
                      new Text("Level:  "),
                      new Text(
                        user.level,
                        style: new TextStyle(fontWeight: FontWeight.w400),
                      ),
                    ],
                  ),
                ),
                new Divider(color: Colors.blue),
                new ListTile(
                  leading: new Icon(
                    Icons.language,
                    color: Colors.blue,
                    size: 26.0,
                  ),
                  title: Row(
                    children: <Widget>[
                      new Text("English:  "),
                      new Text(
                        user.english,
                        style: new TextStyle(fontWeight: FontWeight.w400),
                      ),
                    ],
                  ),
                ),
                new Divider(color: Colors.blue),
                new ListTile(
                  leading: new Icon(
                    Icons.flash_on,
                    color: Colors.blue,
                    size: 26.0,
                  ),
                  title: Row(
                    children: <Widget>[
                      new Text("Active:  "),
                      new Text(
                        user.active,
                        style: new TextStyle(fontWeight: FontWeight.w400),
                      ),
                    ],
                  ),
                ),
                new Divider(color: Colors.blue),
                new ListTile(
                  leading: new Icon(
                    Icons.event,
                    color: Colors.blue,
                    size: 26.0,
                  ),
                  title: Row(
                    children: <Widget>[
                      new Text("In the sport since:  "),
                      new Text(
                        user.inthesportsince,
                        style: new TextStyle(fontWeight: FontWeight.w400),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Main Contact List

Details Page

2 个答案:

答案 0 :(得分:0)

我认为您应该研究List.where()。

https://api.dartlang.org/stable/2.1.0/dart-core/Iterable/where.html

这样,您可以根据过滤器中的值过滤用户。

users.where((user) => user.country == selectedCountry);

这只是一个示例,可以使用null处理和更聪明的where子句。

我希望这会帮助您入门。

答案 1 :(得分:0)

为过滤器创建一个新屏幕,将其命名为FilterScreen。然后,您可以使用任何状态管理框架(提供商,BloC等)来存储用户在FilterScreen中输入的过滤器。返回搜索屏幕后,如果输入了任何过滤器,请重新查询列表。