Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add active/inactive text alignment #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 23 additions & 14 deletions lib/flutter_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class FlutterSwitch extends StatefulWidget {
this.inactiveToggleBorder,
this.activeIcon,
this.inactiveIcon,
this.activeTextAlignment = Alignment.centerLeft,
this.inactiveTextAlignment = Alignment.centerRight,
this.duration = const Duration(milliseconds: 200),
this.disabled = false,
}) : assert(
Expand Down Expand Up @@ -251,6 +253,16 @@ class FlutterSwitch extends StatefulWidget {
/// Defaults to the value of false.
final bool disabled;

/// The alignment of the text when the given value is true.
///
/// Defaults to [Alignment.centerLeft].
final Alignment activeTextAlignment;

/// The alignment of the text when the given value is false.
///
/// Defaults to [Alignment.centerRight].
final Alignment inactiveTextAlignment;

@override
_FlutterSwitchState createState() => _FlutterSwitchState();
}
Expand Down Expand Up @@ -358,7 +370,7 @@ class _FlutterSwitchState extends State<FlutterSwitch>
child: Container(
width: _textSpace,
padding: EdgeInsets.symmetric(horizontal: 4.0),
alignment: Alignment.centerLeft,
alignment: widget.activeTextAlignment,
child: _activeText,
),
),
Expand All @@ -370,7 +382,7 @@ class _FlutterSwitchState extends State<FlutterSwitch>
child: Container(
width: _textSpace,
padding: EdgeInsets.symmetric(horizontal: 4.0),
alignment: Alignment.centerRight,
alignment: widget.inactiveTextAlignment,
child: _inactiveText,
),
),
Expand All @@ -391,20 +403,17 @@ class _FlutterSwitchState extends State<FlutterSwitch>
fit: BoxFit.contain,
child: Container(
child: Stack(
alignment: Alignment.center,
children: [
Center(
child: AnimatedOpacity(
opacity: widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.activeIcon,
),
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,
),
AnimatedOpacity(
opacity: !widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.inactiveIcon,
),
],
),
Expand Down