Skip to content

Instantly share code, notes, and snippets.

@HansMuller
Last active April 16, 2022 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HansMuller/768d3056137eb323256913aea66e890a to your computer and use it in GitHub Desktop.
Save HansMuller/768d3056137eb323256913aea66e890a to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class ToggleButton extends TextButton {
ToggleButton({
super.key,
required super.onPressed,
super.onLongPress,
super.onHover,
super.onFocusChange,
super.style,
super.focusNode,
super.autofocus = false,
super.clipBehavior = Clip.none,
required this.selected,
required Widget super.child,
});
final bool selected;
@override
ButtonStyleButtonState<ToggleButton> createState() => _ToggleButtonState();
}
class _ToggleButtonState extends ButtonStyleButtonState<ToggleButton> {
@override
Set<MaterialState> get materialStates {
return <MaterialState>{
if (widget.selected) MaterialState.selected,
...super.materialStates
};
}
}
class Home extends StatefulWidget {
const Home({ super.key });
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
bool selected = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ToggleButton(
selected: selected,
onPressed: () {
setState(() { selected = !selected; });
},
child: Text('toggle'),
),
),
);
}
}
void main() {
runApp(MaterialApp(home: Home()));
}
/*
diff --git a/packages/flutter/lib/src/material/button_style_button.dart b/packages/flutter/lib/src/material/button_style_button.dart
index ab48cf499e..0013a7c8fd 100644
--- a/packages/flutter/lib/src/material/button_style_button.dart
+++ b/packages/flutter/lib/src/material/button_style_button.dart
@@ -138,7 +138,7 @@ abstract class ButtonStyleButton extends StatefulWidget {
bool get enabled => onPressed != null || onLongPress != null;
@override
- State<ButtonStyleButton> createState() => _ButtonStyleState();
+ State<ButtonStyleButton> createState() => ButtonStyleButtonState<ButtonStyleButton>();
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
@@ -191,7 +191,7 @@ abstract class ButtonStyleButton extends StatefulWidget {
/// * [TextButton], a simple button without a shadow.
/// * [ElevatedButton], a filled button whose material elevates when pressed.
/// * [OutlinedButton], similar to [TextButton], but with an outline.
-class _ButtonStyleState extends State<ButtonStyleButton> with MaterialStateMixin, TickerProviderStateMixin {
+class ButtonStyleButtonState<T extends ButtonStyleButton> extends State<T> with MaterialStateMixin, TickerProviderStateMixin {
AnimationController? _controller;
double? _elevation;
Color? _backgroundColor;
@@ -209,7 +209,7 @@ class _ButtonStyleState extends State<ButtonStyleButton> with MaterialStateMixin
}
@override
- void didUpdateWidget(ButtonStyleButton oldWidget) {
+ void didUpdateWidget(T oldWidget) {
super.didUpdateWidget(oldWidget);
setMaterialState(MaterialState.disabled, !widget.enabled);
// If the button is disabled while a press gesture is currently ongoing,
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment