VelocityX Swiper in Flutter: A Practical Example
VelocityX gives you short, chainable widgets, and its Swiper is one of the most useful pieces: a horizontal card pager with snapping, looping, and callbacks in a few lines. If you have ever wired a PageView with a controller, indicators, and physics tweaks, you know the boilerplate adds up fast. A VelocityX Swiper collapses most of that into a single widget you can drop into a onboarding screen, a promo carousel, or a media picker.
This example walks through adding the package, building a reusable swipe card, hooking up callbacks and a controller, and keeping it smooth on mid-range devices. The numbers below are typical ranges from everyday app work, not benchmark claims. Treat them as starting points for your own profiling.
Add VelocityX and check compatibility
VelocityX is a widget utility package, not a full framework. Add it to pubspec.yaml and run pub get. The package moves quickly, so pin a version you have tested rather than using any. Before you commit, run flutter pub outdated and check that your Flutter SDK version is supported by the release you pick. Most breakages in the wild come from mixing a VelocityX version built for an older SDK with a newer Flutter channel.
If your app already uses Material or Cupertino widgets heavily, VelocityX works alongside them; it does not replace your theme. Keep your design tokens (spacing, radius, colors) in one place and pass them into the Swiper items. That way a package upgrade or a switch back to a plain PageView does not force a redesign. If you are still deciding on a styling direction, this comparison helps: Cupertino vs Material Widgets: Which Style Fits Your App?.
A quick sanity check after installing: build a bare Swiper with three colored containers and swipe it on both an emulator and a physical device. If the swipe feels sticky, the issue is usually the parent scroll view, not the Swiper itself. Nested scrollables are the most common source of gesture conflicts in Flutter carousels.
- Pin an exact version in pubspec.yaml (for example velocity_x: 4.x.y) and commit the lockfile.
- Run flutter pub outdated before upgrading the Flutter SDK.
- Test gestures inside the real parent scroll view, not only on an empty screen.
Build the swipe card item
Each Swiper child should be a self-contained card: fixed or aspect-ratio height, rounded corners, and a single tap target. Avoid unbounded heights; a Swiper inside a Column needs an explicit height or an Expanded parent. A typical card height sits between 160 and 240 logical pixels on phones, with 12 to 16 pixels of horizontal gap between pages.
Keep the card lightweight. A card with an image, a title, a subtitle, and one button usually stays under 30 widgets. If yours climbs past 60, you are probably nesting decorative containers that could be flattened. For a reality check on widget counts, see How Many Widgets Does a Typical Flutter Screen Really Use?.
Use const constructors for the static parts and cache images with the built-in image cache or a disk cache. On a mid-range Android phone, a card that decodes a 1200x800 JPEG on every swipe can drop frames; decoding once at 2x the display width is usually enough. Keep text styles outside the builder where possible so a rebuild does not reallocate them.
- Give the Swiper an explicit height (160–240 px) or wrap it in Expanded.
- Keep each card under roughly 50 widgets by flattening decorative containers.
- Decode images near 2x display width instead of full resolution.
Wire callbacks and a controller
Swiper exposes callbacks for page changes and a controller for programmatic navigation. Use onPageChanged to update an indicator or fetch more items when you get within two pages of the end. Prefetching two pages ahead keeps the next card ready and avoids the blank frame you see when data loads on demand.
If you need buttons like Next and Previous, create a VxSwiperController, pass it to the Swiper, and call next() and previous() from your buttons. Dispose the controller in State.dispose. Forgetting disposal is a small leak, but it also keeps listeners alive after the screen is gone, which can trigger setState after dispose in long sessions.
State management choice matters less than consistency. If the carousel index drives other UI, keep it in a single StatefulWidget or your state management solution, not in both. This is the same discipline covered in StatelessWidget vs StatefulWidget: Which One Should You Pick?.
- Prefetch items when the current index is within 2 pages of the end.
- Dispose VxSwiperController in State.dispose.
- Keep the active index in one owner; do not mirror it in two widgets.
Choose options that match the UX
The defaults are reasonable, but three options change the feel most: viewportFraction, enableLoop, and scrollDirection. A viewportFraction around 0.85 to 0.92 shows a peek of the next card, which signals swiping without an instruction label. Looping is great for promos and bad for step-by-step flows where users need to know where they are.
Autoplay is tempting for banners. If you use it, pause on user interaction and stop when the widget is offscreen. An interval under 4 seconds feels rushed; 5 to 7 seconds is a calmer range. Also respect the platform reduce-motion setting where available, since constant motion is a real accessibility concern.
If you are already deep in RecyclerView patterns on the Android side, the mental model is similar: adapter, view holder, and binding. The pitfalls transfer too, as covered in Data Binding in RecyclerView: Setup and Common Pitfalls.
- viewportFraction 0.85–0.92 for a peek effect.
- Loop only for promotional content, not step flows.
- Autoplay interval 5–7 seconds, paused on interaction.
Performance and testing checklist
Profile in release mode on a physical mid-range device. In debug mode, every frame is slower and numbers mislead. Watch for builds over 16 ms during a swipe; that is the budget for 60 fps, and many devices still run at 60. If you see jank, check image decoding first, then rebuild scope, then the parent scroll view.
Test with 0 items, 1 item, 3 items, and 20 items. Empty and single-item states are where Swiper code most often throws or shows a blank area. Also test with long text and large font scale; a card that fits at 1.0 can overflow at 1.3. Wrap text in maxLines and overflow settings deliberately.
Finally, add a widget test that pumps the Swiper and asserts the callback fires after a drag. It catches accidental breakage during package upgrades. If the Swiper sits above a local database that changes, migrate carefully; this checklist applies: Room Database Migration Checklist Before You Ship.
- Profile only in release mode on a real device.
- Aim to keep card builds under 16 ms per frame.
- Test 0, 1, 3, and 20 items plus font scale 1.3.
| Choice | Typical value | Effect on UX | Watch out for |
|---|---|---|---|
| viewportFraction | 0.85–0.92 | Shows a peek of the next card | Cards narrower than expected on tablets |
| Card height | 160–240 px | Comfortable tap area | Overflow with large font scale |
| enableLoop | true for promos | Endless browsing | Confusing in step flows |
| Autoplay interval | 5–7 s | Calm rotation | Motion sensitivity; pause on touch |
| Prefetch distance | 2 pages | No blank next card | Extra network calls on slow links |
Key points
- Pin the VelocityX version and verify it against your Flutter SDK before shipping.
- Give the Swiper an explicit height and keep each card under about 50 widgets.
- Prefetch two pages ahead and dispose the controller to avoid blank frames and leaks.
- Profile in release mode; keep card builds under the 16.7 ms frame budget.
Flutter Screen Widget and Cost Estimator
Estimate the number of widgets in a screen and a rough development-hour figure from your component counts. Adjust the fields to see how each UI area affects the total effort.
Editor and Flutter contributor
Writes practical Flutter tutorials focused on widgets, layout and animation. Enjoys breaking large UI problems into small, testable pieces and explaining them clearly to beginners.