Skip to content

Instantly share code, notes, and snippets.

@pomarec
Last active May 2, 2022 13:32
Show Gist options
  • Save pomarec/f15fce320604162223de1ce21b5772c6 to your computer and use it in GitHub Desktop.
Save pomarec/f15fce320604162223de1ce21b5772c6 to your computer and use it in GitHub Desktop.
Example de filtres pour Jonathan
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class Filter {
String name;
bool Function(String) condition;
Filter(this.name, this.condition);
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> challenges = ["jo", "po", "clem", "chri"];
List<Filter> filters = [
Filter(
"Commence par p",
(challenge) => challenge[0] == "p",
),
Filter(
"Puis se suit par o",
(challenge) => challenge[1] == "w",
)
];
List<String> filteredChallenges = [];
void computeFilteredChallenges() {
filteredChallenges = challenges
.where((userName) => filters.every(
(filter) => filter.condition(userName),
))
.toList();
}
@override
Widget build(BuildContext context) => Scaffold(
body: ListView.builder(
itemCount: filteredChallenges.length,
itemBuilder: (context, i) => Text(
" " + filteredChallenges[i],
style: const TextStyle(fontSize: 30),
),
),
);
void validateModal() {
filters = modal.newFilters;
computeFilteredChallenges();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment