Created
September 18, 2024 08:13
-
-
Save gasaichandesu/d211bd3ff72e4af5b64234f64e17a573 to your computer and use it in GitHub Desktop.
This file contains 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(MaterialApp( | |
home: TransactionPage(), | |
)); | |
} | |
class TransactionPage extends StatefulWidget { | |
@override | |
_TransactionPageState createState() => _TransactionPageState(); | |
} | |
class _TransactionPageState extends State<TransactionPage> { | |
final ScrollController _scrollController = ScrollController(); | |
int _highlightedMonthIndex = 0; | |
final List<GlobalKey> _sectionKeys = []; | |
// Mock data for months and transactions | |
final List<String> _months = [ | |
'January', | |
'February', | |
'March', | |
'April', | |
'May', | |
'June', | |
'July', | |
'August', | |
'September', | |
'October', | |
'November', | |
'December' | |
]; | |
final Map<int, List<String>> _transactionsByMonth = { | |
0: ['Transaction A', 'Transaction B'], // January | |
1: ['Transaction C', 'Transaction D'], // February | |
2: ['Transaction E', 'Transaction F', 'Transaction G'], // March | |
3: ['Transaction H', 'Transaction I'], // April | |
4: ['Transaction J', 'Transaction K', 'Transaction L', 'Transaction M'], // May | |
5: ['Transaction N'], // June | |
6: ['Transaction O', 'Transaction P'], // July | |
7: ['Transaction Q', 'Transaction R'], // August | |
8: ['Transaction S'], // September | |
9: ['Transaction T', 'Transaction U'], // October | |
10: ['Transaction V'], // November | |
11: ['Transaction W', 'Transaction X'], // December | |
}; | |
@override | |
void initState() { | |
super.initState(); | |
_scrollController.addListener(_onScroll); | |
_sectionKeys.addAll(List.generate(_transactionsByMonth.length, (_) => GlobalKey())); | |
} | |
@override | |
void dispose() { | |
_scrollController.dispose(); | |
super.dispose(); | |
} | |
void _onScroll() { | |
for (int i = 0; i < _sectionKeys.length; i++) { | |
final context = _sectionKeys[i].currentContext; | |
if (context != null) { | |
final box = context.findRenderObject() as RenderBox; | |
final position = box.localToGlobal(Offset.zero); | |
// Check if this section is currently visible on screen | |
if (position.dy + box.size.height > 150) { | |
if (_highlightedMonthIndex != i) { | |
setState(() { | |
_highlightedMonthIndex = i; | |
}); | |
} | |
break; | |
} | |
} | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Transactions'), | |
), | |
body: Column( | |
children: [ | |
// Horizontal list of months | |
Container( | |
height: 60, | |
child: ListView.builder( | |
scrollDirection: Axis.horizontal, | |
itemCount: _months.length, | |
itemBuilder: (context, index) { | |
return Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text( | |
_months[index], | |
style: TextStyle( | |
fontSize: 18, | |
fontWeight: FontWeight.bold, | |
color: _highlightedMonthIndex == index | |
? Colors.blue // Highlight current month | |
: Colors.black, | |
), | |
), | |
); | |
}, | |
), | |
), | |
Expanded( | |
child: ListView.builder( | |
controller: _scrollController, | |
itemCount: _transactionsByMonth.length, | |
itemBuilder: (context, index) { | |
return Column( | |
key: _sectionKeys[index], // Assigning GlobalKey to each section | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text( | |
_months[index], | |
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), | |
), | |
), | |
..._transactionsByMonth[index]! | |
.map((transaction) => ListTile(title: Text(transaction))) | |
.toList(), | |
], | |
); | |
}, | |
), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment