Form
Forms look and behave the same in every product and framework. That includes Flutter, which used to put floating labels inside the fields.
When to use
Section titled “When to use”- Use it to create or edit a record.
- Split a long form into cards with one heading each. Use the Login page for sign in.
Anatomy
Section titled “Anatomy”- Card with a title and one sentence about what to fill in.
- Fields, each with a label above it and an optional one-sentence hint below.
- Error under its field, in
destructive, joined to the field witharia-describedby. - Footer: “Batal” as an outline button, then the primary action.
- Labels above the fields, never inside them as a placeholder or a floating label.
- Hints are one practical sentence: what to fill in, an example, or a limit.
- Errors come from the backend. The client only enables or disables the submit button; it never writes its own error text. Set
aria-invalid="true"on the field. - One primary action per form, last in the footer.
import { Button } from "@/components/ui/button"import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"import { Checkbox } from "@/components/ui/checkbox"import { Input } from "@/components/ui/input"import { Label } from "@/components/ui/label"import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
const genres = [ { label: "Pilih genre", value: null }, { label: "Pop", value: "pop" }, { label: "Rock", value: "rock" }, { label: "Dangdut", value: "dangdut" }, { label: "Jazz", value: "jazz" },]
/** * A form: labels above the fields, a one-sentence hint under a field, the error the backend sent under its * field, and the actions in the card footer. The error text comes from the API response, never from the client. */export default function FormPageDemo() { return ( <div className="min-h-svh bg-background p-6 text-foreground"> <form className="mx-auto max-w-2xl" onSubmit={(e) => e.preventDefault()}> <Card> <CardHeader> <CardTitle>Data rilis</CardTitle> <CardDescription>Isi sesuai yang akan tampil di platform musik.</CardDescription> </CardHeader> <CardContent className="grid gap-5"> <div className="grid gap-2"> <Label htmlFor="release-title">Judul</Label> <Input id="release-title" aria-invalid="true" aria-describedby="release-title-hint release-title-error" /> <p id="release-title-hint" className="text-sm text-muted-foreground"> Tulis persis seperti di sampul. </p> <p id="release-title-error" className="text-sm text-destructive"> Judul lagunya belum diisi. </p> </div> <div className="grid gap-2"> <Label htmlFor="release-artist">Artis</Label> <Input id="release-artist" defaultValue="Raka Pratama" /> </div> <div className="grid gap-2"> <Label htmlFor="release-genre">Genre</Label> <Select items={genres}> <SelectTrigger id="release-genre" className="w-full"> <SelectValue /> </SelectTrigger> <SelectContent> {genres .filter((g) => g.value !== null) .map((g) => ( <SelectItem key={g.value} value={g.value}> {g.label} </SelectItem> ))} </SelectContent> </Select> </div> <div className="flex items-center gap-2"> <Checkbox id="release-explicit" /> <Label htmlFor="release-explicit">Lagu ini mengandung lirik eksplisit</Label> </div> </CardContent> <CardFooter className="justify-end gap-2"> <Button type="button" variant="outline"> Batal </Button> <Button type="submit">Simpan</Button> </CardFooter> </Card> </form> </div> )}<script setup lang="ts">import { Button } from "@/components/ui/button"import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"import { Checkbox } from "@/components/ui/checkbox"import { Input } from "@/components/ui/input"import { Label } from "@/components/ui/label"import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
/** * A form: labels above the fields, a one-sentence hint under a field, the error the backend sent under its * field, and the actions in the card footer. The error text comes from the API response, never from the client. */const genres = [ { label: "Pop", value: "pop" }, { label: "Rock", value: "rock" }, { label: "Dangdut", value: "dangdut" }, { label: "Jazz", value: "jazz" },]</script>
<template> <div class="min-h-svh bg-background p-6 text-foreground"> <form class="mx-auto max-w-2xl" @submit.prevent> <Card> <CardHeader> <CardTitle>Data rilis</CardTitle> <CardDescription>Isi sesuai yang akan tampil di platform musik.</CardDescription> </CardHeader> <CardContent class="grid gap-5"> <div class="grid gap-2"> <Label for="release-title-vue">Judul</Label> <Input id="release-title-vue" aria-invalid="true" aria-describedby="release-title-hint-vue release-title-error-vue" /> <p id="release-title-hint-vue" class="text-sm text-muted-foreground">Tulis persis seperti di sampul.</p> <p id="release-title-error-vue" class="text-sm text-destructive">Judul lagunya belum diisi.</p> </div> <div class="grid gap-2"> <Label for="release-artist-vue">Artis</Label> <Input id="release-artist-vue" default-value="Raka Pratama" /> </div> <div class="grid gap-2"> <Label for="release-genre-vue">Genre</Label> <Select> <SelectTrigger id="release-genre-vue" class="w-full"> <SelectValue placeholder="Pilih genre" /> </SelectTrigger> <SelectContent> <SelectItem v-for="g in genres" :key="g.value" :value="g.value">{{ g.label }}</SelectItem> </SelectContent> </Select> </div> <div class="flex items-center gap-2"> <Checkbox id="release-explicit-vue" /> <Label for="release-explicit-vue">Lagu ini mengandung lirik eksplisit</Label> </div> </CardContent> <CardFooter class="justify-end gap-2"> <Button type="button" variant="outline">Batal</Button> <Button type="submit">Simpan</Button> </CardFooter> </Card> </form> </div></template>// A form: labels above the fields (ShadInputFormField's label, never a floating Material labelText),// a one-sentence hint, the error the backend sent under its field, and the actions at the bottom.import 'package:flutter/widgets.dart';import 'package:shadcn_ui/shadcn_ui.dart';
class FormPageDemo extends StatelessWidget { const FormPageDemo({super.key});
@override Widget build(BuildContext context) { final theme = ShadTheme.of(context); return ShadCard( width: 576, title: Text('Data rilis', style: theme.textTheme.h4), description: const Text('Isi sesuai yang akan tampil di platform musik.'), footer: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ ShadButton.outline(onPressed: () {}, child: const Text('Batal')), const SizedBox(width: 8), ShadButton(onPressed: () {}, child: const Text('Simpan')), ], ), child: Padding( padding: const EdgeInsets.symmetric(vertical: 16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ ShadInputFormField( id: 'title', label: const Text('Judul'), description: const Text('Tulis persis seperti di sampul.'), // The message comes from the API response; the app never writes its own. forceErrorText: 'Judul lagunya belum diisi.', ), const SizedBox(height: 16), ShadInputFormField(id: 'artist', label: const Text('Artis'), initialValue: 'Raka Pratama'), const SizedBox(height: 16), ShadSelectFormField<String>( id: 'genre', label: const Text('Genre'), minWidth: 544, placeholder: const Text('Pilih genre'), options: const [ ShadOption(value: 'pop', child: Text('Pop')), ShadOption(value: 'rock', child: Text('Rock')), ShadOption(value: 'dangdut', child: Text('Dangdut')), ShadOption(value: 'jazz', child: Text('Jazz')), ], selectedOptionBuilder: (context, value) => Text(value), ), const SizedBox(height: 16), ShadCheckboxFormField( id: 'explicit', initialValue: false, inputLabel: const Text('Lagu ini mengandung lirik eksplisit'), ), ], ), ), ); }}Built from
Section titled “Built from”Card, Label, Input, Select, Checkbox and Button.
Flutter
Section titled “Flutter”Use ShadInputFormField(label: …), not InputDecoration.labelText. The label then sits above the field as it does on the web. Show the backend’s message with forceErrorText.
