Skip to content

Form

Download .md

Forms look and behave the same in every product and framework. That includes Flutter, which used to put floating labels inside the fields.

  • 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.
  1. Card with a title and one sentence about what to fill in.
  2. Fields, each with a label above it and an optional one-sentence hint below.
  3. Error under its field, in destructive, joined to the field with aria-describedby.
  4. 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>
)
}

Card, Label, Input, Select, Checkbox and Button.

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.