[GH-ISSUE #1127] Flex: add a method to remove an item #816

Open
opened 2026-03-04 01:07:56 +03:00 by kerem · 1 comment
Owner

Originally created by @ossenthusiast on GitHub (Oct 11, 2025).
Original GitHub issue: https://github.com/rivo/tview/issues/1127

I wanna add the ability to hide the "sidebar" in my app, currently, I am removing everything and rebuilding the entire layout... it would be awesome if there was a way to remove the primitive from the flex and insert it back later.

Originally created by @ossenthusiast on GitHub (Oct 11, 2025). Original GitHub issue: https://github.com/rivo/tview/issues/1127 I wanna add the ability to hide the "sidebar" in my app, currently, I am removing everything and rebuilding the entire layout... it would be awesome if there was a way to remove the primitive from the flex and insert it back later.
Author
Owner

@MonkeyMatrix commented on GitHub (Oct 19, 2025):

You may be able to use fixedSize/proportion for flex items to help with that.

package main

import (
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

func main() {
	app := tview.NewApplication()

	text := tview.NewTextView().SetText("Hello world!")
	othertext := tview.NewTextView().SetText("Something is right here...")

	flex := tview.NewFlex()
	// Set proportion to 1
	flex.AddItem(text, 0, 1, false)
	flex.AddItem(othertext, 0, 1, false)

	flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Rune() == 'u' {
			// Set proportion to 1
			flex.ResizeItem(text, 0, 1)
		} else if event.Rune() == 'd' {
			// Set proportion to 0
			flex.ResizeItem(text, 0, 0)
		}

		return event
	})

	app.SetRoot(flex, true)
	app.Run()
}

If you run this, you should be able to press 'u' and 'd' to show and hide the text TextView. If you do need to remove an item, RemoveItem does exist. I only need to resize in the above code by setting proportion to 0, but your structure might need a different solution.

<!-- gh-comment-id:3419938843 --> @MonkeyMatrix commented on GitHub (Oct 19, 2025): You may be able to use fixedSize/proportion for flex items to help with that. ``` go package main import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() text := tview.NewTextView().SetText("Hello world!") othertext := tview.NewTextView().SetText("Something is right here...") flex := tview.NewFlex() // Set proportion to 1 flex.AddItem(text, 0, 1, false) flex.AddItem(othertext, 0, 1, false) flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Rune() == 'u' { // Set proportion to 1 flex.ResizeItem(text, 0, 1) } else if event.Rune() == 'd' { // Set proportion to 0 flex.ResizeItem(text, 0, 0) } return event }) app.SetRoot(flex, true) app.Run() } ``` If you run this, you should be able to press 'u' and 'd' to show and hide the `text` `TextView`. If you do need to remove an item, `RemoveItem` does exist. I only need to resize in the above code by setting proportion to 0, but your structure might need a different solution.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#816
No description provided.