I created a Widget based on Marks answer above:
import 'package:flutter/material.dart';
class DisabledRenderer extends StatelessWidget {
final Widget child;
final bool enabled;
final double padding;
final double boxWidth;
final Color boxColor;
final double boxRadius;
DisabledRenderer(
{@required this.child,
@required this.enabled,
this.padding = 2,
this.boxWidth = 2,
this.boxColor = Colors.white,
this.boxRadius = 5.0});
@override
Widget build(BuildContext context) => Material(
elevation: 2.0,
child: Opacity(
opacity: enabled ? 1 : 0.3,
child: Container(
padding: EdgeInsets.all(padding),
child: child,
decoration: BoxDecoration(
border: Border.all(width: boxWidth, color: boxColor),
borderRadius: BorderRadius.all(Radius.circular(boxRadius)),
),
),
),
);
}
In use it looks like that:
DisabledRenderer(enabled: false, child: Icon(MdiIcons()['speedometer'],);
The enable bool is usually used with a StreamBuilder to listen to a model event.
How to do that please search for Flutter Bloc pattern and StreamBuilder
If you need a version which supports streams (bloc pattern) then use this one:
import 'package:flutter/material.dart';
class DisabledRenderer extends StatelessWidget {
final Widget child;
final bool enabled;
final Stream enableStream;
final double padding;
final bool showBorder;
final double boxWidth;
final Color boxColor;
final double boxRadius;
DisabledRenderer(
{@required this.child,
this.enabled,
this.enableStream,
this.showBorder = true,
this.padding = 2,
this.boxWidth = 2,
this.boxColor = Colors.white,
this.boxRadius = 5.0}) {
//one or the other, not both
assert((enableStream != null && enabled == null) ||
(enableStream == null && enabled != null));
}
@override
Widget build(BuildContext context) => Material(
elevation: 2.0,
child: enableStream != null
? StreamBuilder<bool>(
initialData: false,
stream: enableStream,
builder: (BuildContext context, AsyncSnapshot enabled) {
return enabled.hasData
? _renderEnabledDisabledState(enabled.data)
: Container();
},
)
: _renderEnabledDisabledState(enabled),
);
Widget _renderEnabledDisabledState(bool enabled) {
return IgnorePointer(
ignoring: !enabled,
child: Opacity(
opacity: enabled ? 1 : 0.3,
child: showBorder
? Container(
padding: EdgeInsets.all(padding),
child: child,
decoration: BoxDecoration(
border: Border.all(width: boxWidth, color: boxColor),
borderRadius: BorderRadius.all(Radius.circular(boxRadius)),
),
)
: child,
),
);
}
}
this is how you would use it:
DisabledRenderer(
showBorder: false,
enableStream: BlocProvider.of(context)
.bluetooth
.isConnected
.stream,
child: YourWidget(),
```