Skip to content

Instantly share code, notes, and snippets.

@yungwarlock
Last active September 4, 2023 13:26
Show Gist options
  • Save yungwarlock/48536f2ed3ce22d4c3d663a2996e486c to your computer and use it in GitHub Desktop.
Save yungwarlock/48536f2ed3ce22d4c3d663a2996e486c to your computer and use it in GitHub Desktop.
SchedulerBinding

Flutter scheduling an event to be run after BuildContext

Created with <3 with dartpad.dev.

import 'package:flutter/material.dart';
import "package:flutter/scheduler.dart";
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
home: Scaffold(
body: Center(
child: MyWidget(),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.star),
onPressed: () {
// This schedules an action to run at a later time
SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
print("Hello");
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Clicked"),
),
);
});
},
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(
'Hello, World!',
style: Theme.of(context).textTheme.headlineMedium,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment