Loading…

Dartvel, home
Docs
Features
Studio
Cloud
Compared

Search the site

GitHub
pub.dev

GETTING STARTED

Getting started
Existing Flutter apps
Existing Native apps
Run on your phone

APP

UI and styling
Class widgets
Routing
State
Accessibility
Keyboard shortcuts
Localization
Devices and desktop
Native device access
Media, 3D and XR

DATA

Data models
Forms
Search
Sync and offline
Import and export
Change capture
Database
Cache
File storage
Images
Privacy and erasure

BACKEND

Backend functions
Auth and sessions
Authorization
Queues and jobs
Workers and memory
Notifications and mail
Outbound HTTP
AI
Webhooks
GraphQL and OpenAPI
API keys and OAuth
Multi-tenancy
Billing and commerce
Modules

OPERATIONS

Edge security
Secrets and environments
Monitoring
OpenTelemetry
Releases

SHIPPING

Build targets
Static web hosting
Servers and deploying

REFERENCE

Testing
CLI reference
Coding agents

APP

Class widgets

Prefer classes to functions? Extend DVClassWidget for pages and reusable widgets alike.

A class and a functional widget build, route and render the same way.

ON THIS PAGE

Write a page as a class

Write a reusable widget as a class

Keep state in signals

Choose a class or a function

Write a page as a class

Put @DVPage on a class that extends DVClassWidget, in a file under lib/pages. The file path is the route, as for a function page, and the generated DVRoutes member points at it.

// lib/pages/team.dart is served at /team.
@DVPage(title: 'Our team')
class const TeamListPage({super.key}) extends DVClassWidget {
  @override
  Widget build(BuildContext context) => const DVBox.list(<Widget>[
        DVText('Our team'),
        MemberCard('Ada', role: 'Engineering'),
        MemberCard('Grace', role: 'Design'),
      ]);
}

Copy code to clipboard

Give the class a const primary constructor that takes super.key.

The page is server-rendered on the web, selectable and readable by screen readers, like every other page.

A loading or error companion (about.loading.dart, about.error.dart) can be a class too.

Write a reusable widget as a class

A widget used inside pages is also a DVClassWidget. It has no @DVPage, so it gets no route. Its constructor parameters are its inputs.

// lib/components/member_card.dart
class const MemberCard(final String name, {super.key, final String role = ''})
    extends DVClassWidget {
  @override
  Widget build(BuildContext context) => DVBox.list(<Widget>[
        DVText(name).modifier(const DVModifier().fontWeight(.w600)),
        if (role.isNotEmpty) DVText(role),
      ]);
}

Copy code to clipboard

Not StatelessWidget or StatefulWidget

dartvel build reports a Flutter widget class in your app as DV-CONV-012. Extend DVClassWidget, or annotate a function with @DVFunctionalWidget.

Keep state in signals

A class widget has no State class and no setState. Create a signal with context.signal in build, and read its value there: the widget redraws when the value changes.

// State is a signal read in build, not a State class and setState.
class const LikeButton({super.key}) extends DVClassWidget {
  @override
  Widget build(BuildContext context) {
    final DVSignal<int> likes = context.signal(0);
    return DVText('Like (${likes.value})').modifier(
      DVModifier().onTap(() => likes.value = likes.value + 1),
    );
  }
}

Copy code to clipboard

Derived signals, globals and reactive data models work the same in a class as in a function. The State page explains them.

Choose a class or a function

Both shapes are first-class. Every sample in these docs has a Function tab and a Class tab.

One app can mix them: a class page can show functional widgets, and the other way round.

pageScrollToTop: true on a DVClassWidget adds the "Back to top" button, the same as on @DVFunctionalWidget.

PREVIOUS UI and styling DVBox, DVText, modifiers and layouts
NEXT Routing File pages, parameters, layouts and links
GitHub
pub.dev
npm
Acknowledgements
Privacy
Terms

FSL-1.1-MIT licensed. Built with Dartvel.

Dartvel is made by

SigmaDev Digital

To the bottom