A toggle button allows the user to change a setting between two states. You can add a basic toggle button to your layout with the ToggleButton object. A sample video is given below to get an idea about what we are going to do in this article.
Demo Video:
Syntax
List Of Booleans to Know Which Selected:
3 Represent Number Of Items, 3 Items Set To Not Selected(False).
main is the principal method called once the program is loaded.
Once the Program Is Loaded, runApp Will Run And Call The Class That We Created(ToggleButtonRun) To Be Run.
This Class Is a Stateful Widget As the User Is Enabling And Disabling the Button And the Effect Taken On the Text.
Creating a State Class ToggleButtonRunState That Extends Its Main State From Class ToggleButtonRun.
Creating List Variable selections, taking 3 bool Variable Set To False (Bold -> false, Italic->false, Underline -> false).
Creating Variable TxtBold Used To Set Text Between Bold If Button Toggled And Normal If Not.
Creating Variable TxtItalic Used To Set Text Between Italic If Button Toggled And Normal If Not.
Creating Variable TxtUnder Used To Set Text Underlined If Button Toggled And None If Not.
As Flutter Is Based On widgets, A Widget Must Be Built.
Creating a MaterialApp That Allows Us To Set the App Title, Taking the Scaffold As a Home.
Scaffold Allow Us to Set the AppBar And Body Of The Page.
As An AppBar, It is a Simple Title With Background Color And Foreground Color.
As An Body, It Takes Column Layout (Elements Represented Each On-Line).
First Row Take ToggleButtons That Take Items As Children (Icons), isSelected Take A List Of bool Created Up That Initialize All Buttons To False (Not Selected) When Each Pressed We Switch It From False to True Set By (Inverse) And Checking Index And If Selected Then Putting Effect If Not Removing Effect To Normal.
Set The Color Of The ToggleButton Icon When Selected, fillColor Set BackgroundColor Of The Toggle Button When Selected.
Second Row Take Text Where Style Taken By Variables That Switch When Button Is Toggled (Bold To Normal Par Example..).