Skip to content

Instantly share code, notes, and snippets.

@jef-sure
Last active February 16, 2024 13:00
Show Gist options
  • Save jef-sure/a421ca3aef4e27106d56088ac68800b5 to your computer and use it in GitHub Desktop.
Save jef-sure/a421ca3aef4e27106d56088ac68800b5 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
final class SwitchState {
static const on = 1;
static const enabled = 2;
final int state;
const SwitchState(this.state);
bool isEnabled() {
return 0 != state & SwitchState.enabled;
}
bool isOn() {
return 0 != state & SwitchState.on;
}
bool isOff() {
return !isOn();
}
bool isDisabled() {
return !isEnabled();
}
SwitchState setOnOff(bool value) {
if (value) return setOn();
return setOff();
}
SwitchState setOn() {
return SwitchState(state | SwitchState.on);
}
SwitchState setEnabled() {
return SwitchState(state | SwitchState.enabled);
}
SwitchState setOff() {
return SwitchState(state & ~SwitchState.on);
}
SwitchState setDisabled() {
return SwitchState(state & ~SwitchState.enabled);
}
SwitchState.onEnabled() : state = SwitchState.on | SwitchState.enabled;
SwitchState.onDisabled() : state = SwitchState.on;
SwitchState.offEnabled() : state = SwitchState.enabled;
SwitchState.offDisabled() : state = 0;
}
class SwitchEvent {
SwitchState state;
bool isPrimary;
SwitchEvent({required this.state, this.isPrimary = false});
}
class TwoSwitches {
SwitchState primary;
SwitchState secondary;
TwoSwitches(this.primary, this.secondary);
}
class SwitchesBloc extends Bloc<SwitchEvent, TwoSwitches> {
SwitchesBloc()
: super(TwoSwitches(SwitchState.onEnabled(), SwitchState.onEnabled())) {
on<SwitchEvent>(_onSwitchEvent);
}
Future<void> _onSwitchEvent(SwitchEvent e, Emitter<TwoSwitches> emit) async {
if (e.state.isOff() && e.isPrimary) {
emit(TwoSwitches(state.primary.setOff(), state.secondary.setDisabled()));
} else if (e.state.isOn() && e.isPrimary) {
emit(TwoSwitches(state.primary.setOn(), state.secondary.setEnabled()));
} else if (e.state.isOff()) {
emit(TwoSwitches(state.primary, state.secondary.setOff()));
} else if (e.state.isOn()) {
emit(TwoSwitches(state.primary, state.secondary.setOn()));
}
}
}
void main() {
runApp(MultiBlocProvider(
//
providers: [
BlocProvider(create: (BuildContext context) => SwitchesBloc()) //
], //
child: MaterialApp(home: MyApp()) //
));
}
class MyApp extends StatelessWidget {
MyApp({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<SwitchesBloc, TwoSwitches>(builder: (context, state) {
var swBloc = BlocProvider.of<SwitchesBloc>(context);
return Scaffold(
appBar: AppBar(
leading: const IconButton(
icon: Icon(Icons.menu),
tooltip: 'Navigation menu',
onPressed: null,
),
title: const Text('Example title'),
actions: const [
IconButton(
icon: Icon(Icons.search),
tooltip: 'Search',
onPressed: null,
),
],
),
// body is the majority of the screen.
body:
Row(crossAxisAlignment: CrossAxisAlignment.center, children: [
Column(mainAxisAlignment: MainAxisAlignment.start, children: [
Switch(
activeColor: Colors.red,
value: swBloc.state.primary.isOn(),
onChanged: (bool value) {
swBloc.add(SwitchEvent(
state: swBloc.state.primary.setOnOff(value),
isPrimary: true));
},
),
Switch(
activeColor: Colors.red,
value: swBloc.state.secondary.isOn(),
onChanged: swBloc.state.secondary.isDisabled()? null : (bool value) {
swBloc.add(SwitchEvent(
state: swBloc.state.secondary.setOnOff(value),
isPrimary: false));
},
)
])
]));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment