Skip to content

Instantly share code, notes, and snippets.

@singlesoup
Last active September 22, 2022 14:19
Show Gist options
  • Save singlesoup/aea34c14dcff80c6ef1a1f9b770aed28 to your computer and use it in GitHub Desktop.
Save singlesoup/aea34c14dcff80c6ef1a1f9b770aed28 to your computer and use it in GitHub Desktop.
Mixins - 1
import 'package:flutter/material.dart';
import 'dart:developer' as devtools show log;
extension Log on Object {
void log() => devtools.log(toString());
}
abstract class Animal{
const Animal();
}
mixin CanRun on Animal {
int get speed;
void run() {
"Running at a speed of: $speed".log();
}
}
class Cat extends Animal with CanRun {
int speed = 10;
@override
run() {
super.run();
"Cat func is called".log();
}
}
// By default it extends Type "Object", that why the error
class Dog with CanRun {
@override
int get speed => throw UnimplementedError();
}
void testIt() {
final cat = Cat();
cat.run();
cat.speed = 20;
cat.run();
}
void main() {
'Hello'.log();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
testIt();
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: Colors.amberAccent,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(
'Hello, World!',
style:
Theme.of(context).textTheme.headline5!.copyWith(color: Colors.black),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment