Forms 2: Sliders and Buttons

The time has come to add the most important feature: the ability to rate to a dog.

1. Add the Form

Start by adding the form UI to dog_detail_page.dart.

This is what the page will look like:

dog rating page

The interface will consist of two main widgets:

  • a Slider to change the rating.
  • a RaisedButton to submit the slider value.Add them both to the _DogDetailPageState:
  1. // dog_detail_page.dart
  2. class _DogDetailPageState extends State<DogDetailPage> {
  3. final double dogAvatarSize = 150.0;
  4. // This is the starting value of the slider.
  5. double _sliderValue = 10.0;
  6. // ...
  7. Widget get addYourRating {
  8. return Column(
  9. children: <Widget>[
  10. Container(
  11. padding: EdgeInsets.symmetric(
  12. vertical: 16.0,
  13. horizontal: 16.0,
  14. ),
  15. child: Row(
  16. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  17. children: <Widget>[
  18. // In a row, column, listview, etc., a Flexible widget is a wrapper
  19. // that works much like CSS's flex-grow property.
  20. //
  21. // Any room left over in the main axis after
  22. // the widgets are given their width
  23. // will be distributed to all the flexible widgets
  24. // at a ratio based on the flex property you pass in.
  25. // Because this is the only Flexible widget,
  26. // it will take up all the extra space.
  27. //
  28. // In other words, it will expand as much as it can until
  29. // the all the space is taken up.
  30. Flexible(
  31. flex: 1,
  32. // A slider, like many form elements, needs to know its
  33. // own value and how to update that value.
  34. //
  35. // The slider will call onChanged whenever the value
  36. // changes. But it will only repaint when its value property
  37. // changes in the state using setState.
  38. //
  39. // The workflow is:
  40. // 1. User drags the slider.
  41. // 2. onChanged is called.
  42. // 3. The callback in onChanged sets the sliderValue state.
  43. // 4. Flutter repaints everything that relies on sliderValue,
  44. // in this case, just the slider at its new value.
  45. child: Slider(
  46. activeColor: Colors.indigoAccent,
  47. min: 0.0,
  48. max: 15.0,
  49. onChanged: (newRating) {
  50. setState(() => _sliderValue = newRating);
  51. },
  52. value: _sliderValue,
  53. ),
  54. ),
  55. // This is the part that displays the value of the slider.
  56. Container(
  57. width: 50.0,
  58. alignment: Alignment.center,
  59. child: Text('${_sliderValue.toInt()}',
  60. style: Theme.of(context).textTheme.display1),
  61. ),
  62. ],
  63. ),
  64. ),
  65. submitRatingButton,
  66. ],
  67. );
  68. }
  69. // A simple Raised Button that as of now doesn't do anything yet.
  70. Widget get submitRatingButton {
  71. return RaisedButton(
  72. onPressed: () => print('pressed!'),
  73. child: Text('Submit'),
  74. color: Colors.indigoAccent,
  75. );
  76. }
  77. @override
  78. Widget build(BuildContext context) {
  79. return Scaffold(
  80. backgroundColor: Colors.black87,
  81. appBar: AppBar(
  82. backgroundColor: Colors.black87,
  83. title: Text('Meet ${widget.dog.name}'),
  84. ),
  85. // Make the body a ListView that displays
  86. // both the profile and the rating form.
  87. body: ListView(
  88. children: <Widget>[dogProfile, addYourRating],
  89. ),
  90. );
  91. }
  92. }

If you hot reload your app, you should have a working slider.

2. Wire up the Submit button

The submitRatingButton does what could technically be done in the Slider onChanged callback.

It updates the rating in the dog class itself. That way, throughout the app the new rating is shown, because Flutter will rebuild everything that includes the Dog's rating.

This is as simple as adding this function to your _DogDetailPageState class, and then calling it when the submit button is pressed:

  1. // dog_detail_page.dart
  2. // In the next section you'll add error handling.
  3. // For now this is all it does.
  4. void updateRating() {
  5. setState(() => widget.dog.rating = _sliderValue.toInt());
  6. }

And then in your submitRatingButton widget:

  1. // dog_detail_page.dart
  2. Widget get submitRatingButton {
  3. return RaisedButton(
  4. onPressed: updateRating,
  5. child: Text('Submit'),
  6. color: Colors.indigoAccent,
  7. );
  8. }

After adding this, you can move the slider, press submit, and then travel back to the main page. You should see the dog's rating updated.