I am trying to make a way to iterate over a vector of text inputs, create a text field to each input and link that text field to the content by index. When I build my program, the compile does not raise any error.
When I run the code, the code crashs. I am not sure what I am doing wrong here.
here is my code:
use iced::widget::{button, column, text, Column, container, row, text_input};
#[derive(Debug, Clone)]
pub enum Message {
ContentChanged(String, usize),
}
#[derive(Default)]
struct App {
content: Vec<String>,
}
struct TextInputs {
title: String,
placeholder: String,
}
impl TextInputs {
pub fn new(title: String, placeholder: String) -> TextInputs {
TextInputs {
title,
placeholder,
}
}
}
impl App {
pub fn view(&self) -> Column<Message> {
// We use a column: a simple vertical layout
let mut col = column![];
for (idx, obj) in vec![
TextInputs::new("Name".to_string(), "Enter your name...".to_string()),
TextInputs::new("Location".to_string(), "Enter your location...".to_string()),
TextInputs::new("Phone".to_string(), "Enter your phone number...".to_string()),
TextInputs::new("Email".to_string(), "Enter your email...".to_string()),
].into_iter().enumerate() {
let row = row!(
text(obj.title),
text_input(obj.placeholder.as_str(), &self.content[idx])
.on_input(move |_| Message::ContentChanged(self.content[idx].clone(), idx)));
col = col.push(row)
}
col.into()
}
pub fn update(&mut self, message: Message) {
match message {
Message::ContentChanged(content, idx) => {
self.content[idx] = content;
}
}
}
}
fn main() -> iced::Result {
iced::run("A cool example", App::update, App::view)
}