Skip to content

Commit 7ba5e21

Browse files
Merge pull request #20 from boringdeveloper/0.2.x
Handle switch display in AppBar
2 parents 7a2aa8e + 0d818ed commit 7ba5e21

5 files changed

Lines changed: 69 additions & 52 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@
3737
* Update Readme
3838
* Added toggle icon property
3939
* Added sample usage of custom border and icons in /example
40+
41+
## [0.2.1] - February 16, 2021
42+
43+
* Handling the display of the switch in the AppBar

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(

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ packages:
6868
path: ".."
6969
relative: true
7070
source: path
71-
version: "0.2.0"
71+
version: "0.2.1"
7272
flutter_test:
7373
dependency: "direct dev"
7474
description: flutter

lib/flutter_switch.dart

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -310,58 +310,60 @@ class _FlutterSwitchState extends State<FlutterSwitch>
310310
return AnimatedBuilder(
311311
animation: _animationController,
312312
builder: (context, child) {
313-
return GestureDetector(
314-
onTap: () {
315-
if (widget.value)
316-
_animationController.forward();
317-
else
318-
_animationController.reverse();
319-
320-
widget.onToggle(!widget.value);
321-
},
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,
313+
return Align(
314+
child: GestureDetector(
315+
onTap: () {
316+
if (widget.value)
317+
_animationController.forward();
318+
else
319+
_animationController.reverse();
320+
321+
widget.onToggle(!widget.value);
322+
},
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
);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_switch
22
description: A custom switch widget that can have a custom height and width, borders, border radius, colors, toggle size, custom text and icons inside the toggle.
3-
version: 0.2.0
3+
version: 0.2.1
44
homepage: https://github.com/boringdeveloper/FlutterSwitch
55

66
environment:

0 commit comments

Comments
 (0)