Created with <3 with dartpad.dev.
Last active
August 23, 2023 17:24
-
-
Save demolaf/d814213af4861a9250859b1bacd3fecb to your computer and use it in GitHub Desktop.
Sorting a list of items based on their date property
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
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
final String title; | |
const MyHomePage({ | |
Key? key, | |
required this.title, | |
}) : super(key: key); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(widget.title), | |
), | |
body: Column( | |
// TODO: you can feed your list view builder with dates from today, yesterday and beyond that will | |
// be loaded dynamically (not all at once but using infinite scrolling / pagination) | |
children: List.generate(5, (index) { | |
/// You feed your transactions to this function and it maps it to an object | |
/// that sections the transactions based on their date | |
final dateSection = mapTransactionsToDateWidget(DateTime.now(), listOfTransactions()); | |
return Column( | |
children: [ | |
Text(dateSection.date.toString()), | |
Column( | |
children: List.generate(dateSection.transactions.length, (index) => Text(dateSection.transactions[index].name),), | |
), | |
], | |
); | |
}), | |
), | |
); | |
} | |
SortedTransactions mapTransactionsToDateWidget(DateTime date, List<Transaction> transactions) { | |
// TODO: change isAtSameMomentAs to isSameDayAs not sure the exact method name this is just an example (you can use moment package) | |
return SortedTransactions(date: date, transactions: transactions.where((transaction) => transaction.date.isAtSameMomentAs(date)).toList(),); | |
} | |
/// This is your list of transactions | |
List<Transaction> listOfTransactions() { | |
return [ | |
Transaction(name: 'Test 1', date: DateTime.now()), | |
Transaction(name: 'Test 2', date: DateTime.now()), | |
Transaction(name: 'Test 3', date: DateTime.now()), | |
]; | |
} | |
} | |
class SortedTransactions { | |
final DateTime date; | |
final List<Transaction> transactions; | |
SortedTransactions({required this.date, required this.transactions}); | |
} | |
class Transaction { | |
final String name; | |
final DateTime date; | |
Transaction({required this.name, required this.date}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment