Skip to content

Instantly share code, notes, and snippets.

@andantonyan
Created April 15, 2022 14:27
Show Gist options
  • Save andantonyan/5a3764a8bea2782153bd5927829e0f67 to your computer and use it in GitHub Desktop.
Save andantonyan/5a3764a8bea2782153bd5927829e0f67 to your computer and use it in GitHub Desktop.
Flutter mixin composition example
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: 'Mixin Composition',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with DialogMixin, LocationMixin {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Mixin Composition'),
),
floatingActionButton: FloatingActionButton(
onPressed: _checkLocation,
tooltip: 'Check Location',
child: const Icon(Icons.my_location_outlined),
),
);
}
void _checkLocation() {
check();
}
}
mixin DialogMixin {
void show() {
print('Show dialog');
}
}
mixin LocationMixin<T extends StatefulWidget> on State<T>, DialogMixin {
void check() {
show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment