How to Change Button Icon to Animated Text in Flutter? - flutter

I am Facing Some Issues on this Topic. I have a Material Button. as you can see in the Code below there is an "Emoticon" icon is Set for it.
MaterialButton(
onPressed: () {},
// color: Colors.transparent,
child: Icon(
Icons.emoji_emotions_outlined,
color: Colors.black54,
size: 35,
),
shape: CircleBorder(),
padding: EdgeInsets.all(16),
),
],
),
I also Have a Text Field which Looks like, this, (below),
TextFormField(
// autofocus: true,
decoration: InputDecoration(
labelText: 'Write Task',
// border: OutlineInputBorder(),
prefixIcon: Icon(Icons.add),
),
),
After the user is done writing I need to Show up this List of emojis [😀,😃,😄,😁,😆,😅,🤣] One by one in the Delay of One Second. inside the Material Button, Place Above, in the place of Icon.
I have a Material Button
I have an Icon Inside it.
I also Have a Text Field
After TextField is Done Writing, I need to Show List of emojis [😀,😃,😄,😁,😆,😅,🤣]
I want to replace the Icon in the Material Button.
And I want to Show a Slide Show of These Emojis Except the Button's Icon
Also If user Clicks on Emoji the Emoji Gets Locked, or Store in Some kind of Variable
I really want to Seek Help at this.

Two things here,
Initially one icon is shown in material button. As per the user's interaction the icon will be changed. To achieve this, create instance of IconData (as Icon accepts IconData) and initially set the default icon. You can set the selected IconData to the variable whenever user selects another icon.
I don't know whether this traditional icons are there in Material Icons library or not. Check font_awesome_flutter otherwise.

Related

How Can I change index of PageView from a Widget inside one of the pages?

My ultimate goal is to animate a widget from one Page to another in a PageView, if you can help with that it would mean a lot. But for now, I can't even change the page by tapping on the widget let alone animating it. I tried a similar approach to this video and initially I was able to make it work but just till the point where I could change the pages from BottomNavigationBar and by swiping the pages (which I could do normally too) but when I tried to apply it to change the values using the Widget, I wasn't able to. It did not give any errors but it simply did nothing. I am unable to share the code now as I tried way too many things and now its a complete mess, Please mention if the code is an important requirement I will rewrite it and attach.
Note: If for the animation part somehow you can give me a way with Hero Widget it would be the best as with or without Hero I am trying to achieve the same animation as Hero would provide. I have only seen Hero being used with pictures, I don't know if other widgets are possible.
I tried the solution mentioned in this StackOverflow answer it works but it opens a new page for me on top of the current page. This defeats the purpose I want to later incorporate which is animating the widget. Or if you can give me a way to solve it with that method will also be helpful.
These below are some screenshots of the UI of the app and I have mentioned what I want to achieve in the description of the problem.
Home Widget Page (Source) "home_widget.dart" contains the code for this.
TO
Search Widget Page (Destination) "search_widget.dart" contains the code for this.
These are 2 Pages in the PageView of my "home.dart" file
I want to tap on the search bar in "Home Widget Page" which will change the index of my BottomNavigationBar and also my PageView Controller so that the "Search Widget Page" shows.
EDIT 1:-
File/Class Structure of my project:-
lib
screens
home.dart [code for the homepage contains the PageView and
the BottomNavBar]
widgets
home_widget.dart [code for the First Page for PageView]
search_widget.dart [code for the Second Page for PageView]
The BottomNavBar and PageView are connected together which switch page together in the home.dart file and I want to tap the "Search bar" inside home_widget.dart and the Page index in home.dart should be updated to now render search_widget.dart
You need to give access that page to manipulate your page and tab controllers. Generally they will be passed as a parameter. Or You can declare them as static for StatefulWidget(or Stateless) class NOT STATE CLASS
Use tab bar in bottom nav bar and pass the same tab controller there
bottomNavigationBar: Card(
margin: EdgeInsets.zero,
elevation: 20,
child: TabBar(
controller: _controller,
isScrollable: false,
labelColor: Colors.blue,
unselectedLabelColor: Colors.grey,
indicator: const BoxDecoration(),
labelStyle: AppTextStyle.kTitleMedium,
labelPadding: const EdgeInsets.only(bottom: 20, top: 10),
indicatorSize: TabBarIndicatorSize.label,
padding: EdgeInsets.zero,
indicatorColor: AppColors.kWhiteColor,
automaticIndicatorColorAdjustment: false,
onTap: homeBloc.togglePageOnIndex,
tabs: const [
Tab(
icon: Icon(
Icons.home,
size: 32,
),
iconMargin: EdgeInsets.symmetric(vertical: 4),
text: 'home',
),
Tab(
icon: Icon(
Icons.search,
size: 32,
),
iconMargin: EdgeInsets.symmetric(vertical: 4),
text: 'search',
),
]),
),

Flutter ElevatedButton shape - rectangle

