Forms
Radio
Picks exactly one option from a small, visible set.


Static preview
use std::fmt;
use iced::Element;
use iced::widget::{column, text};
use iced_cube::primitives::radio_group;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Density {
Comfortable,
Default,
Compact,
}
impl fmt::Display for Density {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Density::Comfortable => "Comfortable",
Density::Default => "Default",
Density::Compact => "Compact",
})
}
}
#[derive(Debug, Clone)]
pub enum Message {
Selected(Density),
}
#[derive(Debug)]
pub struct Example {
density: Density,
}
impl Default for Example {
fn default() -> Self {
Self {
density: Density::Default,
}
}
}
impl Example {
pub fn update(&mut self, message: Message) {
let Message::Selected(density) = message;
self.density = density;
}
pub fn view(&self) -> Element<'_, Message> {
column![
radio_group(
[Density::Comfortable, Density::Default, Density::Compact],
Some(self.density),
)
.on_select(Message::Selected),
text(format!("Selected: {}", self.density)).size(14),
]
.spacing(20)
.width(220)
.into()
}
}Usage
Use radios when there are only a handful of options and seeing them all at once helps the choice. For longer lists, a select takes less room.
Options are usually a small Copy enum with a Display implementation. Clicking a radio or its label emits its value; the app stores it and passes it back as selected.
Examples
Horizontal
The same group laid out in a row.


Static preview
use iced::Element;
use iced_cube::primitives::radio::Direction;
use iced_cube::primitives::radio_group;
#[derive(Debug, Clone)]
pub enum Message {
Selected(&'static str),
}
#[derive(Debug)]
pub struct Example {
size: &'static str,
}
impl Default for Example {
fn default() -> Self {
Self { size: "Medium" }
}
}
impl Example {
pub fn update(&mut self, message: Message) {
let Message::Selected(size) = message;
self.size = size;
}
pub fn view(&self) -> Element<'_, Message> {
radio_group(["Small", "Medium", "Large"], Some(self.size))
.direction(Direction::Horizontal)
.on_select(Message::Selected)
.into()
}
}Single radios
Individual radios, one of them disabled.


Static preview
use iced::Element;
use iced::widget::column;
use iced_cube::primitives::radio;
#[derive(Debug, Clone)]
pub enum Message {
Selected(u8),
}
#[derive(Debug)]
pub struct Example {
plan: u8,
}
impl Default for Example {
fn default() -> Self {
Self { plan: 1 }
}
}
impl Example {
pub fn update(&mut self, message: Message) {
let Message::Selected(plan) = message;
self.plan = plan;
}
pub fn view(&self) -> Element<'_, Message> {
let selected = Some(self.plan);
column![
radio("Starter", 1, selected).on_select(Message::Selected),
radio("Professional", 2, selected).on_select(Message::Selected),
radio("Enterprise (contact sales)", 3, selected),
]
.spacing(12)
.into()
}
}API
| Item | Description |
|---|---|
radio_group(options, selected) | Creates one radio per option, labelled with its Display text. It renders disabled until it has a message. |
.on_select(f) | Sets the message emitted with the option that was clicked. |
.direction(Direction) | Vertical or Horizontal. Defaults to Vertical. Horizontal groups wrap when space runs out. |
radio(label, value, selected) | Creates a single radio for building custom layouts. It is selected when selected holds its value. |
radio(...).on_select(f) | Sets the message for a click, built from the radio's value. Without it the radio is disabled. |