Rust + Iced: How do I place a border around a grid widget

⚓ Rust    📅 2026-04-02    👤 surdeus    👁️ 5      

surdeus

I have a function that creates a view. In the view I display data in a grid. It works but I am unable to figure out how to place a border around each grid element so it looks like a spreadsheet. I would try another widget if required. Any assistance will be appreciated. Below is the function.

pub fn closing_view<'a>(heading: &str, query_results: &'a Vec<QueryResults>) -> iced::Element<'a, Message> {

    let mut grid_height = 0.0; 
    let mut my_grid = Grid::new();

    my_grid = my_grid
        .columns(2)
        .spacing(15)  // want 25 horizontal 0 vertical spacing between cells, default = 0
        .width(260) 
        ;

    for result in query_results {

        let naive_date = NaiveDate::parse_from_str(&result.date, "%Y-%m-%d").unwrap();
        let formatted_date = naive_date.format("%b %e, %Y").to_string();

        my_grid = my_grid
            .push(widget::text(format!("{}", formatted_date)).size(20.0).align_x(Horizontal::Right))
            .push(widget::text(format!("${:.2}", result.close)).size(20.0).align_x(Horizontal::Left));
        grid_height += 38.0
        
    }

    my_grid = my_grid.height(Length::Fixed(grid_height));

    let column_widget = 

        widget::column![
    
            widget::container(
                widget::button(widget::text("Home").size(15.0))
                    .on_press(Message::GoToHomeScreen)
                    .style(btn_blue_no_border)
            )
                .width(Length::Fill)
                .align_x(Horizontal::Right)
                .align_y(Vertical::Top),


            widget::container(
                widget::text(format!("{}", heading)).size(30.0)
            )
            .width(Length::Fill)
            .padding(iced::padding::bottom(10))
            .align_x(Horizontal::Center)
            .align_y(Vertical::Center), 

            widget::container(
                widget::row![
                    widget::text("Date").size(20.0),
                    widget::text("Close").size(20.0),
                ]
                .width(Length::Fill)
                .spacing(65)
                .padding(iced::padding::top(20))
                .padding(iced::padding::left(210))
            )
            .width(Length::Fill)
            .align_x(Horizontal::Center), 

            widget::rule::horizontal(1)
                .style(horizontal_rule),
        
            widget::scrollable(
                widget::container(
                    my_grid
                ) // end of container
                .padding(iced::padding::top(9))
                .width(Length::Fill)
                .align_x(Horizontal::Center)
                .align_y(Vertical::Top)
            ) // end of scrollable
        ];

    let my_container: widget::Container<'_, Message> =
        widget::Container::new(column_widget)
            .width(Length::Fill)
            .height(Length::Fill)
            .style(container_style);

    my_container.into()

}

2 posts - 2 participants

Read full topic

🏷️ Rust_feed