Last active
May 2, 2022 13:32
-
-
Save pomarec/f15fce320604162223de1ce21b5772c6 to your computer and use it in GitHub Desktop.
Example de filtres pour Jonathan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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