Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.3.1] - April 12, 2021

* Revised the activeIcon and inactiveIcon to accept other types of widget including Images, Icon and FontAwesome Icons.

## [0.3.0] - March 06, 2021

* Migrated to null safety. Beware for possible breaking changes.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Add this to your package's `pubspec.yaml` file:

```yaml
dependencies:
flutter_switch: ^0.3.0
flutter_switch: ^0.3.1
```

You can install packages from the command line with Flutter:
Expand Down
72 changes: 72 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class _MyHomePageState extends State<MyHomePage> {
bool status5 = false;
bool status6 = false;
bool status7 = false;
bool status8 = false;
bool status9 = false;
bool isSwitchOn = false;

Color _textColor = Colors.black;
Expand Down Expand Up @@ -297,6 +299,76 @@ class _MyHomePageState extends State<MyHomePage> {
),
],
),
SizedBox(height: 20.0),
Text("Image as toggle icon"),
SizedBox(height: 10.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlutterSwitch(
width: 100.0,
height: 55.0,
toggleSize: 45.0,
value: status8,
borderRadius: 30.0,
padding: 2.0,
activeToggleColor: Color(0xFF0082C8),
inactiveToggleColor: Color(0xFF01579B),
activeSwitchBorder: Border.all(
color: Color(0xFF00D2B8),
width: 6.0,
),
inactiveSwitchBorder: Border.all(
color: Color(0xFF29B6F6),
width: 6.0,
),
activeColor: Color(0xFF55DDCA),
inactiveColor: Color(0xFF54C5F8),
activeIcon: Image.network(
"https://img2.pngio.com/functional-bits-in-flutter-flutter-community-medium-flutter-png-1000_1000.png",
),
inactiveIcon: Image.network(
"https://upload.wikimedia.org/wikipedia/commons/7/7e/Dart-logo.png",
),
onToggle: (val) {
setState(() {
status8 = val;
});
},
),
Container(
alignment: Alignment.centerRight,
child: Text("Value: $status8"),
),
],
),
SizedBox(height: 10.0),
Text("Rounded Rectangle Switch"),
SizedBox(height: 10.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlutterSwitch(
toggleRatio: 1.5,
height: 40,
width: 100,
toggleSize: 40,
value: status9,
onToggle: (val) {
setState(() {
status9 = val;
});
},
),
Container(
alignment: Alignment.centerRight,
child: Text(
"Value: $status9",
),
),
],
),
SizedBox(height: 20.0),
],
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.3.0"
version: "0.3.1"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
60 changes: 40 additions & 20 deletions lib/flutter_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FlutterSwitch extends StatefulWidget {
this.inactiveIcon,
this.duration = const Duration(milliseconds: 200),
this.disabled = false,
this.toggleRatio = 1,
}) : assert(
(switchBorder == null || activeSwitchBorder == null) &&
(switchBorder == null || inactiveSwitchBorder == null),
Expand Down Expand Up @@ -230,14 +231,16 @@ class FlutterSwitch extends StatefulWidget {
final BoxBorder? inactiveToggleBorder;

/// The icon inside the toggle when the given value is true.
/// activeIcon can be an Icon Widget, an Image or Fontawesome Icons.
///
/// This property is optional.
final Icon? activeIcon;
final Widget? activeIcon;

/// The icon inside the toggle when the given value is false.
/// inactiveIcon can be an Icon Widget, an Image or Fontawesome Icons.
///
/// This property is optional.
final Icon? inactiveIcon;
final Widget? inactiveIcon;

/// The duration in milliseconds to change the state of the switch
///
Expand All @@ -249,6 +252,13 @@ class FlutterSwitch extends StatefulWidget {
/// Defaults to the value of false.
final bool disabled;

/// The toggle will be a rounded rectangle when the value is not 1.
///
/// Width = Size * toggleRatio, Height = Size.
///
/// Defaults to the value of 1.
final double toggleRatio;

@override
_FlutterSwitchState createState() => _FlutterSwitchState();
}
Expand Down Expand Up @@ -375,31 +385,40 @@ class _FlutterSwitchState extends State<FlutterSwitch>
child: Align(
alignment: _toggleAnimation.value,
child: Container(
width: widget.toggleSize,
width: widget.toggleSize * widget.toggleRatio,
height: widget.toggleSize,
padding: EdgeInsets.all(4.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
shape: widget.toggleRatio == 1
? BoxShape.circle
: BoxShape.rectangle,
color: _toggleColor,
borderRadius: widget.toggleRatio == 1
? null
: BorderRadius.circular(widget.borderRadius),
border: _toggleBorder,
),
child: Container(
child: Stack(
children: [
Center(
child: AnimatedOpacity(
opacity: widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.activeIcon,
child: FittedBox(
fit: BoxFit.contain,
child: Container(
child: Stack(
children: [
Center(
child: AnimatedOpacity(
opacity: widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.activeIcon,
),
),
),
Center(
child: AnimatedOpacity(
opacity: !widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.inactiveIcon,
Center(
child: AnimatedOpacity(
opacity: !widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.inactiveIcon,
),
),
),
],
],
),
),
),
),
Expand All @@ -417,6 +436,7 @@ class _FlutterSwitchState extends State<FlutterSwitch>

FontWeight get _activeTextFontWeight =>
widget.activeTextFontWeight ?? FontWeight.w900;

FontWeight get _inactiveTextFontWeight =>
widget.inactiveTextFontWeight ?? FontWeight.w900;

Expand Down
38 changes: 19 additions & 19 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,49 @@ packages:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.5.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.15.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
flutter:
Expand All @@ -64,21 +64,21 @@ packages:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0"
sky_engine:
Expand All @@ -90,56 +90,56 @@ packages:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.2.19"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
sdks:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_switch
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.
version: 0.3.0
version: 0.3.1
homepage: https://github.com/boringdeveloper/FlutterSwitch

environment:
Expand Down