Created with <3 with dartpad.dev.
Created
August 23, 2023 16:15
-
-
Save demolaf/4ecc15098187e86bbb7a35288b384ea1 to your computer and use it in GitHub Desktop.
Sorting items in a list based on their dates
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> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(widget.title), | |
), | |
body: Column( | |
children: List.generate(dateTimes().length, (index) { | |
final dateSection = mapTransactionsToDateSection(dateTimes()[index], listOfTransactions()); | |
return Column(children: [ | |
Text(dateSection.date.toString()), | |
Column( | |
children: List.generate(dateSection.transactions.length, (index) => Column( | |
children: [ | |
Text(dateSection.transactions[index].name), | |
],)) | |
), | |
]); | |
}) | |
) | |
); | |
} | |
List<DateTime> dateTimes() { | |
return [ | |
DateTime.now(), | |
DateTime.now().add(const Duration(seconds: 30)), | |
DateTime.now().add(const Duration(seconds: 100)), | |
]; | |
} | |
/// map today, yesterday and previous days to DateSection | |
DateSection mapTransactionsToDateSection(DateTime date, List<Transaction> transactions) { | |
return DateSection(date: date, transactions: transactions.where((transaction) => transaction.date.isAtSameMomentAs(date)).toList()); | |
} | |
/// Your list of transactions | |
List<Transaction> listOfTransactions() { | |
return [ | |
Transaction(name: "Test 1", date: DateTime.now()), | |
Transaction(name: "Test 2", date: DateTime.now().add(const Duration(seconds: 30))), | |
Transaction(name: "Test 3", date: DateTime.now()), | |
Transaction(name: "Test 4", date: DateTime.now().add(const Duration(seconds: 30))), | |
Transaction(name: "Test 5", date: DateTime.now().add(const Duration(seconds: 100))), | |
]; | |
} | |
} | |
class DateSection { | |
final DateTime date; | |
final List<Transaction> transactions; | |
DateSection({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