Skip to content

Instantly share code, notes, and snippets.

@tomjnsn
Created October 6, 2020 19:40
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 tomjnsn/00714828b04d61a5d196e08900a4a9fc to your computer and use it in GitHub Desktop.
Save tomjnsn/00714828b04d61a5d196e08900a4a9fc to your computer and use it in GitHub Desktop.
Sample Flutter Likert
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: MyStatefulWidget(),
),
);
}
}
class LabeledRadio extends StatelessWidget {
const LabeledRadio({
this.label,
this.padding,
this.groupValue,
this.value,
this.onChanged,
this.itemOrder,
});
final String label;
final EdgeInsets padding;
final int groupValue;
final int value;
final Function onChanged;
final int itemOrder;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
if (value != groupValue) onChanged(value);
},
child: CustomPaint(
painter: _LinePainter(itemOrder),
child: Padding(
padding: padding,
child: Row(
children: <Widget>[
Radio<int>(
groupValue: groupValue,
value: value,
onChanged: (int newValue) {
onChanged(newValue);
},
),
Text(label),
],
),
),
),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedVal = -1;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <LabeledRadio>[
LabeledRadio(
label: 'Strongly Disagree',
padding: const EdgeInsets.symmetric(horizontal: 5.0),
value: 2,
groupValue: _selectedVal,
onChanged: (int newValue) {
setState(() {
_selectedVal = newValue;
});
},
itemOrder: _LinePainter.FIRST_ITEM,
),
LabeledRadio(
label: 'Disagree',
padding: const EdgeInsets.symmetric(horizontal: 5.0),
value: 3,
groupValue: _selectedVal,
onChanged: (int newValue) {
setState(() {
_selectedVal = newValue;
});
},
itemOrder: _LinePainter.MIDDLE_ITEM,
),
LabeledRadio(
label: 'Neutral',
padding: const EdgeInsets.symmetric(horizontal: 5.0),
value: 4,
groupValue: _selectedVal,
onChanged: (int newValue) {
setState(() {
_selectedVal = newValue;
});
},
itemOrder: _LinePainter.MIDDLE_ITEM,
),
LabeledRadio(
label: 'Agree',
padding: const EdgeInsets.symmetric(horizontal: 5.0),
value: 5,
groupValue: _selectedVal,
onChanged: (int newValue) {
setState(() {
_selectedVal = newValue;
});
},
itemOrder: _LinePainter.MIDDLE_ITEM,
),
LabeledRadio(
label: 'Strongly Agree',
padding: const EdgeInsets.symmetric(horizontal: 5.0),
value: 6,
groupValue: _selectedVal,
onChanged: (int newValue) {
setState(() {
_selectedVal = newValue;
});
},
itemOrder: _LinePainter.LAST_ITEM,
),
],
),
);
}
}
class _LinePainter extends CustomPainter {
static const FIRST_ITEM = -1;
static const MIDDLE_ITEM = 0;
static const LAST_ITEM = 1;
int buttonType;
_LinePainter(this.buttonType);
@override
void paint(Canvas canvas, Size size) {
final p = Paint()
..color = Colors.black
..strokeWidth = 2;
if (buttonType == MIDDLE_ITEM || buttonType == LAST_ITEM) {
canvas.drawLine(Offset(29, 0), Offset(29, (size.height / 2) - 9), p);
}
if (buttonType == FIRST_ITEM || buttonType == MIDDLE_ITEM) {
canvas.drawLine(
Offset(29, (size.height / 2) + 9), Offset(29, size.height), p);
}
}
@override
bool shouldRepaint(_LinePainter oldDelegate) => false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment