Skip to content

Instantly share code, notes, and snippets.

@ShinjiKobayashi
Created April 30, 2021 05:50
Show Gist options
  • Save ShinjiKobayashi/bef1172198e0e0eba26ea36b5233d9b0 to your computer and use it in GitHub Desktop.
Save ShinjiKobayashi/bef1172198e0e0eba26ea36b5233d9b0 to your computer and use it in GitHub Desktop.
dartでのinstance評価と演算子オーバーロード
import 'package:flutter/material.dart';
final 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),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class TestObject {
final int id;
final String value;
TestObject(this.id, this.value);
}
class TestObject2 {
final int id;
final String value;
TestObject2(this.id, this.value);
@override
bool operator==(other) {
if(other is! TestObject2) {
return false;
}
return id == (other as TestObject2).id;
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(valueChecker(), style: Theme.of(context).textTheme.headline4);
}
String valueChecker() {
// final a = TestObject(1, 'a');
// final b = TestObject(1, 'a');
final a = TestObject2(1, 'a');
final b = TestObject2(1, 'b');
if (a == b) {
return "same";
} else {
return "different";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment