Skip to content

Instantly share code, notes, and snippets.

@Luckey-Elijah
Created October 4, 2021 20:55
Show Gist options
  • Save Luckey-Elijah/9866c1a7e973ae638749a75923db48fd to your computer and use it in GitHub Desktop.
Save Luckey-Elijah/9866c1a7e973ae638749a75923db48fd to your computer and use it in GitHub Desktop.
Flutter mixin examples
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Mixin Demo',
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget
with MyRoutesMixin, MyApplicationStrings {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(homePage)),
body: Center(
child: TextButton(
child: Text(detailsPage.toUpperCase()),
onPressed: () => pushDetailsPage(context),
),
),
);
}
}
class DetailsPage extends StatelessWidget
with LoggerMixin, MyApplicationStrings {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(detailsPage)),
body: Center(
child: Column(
children: [
TextButton(
onPressed: Navigator.of(context).pop,
child: Text(goBack),
),
TextButton(
onPressed: () => logButtonPressed(example),
child: Text(example),
),
],
),
),
);
}
}
class Logger {
const Logger();
void log(String label, DateTime dateTime) {
print('PRESSED: $label at ${dateTime.hour}:${dateTime.minute}');
// TODO: complete the logger
}
}
mixin MyRoutesMixin on Widget {
Future<T?> pushDetailsPage<T extends Object?>(BuildContext context) =>
Navigator.push<T>(
context,
MaterialPageRoute(builder: (_) => DetailsPage()),
);
}
mixin LoggerMixin {
static const _logger = Logger();
void logButtonPressed(String label) => _logger.log(label, DateTime.now());
}
mixin MyApplicationStrings {
final example = 'EXAMPLE';
final goBack = 'GO BACK';
final detailsPage = 'Details Page';
final homePage = 'Home Page';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment