Your First Flutter App: Polishing the App

Apr 12 2022 Dart 2.14.1, Flutter 2.5, Visual Studio Code

Part 3: Style the App

25. Style the Slider Thumb

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 24. Style the Slider Next episode: 26. Conclusion

This video was last updated on Apr 12 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

Our slider looks really good, but it would be nice to upgrade the thumb. We want to add a custom image to it that looks like bullseye. Now, there are a couple of challenging aspects when updating the thumb. We first need to get the image from our app bundle.

import 'package:flutter/material.dart';
import 'dart:ui' as ui;
class SliderThumbImage extends SliderComponentShape {

}
final ui.Image? image;
SliderThumbImage(this.image);
@override
Size getPreferredSize(bool isEnabled, bool isDiscrete) {
  return const Size(0, 0);
}
  @override
  void paint(
    PaintingContext context,
    Offset center, {
    required Animation<double> activationAnimation,
    required Animation<double> enableAnimation,
    required bool isDiscrete,
    required TextPainter labelPainter,
    required RenderBox parentBox,
    required SliderThemeData sliderTheme,
    required TextDirection textDirection,
    required double value,
    required double textScaleFactor,
    required Size sizeWithOverflow,
  }) {

  }
final thumbImage = image;
if (thumbImage != null) {

}
final canvas = context.canvas;
final imageOffset = Offset(
    center.dx - (thumbImage.width / 2),
    center.dy - (thumbImage.height / 2),
);
var paint = Paint();
paint.filterQuality = FilterQuality.high;
canvas.drawImage(thumbImage, imageOffset, paint);
import 'slider_thumb_image.dart';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
ui.Image? _sliderImage;
Future<ui.Image> _load(String asset) async {

}
final data = await rootBundle.load(asset);
final codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
final fi = await codec.getNextFrame();
return fi.image;
@override
void initState() {
  _load('images/nub.png').then((image) {
      setState(() {
      _sliderImage = image;
      });
  });
  super.initState();
}
thumbShape: SliderThumbImage(_sliderImage),