Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abhaysood/7877e39c9da3209deb3ddf0a9056aedf to your computer and use it in GitHub Desktop.
Save abhaysood/7877e39c9da3209deb3ddf0a9056aedf to your computer and use it in GitHub Desktop.
Blog: Enrich the Flutter Inspector with Diagnosticable properties
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late MyColorScheme _myColorScheme;
int _counter = 0;
@override
void initState() {
_myColorScheme = MyColorScheme(Colors.black, Colors.white);
super.initState();
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(IntProperty('_counter', _counter));
properties.add(
DiagnosticsProperty<MyColorScheme>('myColorScheme', _myColorScheme));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class MyColorScheme with Diagnosticable {
final Color textColor;
final Color backgroundColor;
MyColorScheme(this.textColor, this.backgroundColor);
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(ColorProperty('textColor', textColor));
properties.add(ColorProperty('backgroundColor', backgroundColor));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment