I'm trying to implement a widget swap for a ToggleSwitch in flutter. So far I have worked around to change the swap of widgets and toggleSwitch separately. I need to combine them so when I select a toggle switch it will show the widget it need to show and when the other toggle switch clicked the second widget come into view. Even when I work that the widgets are swapping, the ToggleSwitch does not change it's state. Please help me in improving this. Many Thanks in advance.
here is the build() code snippet I'm trying to work out.
Widget build(BuildContext context) {
final petHeader = Container(
alignment: Alignment.center,
color: Colors.white,
width: MediaQuery.of(context).size.width,
child: Row(
children: [
Expanded(
flex: 2,
child: Hero(
tag: 'hero',
child: Padding(
padding: EdgeInsets.all(4.0),
child: CircleAvatar(
radius: 30.0,
backgroundColor: Colors.grey[300],
child: Image(
image: AssetImage(
'assets/images/logo_small.png',
),
),
),
),
),
),
Expanded(
flex: 3,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Dog Name',
style: TextStyle(fontSize: 20.0, color: Colors.black87),
),
Text(
'Age',
style: TextStyle(fontSize: 12.0, color: Colors.black87),
),
],
),
),
),
Expanded(
flex: 5,
child: Container(
margin: EdgeInsets.symmetric(
horizontal: 12.0,
),
child: Text(
'Daily Guide',
style: TextStyle(fontSize: 32.0, color: Colors.black87),
textAlign: TextAlign.end,
),
),
),
],
),
);
final macroCharts = Center(
child: Text('macro chart here'),
);
final microCharts = Center(
child: Text('micro chart here'),
);
Widget getMacroChart(){
return Center(
child: Text('macro chart here'),
);
}
Widget getMicroChart(){
return Center(
child: Text('micro chart here'),
);
}
Widget getCustomContainer() {
switch (selectedWidget) {
case widgetMarker.macro:
return getMacroChart();
case widgetMarker.micro:
return getMicroChart();
}
return getMacroChart();
}
final userSaveBtn = Container(
margin: EdgeInsets.only(top: 2.0, bottom: 4.0),
alignment: Alignment.center,
// width: MediaQuery.of(context).size.width,
child: ToggleSwitch(
cornerRadius: 4.0,
minWidth: MediaQuery.of(context).size.width,
minHeight: MediaQuery.of(context).size.height,
fontSize: 20.0,
initialLabelIndex: 0,
activeBgColor: Color(0xFF03B898),
activeFgColor: Colors.white,
inactiveBgColor: Colors.grey[300],
inactiveFgColor: Colors.black54,
labels: [' Macro\nNutrients', ' Micro\nNutrients'],
onToggle: (index) {
print('switched to: $index');
setState(() {
_swapNutrients = !_swapNutrients;
});
},
),
);
var swapTile = new Container(
child: _swapNutrients ? macroCharts : microCharts,
);
final body =
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
flex: 3,
child: Container(
width: MediaQuery.of(context).size.width,
color: Colors.transparent,
child: Card(
child: Padding(
padding: EdgeInsets.all(4.0),
child: Text(
'Welcome Alucard',
style: TextStyle(fontSize: 28.0, color: Colors.black87),
),
),
),
),
),
Expanded(
flex: 6,
child: Card(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 1,
child: userSaveBtn,
Expanded(
flex: 7,
child: Container(
width: MediaQuery.of(context).size.width,
color: Colors.transparent,
child: swapTile,//getCustomContainer(),
),
),
],
),
),
),
),
],
);
final lorem = Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit condimentum mauris id tempor. Praesent eu commodo lacus. Praesent eget mi sed libero eleifend tempor. Sed at fringilla ipsum. Duis malesuada feugiat urna vitae convallis. Aliquam eu libero arcu.',
style: TextStyle(fontSize: 16.0, color: Colors.white),
),
);
final mainBody = Container(
margin: EdgeInsets.only(
top: 16.0,
),
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.only(
top: 12.0,
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
stops: [0.5, 1.0],
colors: [
Color(0xFF03B898),
Color(0xFF01816B),
],
),
),
child: Column(
children: <Widget>[
// SizedBox(height: 32.0),
Expanded(
flex: 1,
child: petHeader,
),
Expanded(
flex: 11,
child: Padding(
padding: const EdgeInsets.all(4.0),
child: body,
),
),
// lorem,
],
),
);
return Scaffold(
body: mainBody, //_isShowingDialog ? bodyWithDialog : bodyWithCharts
);
}
}

