Skip to content

Instantly share code, notes, and snippets.

@romanejaquez
Created March 2, 2022 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanejaquez/c69b56b4137f212554ffa6e5e575ede0 to your computer and use it in GitHub Desktop.
Save romanejaquez/c69b56b4137f212554ffa6e5e575ede0 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:intl/intl.dart' show toBeginningOfSentenceCase;
import 'package:google_fonts/google_fonts.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: const FirebaseOptions(
apiKey: "AIzaSyDOdBmVvI30Jg49oup4gsJMZMjTPhHOloQ",
authDomain: "fullstack-labs.firebaseapp.com",
databaseURL: "https://fullstack-labs.firebaseio.com",
projectId: "fullstack-labs",
storageBucket: "fullstack-labs.appspot.com",
messagingSenderId: "251418261584",
appId: "1:251418261584:web:2e01357a093d27476b7955"
)
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget()
),
),
);
}
}
List<Container> options = [
Container(
padding: EdgeInsets.all(15),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
hintText: 'Option 1',
),
),
),
];
class MyWidget extends StatefulWidget {
@override
MyWidgetState createState() => MyWidgetState();
}
class Meal {
final String? id;
final String? title;
final String? imgUrl;
final List<MealType>? mealTypeFilter;
Meal({ this.id, this.title, this.imgUrl, this.mealTypeFilter });
}
enum MealType {
none,
isLowCalorie,
isVegan,
isBreakfast,
isDinner,
isLunch
}
class MyWidgetState extends State<MyWidget> {
List<Meal> meals = [
Meal(
id: '1001',
title: 'Pancakes',
imgUrl: '',
mealTypeFilter: [
MealType.isBreakfast
]
),
Meal(
id: '1002',
title: 'Chicken Wings',
imgUrl: '',
mealTypeFilter: [
MealType.isLunch
]
),
Meal(
id: '1003',
title: 'Yogurt',
imgUrl: '',
mealTypeFilter: [
MealType.isLowCalorie,
MealType.isBreakfast,
MealType.isVegan
]
)
];
Set<MealType> appliedFilters = {};
List<Meal> filteredList = [];
List<Meal> getFilteredList() {
if (appliedFilters.isEmpty) {
return meals;
}
return meals.where((m) => m.mealTypeFilter!.any((f) => appliedFilters.contains(f))).toList();
}
@override
Widget build(BuildContext context) {
filteredList = getFilteredList();
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: List.generate(
MealType.values.length, (index) {
return TextButton(
onPressed: () {
setState(() {
if (MealType.values[index] == MealType.none) {
appliedFilters.clear();
}
else {
appliedFilters.add(MealType.values[index]);
}
});
},
child: Text(MealType.values[index].name)
);
}
)
),
Expanded(
child: ListView.builder(
itemCount: filteredList.length,
itemBuilder: (context, index) {
var meal = filteredList[index];
return Text(meal.title!);
}
)
)
]
)
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment