Skip to content

Instantly share code, notes, and snippets.

@hacker1024
Created March 5, 2019 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hacker1024/8424c4dd9e3c6f10056a9d95bde45501 to your computer and use it in GitHub Desktop.
Save hacker1024/8424c4dd9e3c6f10056a9d95bde45501 to your computer and use it in GitHub Desktop.
A simple segmented control form field for flutter.
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