This should be the easiest one ever.
I am coming back to flutter after years away as I am forced to update my old app. Everything has changed!
I am trying to make an ElevatedButton that is square. The default has slightly rounded edges that looks weird when I am making a button the width of the screen.
All I want to know is how I can make this button a square. That's it! No tricks!
ElevatedButton has:
ElevatedButton.styleFrom(
shape: ???,
),
But I can't find any information anywhere online what my options are for shape. Only a few examples on how to make it round or beveled or literally every other shape you can think of except for a simple rectangle :D
Try below code:
ElevatedButton, ButtonStyle, BorderRadius
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
//borderRadius: BorderRadius.zero, //Rectangular border
),
),
onPressed: () {},
child: const Text(
'Submit',
),
),
Result->

How to make a button in Flutter have the same shape as its icon?

I am trying to create a backspace button (using an ElevatedButton()). I don't want the button to be circular, but instead have the same shape as the icon. Also, when tapping the button, I want the splash effect to be the same size and shape as the button.
I've attached a reference image below of the shape I'm trying to replicate.
Extra challenge: I'm also trying to set the fill color of the button to be black and the color of the button to be grey (like the example).
You can use the IconButton() widget instead of ElevtaedButton() as well as defining the splashRadius to change the size of the splash effect:
IconButton(
splashRadius: 1, // Set the Size of the splash area
color: Colors.grey,
icon: Icon(Icons.backspace),
onPressed: () {},
),
Result:
Or, if you want to use ElevatedButton(), use the .icon constructor:
ElevatedButton.icon(
style: ElevatedButton.styleFrom(primary: Colors.grey.shade300),
label: Text('Hello World'),
icon: Icon(
Icons.backspace,
color: Colors.grey,
),
onPressed: () {},
)
Result:
There are many default icons in class Icons are not good-looking for your app. You can use some design platform, such as Figma, then download it as svg. Then the code could be:
InkWell(
onTap: () {},
child: SvgPicture.asset(path_to_svg_icon)
)
This way, you can edit color, shape, style... for your icon. Good luck!

Flutter avoid focusing on TextField when clicking button inside

I have a widget that looks like this:
The code looks like this:
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter your address or ZIP code',
hintStyle: TextStyle(fontSize: 18),
suffixIcon: IconButton(
icon: Icon(Icons.gps_fixed),
onPressed: () {
...
},
),
),
),
The behavior I want is that when the user clicks on the GPS button, instead of having the keyboard pop up, the TextField should become temporarily read-only and be filled with the current address after we get a response from some API.
The problem is, the way I wrote the code causes the TextField to gain focus when the GPS button is clicked. The click handler then disables the TextField to do async query, which causes the keyboard to disappear immediately. This creates a very janky animation.
Is there a way to make the TextField NOT gain focus when the GPS button is clicked?
This suffixIcon for the textfield is a part of it, If you click the suffixIcon, it's also performing click on the textfield. This is the reason why you are getting your keyboard poped up.
Solution:
Wrap your textfield into a Stack, and make sure IconButton is the last child of it. This performs click on your IconButton instead of gaining focus in textfield.
Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextField(),
IconButton(
icon: Icon(Icons.add),
onPressed: () {
},
),
],
)
It is a known bug, issue has ben opened on github , and closed due to inactivity... You can take a look at that issue, there are some workarounds posted by users, they mainly revolve around stacks. I'm guessing that is where Suman's answer stems from :).
I propose a different approach, simply separate the button from the TextField.
Something like this:
Row(
children: <Widget>[
Expanded(
flex: 1,
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter your address or ZIP code',
hintStyle: TextStyle(fontSize: 18),
),
)),
Expanded(
flex: 0,
child: IconButton(
icon: Icon(Icons.gps_fixed),
tooltip: 'Get a gpx fix',
onPressed: () {
print("do something");
}),
),
],
)
);
I personally prefer to have the button outside the text field because IMHO it conveys that it is a button more so than when having it inside. When inside it looks like it is just an icon that describes the field. Anyhow that is just personal preference, you can add border around both elements if that is what you prefer by wrapping both in a container and placing the decoration on the container.

How to fill color in the icon in flutter

I am new in flutter. I am using multiple theme(i.e. dark mode) in the app. So, When we use icon in different theme, automatically take background color according to the theme. I want background color of theme but not inside the icon.
Example:
I am using youtube's icon in dark theme so look like below,
But i want to like below,
I am using
Icon(
FontAwesomeIcons.youtube,
color: Colors.red
)
So how to fill the color white in this icon ? (Or also you can suggest me to do that in proper as well as better way to implement)
(so, i can use white filled icon in every theme)
You can use a Stack to place a filled Container under your icon like so:
Stack(children: <Widget>[
Positioned.fill(
child: Container(
margin: EdgeInsets.all(5), // Modify this till it fills the color properly
color: Colors.white, // Color
),
),
Icon(
FontAwesomeIcons.youtube, // Icon
color: Colors.red,
),
),
])
Since its a container, you can also modify its shape in case there are random icon shapes that a normal square does not help in :P
I tried filling the icon play_circle_filled with green using this on DartPad and it gave me this:
I also faced the same issue. I think there could be a modification in Sidak's solution. Instead of using a container in the background, we could use the same Icon of the desired color in the background using the stack widget.
Stack(children: <Widget>[
Positioned.fill(
child: Container(
margin: EdgeInsets.all(5), // Modify this till it fills the color properly
color: Colors.white, // Color
),
),
Icon(
FontAwesomeIcons.youtube, // Icon
color: Colors.red,
),
),
])