So I have an appbar widget which work fine:
typedef void BoolCallback(bool val);
class MyAppBar extends PreferredSize {
final int opacity;
final String title;
MyAppBar(this.opacity, [this.title]);
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
@override
Widget build(BuildContext context) {
return AppBar(
title: Text(title != null ? title : ''),
backgroundColor: Colors.white,
centerTitle: true,
leading: Container(
color: Color(0x00ffffff), child: IconButton(
icon: Icon(Icons.lightbulb_outline),
iconSize: 120,
onPressed: () {},
)),
actions: [
Switch(value: true, onChanged: (val){
callback(val);}),
],
flexibleSpace: Container(
decoration: myBoxDecoration(opacity)
),
);
}
}
which is called from:
bool _isOn = true;
(...)
Scaffold(
appBar: MyAppBar((val) => _isOn = val, 0xff, 'dummy title'),
body: _isOn ? Widget1 : Widget2
(...)
However due to recent development I would like to include a switch button on the far right of the appbar with a callback in order for the body to change based on the value of switch. How can I simply do this? Do I need to get rid off appbar and go for a custom container? Any help strongly appreciated!
Edit: following some help in the comment section (disappeared?) I used action to add the button and use a callback. However the main problem is that the switch button is stateful and I don't know how I can combine a stateful widget and PreferredSize...