Last active
December 13, 2021 18:37
-
-
Save gladson/3537bfa728764ebf3b0191146f4cdfa1 to your computer and use it in GitHub Desktop.
Custom CheckBox - Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class SCheckbox extends StatefulWidget { | |
final ValueChanged<bool> onChanged; | |
final bool isChecked; | |
final String title; | |
final double size; | |
final double iconSize; | |
final Color selectedColor; | |
final Color selectedIconColor; | |
final Color borderColor; | |
final Icon checkIcon; | |
const SCheckbox( | |
{Key key, | |
this.onChanged, | |
this.isChecked, | |
this.title, | |
this.size, | |
this.iconSize, | |
this.selectedColor, | |
this.selectedIconColor, | |
this.borderColor, | |
this.checkIcon}) | |
: super(key: key); | |
@override | |
_SCheckboxState createState() => _SCheckboxState(); | |
} | |
class _SCheckboxState extends State<SCheckbox> { | |
bool _isSelected = false; | |
@override | |
void initState() { | |
_isSelected = widget.isChecked ?? false; | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Row( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Padding( | |
padding: const EdgeInsets.only( | |
top: 2.5, | |
right: 8, | |
), | |
child: SizedBox( | |
width: 24, | |
height: 24, | |
child: GestureDetector( | |
onTap: () { | |
setState(() { | |
_isSelected = !_isSelected; | |
widget.onChanged(_isSelected); | |
}); | |
}, | |
child: Container( | |
decoration: BoxDecoration( | |
borderRadius: BorderRadius.circular(3.0), | |
border: Border.all( | |
color: widget.borderColor ?? Colors.red, | |
width: 2, | |
), | |
), | |
child: AnimatedContainer( | |
margin: EdgeInsets.all(2), | |
duration: Duration(milliseconds: 1000), | |
curve: Curves.fastLinearToSlowEaseIn, | |
width: widget.size ?? 20, | |
height: widget.size ?? 20, | |
decoration: BoxDecoration( | |
color: _isSelected | |
? widget.selectedColor ?? Colors.red | |
: Colors.white, | |
), | |
child: _isSelected | |
? Padding( | |
padding: const EdgeInsets.all(2.0), | |
child: Container(color: Colors.red), | |
) | |
: Container(), | |
), | |
), | |
), | |
), | |
), | |
Expanded( | |
child: Text( | |
widget.title ?? "", | |
style: const TextStyle( | |
fontSize: 16, | |
color: const Color(0xFF222222), | |
), | |
), | |
) | |
], | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class SCheckbox extends StatefulWidget { | |
final ValueChanged<bool> onChanged; | |
final bool isChecked; | |
final String title; | |
final double? size; | |
final double? iconSize; | |
final Color? selectedColor; | |
final Color? selectedIconColor; | |
final Color? borderColor; | |
final Icon? checkIcon; | |
const SCheckbox({ | |
Key? key, | |
this.size, | |
this.iconSize, | |
this.selectedColor, | |
this.selectedIconColor, | |
this.borderColor, | |
this.checkIcon, | |
required this.onChanged, | |
required this.isChecked, | |
required this.title, | |
}) : super(key: key); | |
@override | |
_SCheckboxState createState() => _SCheckboxState(); | |
} | |
class _SCheckboxState extends State<SCheckbox> { | |
bool _isSelected = false; | |
@override | |
void initState() { | |
_isSelected = widget.isChecked ?? false; | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Stack( | |
children: [ | |
Positioned( | |
top: 7, | |
left: 30, | |
child: SizedBox( | |
width: MediaQuery.of(context).size.width * 0.9, | |
child: Text( | |
widget.title ?? "", | |
style: const TextStyle( | |
fontSize: 16, | |
color: Color(0xFF222222), | |
), | |
), | |
), | |
), | |
Positioned( | |
top: 5, | |
left: 0.5, | |
child: SizedBox( | |
width: 24, | |
height: 24, | |
child: GestureDetector( | |
onTap: () { | |
setState(() { | |
_isSelected = !_isSelected; | |
widget.onChanged(_isSelected); | |
}); | |
}, | |
child: Container( | |
decoration: BoxDecoration( | |
borderRadius: BorderRadius.circular(3.0), | |
border: Border.all( | |
color: widget.borderColor ?? Colors.red, | |
width: 2, | |
), | |
), | |
child: AnimatedContainer( | |
margin: const EdgeInsets.all(2), | |
duration: const Duration(milliseconds: 1000), | |
curve: Curves.fastLinearToSlowEaseIn, | |
width: widget.size ?? 20, | |
height: widget.size ?? 20, | |
decoration: BoxDecoration( | |
color: _isSelected | |
? widget.selectedColor ?? Colors.red | |
: Colors.white, | |
), | |
child: _isSelected | |
? Padding( | |
padding: const EdgeInsets.all(2.0), | |
child: Container(color: Colors.red), | |
) | |
: Container(), | |
), | |
), | |
), | |
), | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Para usar no projeto: