Last active
August 25, 2022 20:19
-
-
Save Lootwig/6efd91ab05c48f648630ffa8fe9930a7 to your computer and use it in GitHub Desktop.
septemberAppBar
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(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: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
static const _tileHeight = 40.0; | |
static final approximateDaysPerMonth = Iterable<int>.generate(30); | |
static const _months = ["August", "September"]; | |
final controller = ScrollController(); | |
String _title = "August"; | |
@override | |
void initState() { | |
super | |
.initState(); // did you know? initState's doc says to call super.initState FIRST in implementers | |
controller.addListener(_onScroll); | |
} | |
@override | |
void dispose() { | |
controller.dispose(); | |
super.dispose(); | |
} | |
void _onScroll() { | |
final itemIndex = controller.offset ~/ _tileHeight; | |
setState(() => _title = _months[itemIndex ~/ 30]); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text(_title)), | |
body: ListView.builder( | |
itemCount: _months.length * approximateDaysPerMonth.length, | |
controller: controller, | |
itemBuilder: (_, index) { | |
final month = _months[index ~/ approximateDaysPerMonth.length]; | |
final day = index % 30 + 1; | |
return SizedBox( | |
height: _tileHeight, | |
child: ListTile(title: Text('$month $day')), | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment