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

标签 dart flutter

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

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

我只需要提示一下要寻找和学习什么就可以实现它。我对应用程序的实际做法可以吗?

非常感谢您的任何帮助。
地亚哥

主镖

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

最佳答案

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

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

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

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

这只是一个示例,可能需要空处理和更聪明的where子句。

希望这对您有所帮助。

关于dart - Flutter:如何在列表中添加几个过滤器并显示新结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54389190/

相关文章:

javascript - AngularJS 2 - 使用哪种语言? ( typescript ,Javascript, Dart )

android - flutter upi 事务不起作用(错误)

android - 升级到最新的 SDK 版本 1.20.4 后运行/调试 Flutter 应用程序非常慢

dart - 如何同步滚动两个 SingleChildScrollView 小部件

dart - Flutter 在 initState 方法中获取上下文

flutter - Flutter在GridView下方添加容器

dart - 如何使用 PaginatedDataTable 正确实现列、字段和行上的单击事件

dart - 角 Dart : ng-repeat filter update

android - 为什么设备管理器打不开?

Flutter Icons 小部件在 vscode 中没有预览