Skip to content

Instantly share code, notes, and snippets.

@coopernurse
Last active April 8, 2022 17:57
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 coopernurse/2d17e31ee319119bc27074bcbe1489d7 to your computer and use it in GitHub Desktop.
Save coopernurse/2d17e31ee319119bc27074bcbe1489d7 to your computer and use it in GitHub Desktop.
dart hello world
import 'package:flutter/material.dart';
import 'dart:math';
import 'dart:async';
void main() {
runApp(CalendarApp());
}
class CalendarApp extends StatelessWidget {
Widget build(BuildContext context) {
const todos = [
"Clean gutters",
"Grocery shopping",
"Grocery shopping Grocery shopping Grocery shopping Grocery shopping Grocery shopping Grocery shopping Grocery shopping",
"Oil change",
"Clip cats claws",
"Go to the gym",
];
return MaterialApp(
home: Scaffold(
body: Column(
children: <Widget>[
TodoRotator(Duration(seconds: 3), 2, todos),
],
),
),
);
}
}
class TodoRotator extends StatefulWidget {
final Duration rotateInterval;
final int perPage;
final List<String> allItems;
TodoRotator(this.rotateInterval, this.perPage, this.allItems);
@override
_TodoRotatorState createState() => _TodoRotatorState(rotateInterval, perPage, allItems, 0);
}
class _TodoRotatorState extends State<TodoRotator> {
final Duration rotateInterval;
final int perPage;
final List<String> allItems;
int startOffset;
late Timer timer;
_TodoRotatorState(this.rotateInterval, this.perPage, this.allItems, this.startOffset);
void nextPage() {
setState(() {
startOffset += perPage;
if (startOffset >= allItems.length) {
startOffset = 0;
}
});
}
@override
void initState() {
super.initState();
timer = Timer.periodic(rotateInterval, (Timer t) => nextPage());
}
@override
Widget build(BuildContext context) {
final endOffset = min(startOffset + perPage, allItems.length);
return TodoWidget(allItems.sublist(startOffset, endOffset));
}
}
class TodoWidget extends StatelessWidget {
final List<String> todos;
TodoWidget(this.todos);
Widget build(BuildContext context) {
List<Widget> children = [
Text("TODO",
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold))
];
for (var i = 0; i < todos.length; i++) {
children.add(
Text("${i + 1}) ${todos[i]}", style: const TextStyle(fontSize: 15)));
}
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment