Created
March 5, 2019 07:39
-
-
Save hacker1024/8424c4dd9e3c6f10056a9d95bde45501 to your computer and use it in GitHub Desktop.
A simple segmented control form field for flutter.
This file contains 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 SegmentedControlFormField extends FormField<int> { | |
final List<String> options; | |
final double textScaleFactor; | |
SegmentedControlFormField({ | |
this.options, | |
this.textScaleFactor, | |
FormFieldSetter<int> onSaved, | |
FormFieldValidator<int> validator, | |
int initialValue = 0, | |
bool autovalidate = false, | |
}) : super( | |
onSaved: onSaved, | |
validator: validator, | |
initialValue: initialValue, | |
autovalidate: autovalidate, | |
builder: (FormFieldState<int> state) { | |
return Container( | |
padding: const EdgeInsets.symmetric(horizontal: 2), | |
decoration: BoxDecoration( | |
color: swatch.shade500, | |
borderRadius: BorderRadius.circular(2), | |
), | |
child: Row( | |
children: options.map<Widget>( | |
(String option) { | |
return Expanded( | |
child: Padding( | |
padding: const EdgeInsets.symmetric( | |
vertical: 4, horizontal: 2), | |
child: Stack( | |
alignment: Alignment.center, | |
children: <Widget>[ | |
GestureDetector( | |
onTap: () { | |
state.didChange(options.indexOf(option)); | |
}, | |
child: AnimatedContainer( | |
height: 48, | |
duration: Duration(milliseconds: 100), | |
decoration: BoxDecoration( | |
color: options.indexOf(option) == state.value | |
? swatch.shade100 | |
: swatch.shade500, | |
borderRadius: BorderRadius.circular(2), | |
), | |
), | |
), | |
IgnorePointer( | |
child: Text( | |
option, | |
style: TextStyle( | |
color: | |
options.indexOf(option) == state.value | |
? Colors.black | |
: Colors.white), | |
textScaleFactor: textScaleFactor, | |
softWrap: false, | |
overflow: TextOverflow.fade, | |
), | |
), | |
], | |
), | |
), | |
); | |
}, | |
).toList(growable: false), | |
), | |
); | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment