Skip to content

Commit 9581c25

Browse files
committed
Handle switch display in appbar
1 parent 44ed032 commit 9581c25

2 files changed

Lines changed: 54 additions & 41 deletions

File tree

example/lib/main.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class _MyHomePageState extends State<MyHomePage> {
2828
bool status5 = false;
2929
bool status6 = false;
3030
bool status7 = false;
31+
bool isSwitchOn = false;
3132

3233
Color _textColor = Colors.black;
3334
Color _appBarColor = Color.fromRGBO(36, 41, 46, 1);
@@ -50,6 +51,16 @@ class _MyHomePageState extends State<MyHomePage> {
5051
"FlutterSwitch Demo",
5152
style: TextStyle(color: Colors.white),
5253
),
54+
actions: [
55+
FlutterSwitch(
56+
value: isSwitchOn,
57+
onToggle: (value) {
58+
setState(() {
59+
isSwitchOn = value;
60+
});
61+
},
62+
),
63+
],
5364
),
5465
body: SingleChildScrollView(
5566
child: Padding(

lib/flutter_switch.dart

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -319,49 +319,51 @@ class _FlutterSwitchState extends State<FlutterSwitch>
319319

320320
widget.onToggle(!widget.value);
321321
},
322-
child: Container(
323-
width: widget.width,
324-
height: widget.height,
325-
padding: EdgeInsets.all(widget.padding),
326-
decoration: BoxDecoration(
327-
borderRadius: BorderRadius.circular(widget.borderRadius),
328-
color: _switchColor,
329-
border: _switchBorder,
330-
),
331-
child: Row(
332-
mainAxisAlignment: MainAxisAlignment.spaceBetween,
333-
children: <Widget>[
334-
_toggleAnimation.value == Alignment.centerRight
335-
? Expanded(
336-
child: Container(
337-
padding: EdgeInsets.symmetric(horizontal: 4.0),
338-
child: _activeText,
339-
),
340-
)
341-
: Container(),
342-
Align(
343-
alignment: _toggleAnimation.value,
344-
child: Container(
345-
width: widget.toggleSize,
346-
height: widget.toggleSize,
347-
decoration: BoxDecoration(
348-
shape: BoxShape.circle,
349-
color: _toggleColor ?? Colors.white,
350-
border: _toggleBorder,
322+
child: Align(
323+
child: Container(
324+
width: widget.width,
325+
height: widget.height,
326+
padding: EdgeInsets.all(widget.padding),
327+
decoration: BoxDecoration(
328+
borderRadius: BorderRadius.circular(widget.borderRadius),
329+
color: _switchColor,
330+
border: _switchBorder,
331+
),
332+
child: Row(
333+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
334+
children: <Widget>[
335+
_toggleAnimation.value == Alignment.centerRight
336+
? Expanded(
337+
child: Container(
338+
padding: EdgeInsets.symmetric(horizontal: 4.0),
339+
child: _activeText,
340+
),
341+
)
342+
: Container(),
343+
Align(
344+
alignment: _toggleAnimation.value,
345+
child: Container(
346+
width: widget.toggleSize,
347+
height: widget.toggleSize,
348+
decoration: BoxDecoration(
349+
shape: BoxShape.circle,
350+
color: _toggleColor ?? Colors.white,
351+
border: _toggleBorder,
352+
),
353+
child: _icon,
351354
),
352-
child: _icon,
353355
),
354-
),
355-
_toggleAnimation.value == Alignment.centerLeft
356-
? Expanded(
357-
child: Container(
358-
padding: EdgeInsets.symmetric(horizontal: 4.0),
359-
alignment: Alignment.centerRight,
360-
child: _inactiveText,
361-
),
362-
)
363-
: Container(),
364-
],
356+
_toggleAnimation.value == Alignment.centerLeft
357+
? Expanded(
358+
child: Container(
359+
padding: EdgeInsets.symmetric(horizontal: 4.0),
360+
alignment: Alignment.centerRight,
361+
child: _inactiveText,
362+
),
363+
)
364+
: Container(),
365+
],
366+
),
365367
),
366368
),
367369
);

0 commit comments

Comments
 (0)