Flutter disable and enable buttons - flutter

i had made a function that enabled the second button when i press the first one, everything is working fine but when the second button is enabled i want to go to another page when i press on the second one but i can t do that, how can I reach that. Here is my code:
child: MaterialButton(
elevation: 0,
// ignore: avoid_returning_null_for_void
onPressed: isDisable ? () => null : clickButton(),
child: Text(
'Done',
style: TextStyle(
fontSize: 16.sp, fontWeight: FontWeight.w400),
),
),
I want here on pressed to go to another page but i cant in this form how can i reach that? Thank in advance
I've tried with setState

You like provide null directly instead of method body. Use like.
onPressed: isDisable ? null:() => clickButton(),
Or can be
onPressed: isDisable ? null: clickButton, //depend on `clickButton`
This way, you will be able to see the disable state on ui.

Related

Need Some Help Updating Persistent Footer Buttons in Flutter App

I'm using four persistentFooterButtons in my app to (1) turn on BLE scanning, (2) turn off BLE scanning, (3) disconnect BLE and (4) run a command sequence to join my bluetooth device to a WiFi network (I'm also using Flutter Reactive BLE). Here's an example of my button code:
Tooltip(
message: 'Turn BLE scanning on',
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black,
foregroundColor: Colors.white,
),
onPressed: (scanning || running)
? null
: () {
if (connected) {
disconnect();
connected = false;
}
startScan();
},
child: scanning
? const Icon(Icons.bluetooth_searching)
: const Icon(Icons.bluetooth),
),
As you can see, I'm also using ternary expressions in my button code to perform some additional UI operations. The WiFi joining process involves some navigation through a handful of screens. At the very end of the process, I'd like to send the user back to the starting page which has the widget tree for the persistentFooterButtons.
I send the user back via a button press:
SizedBox(
width: 100.0,
height: 50.0,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size(100.0, 30.0),
),
onPressed: (userMessage.contains('Success'))
? () {
Navigator.pushNamed(
context,
MyHomePage.routeName,
arguments: MyHomePageArguments(
resumeProcess,
connected,
scanning,
running,
statusUpdateAll,
statusUpdateVisibility,
wifiData,
wifiCredentials,
),
);
}
: null,
child: const Text('Home')),
),
I'm passing the arguments that are used in the ternary expressions that represent the state of the buttons. Since I'm wanting to update these buttons in the UI, I should be using setState but I don't know how to implement it in this use case. Can anyone tell me how to do this? Thank you.

How can I change an Icon in flutter when i add a new Entry in my Database?

So i set up a working Realtime Database, to which i can add entries. Now i want to notify the user whenever a new entry was added to the Database by adding a little Red Dot to the Icon in the BottomNavigationBar which disappears again when the user has clicked it. Any ideas on how to achieve this?
I recommend using the badge package to achieve this. You will have to ensure the state updates using your own state management. If the user goes to the notification screen, you will need to trigger an update to mark notifications as read.
return Badge(
showBadge: unreadNotifications > 0,
badgeContent: Text(
'$unreadNotifications',
style: kTextStyle,
),
position: const BadgePosition(
top: -3,
end: 3,
),
child: IconButton(
icon: const Icon(
Icons.notifications,
),
onPressed: () {
Navigator.pushNamed(context, NotificationScreen.id);
},
),
);

In Flutter, how to enable and disable a button by clicking on another button?

I have two elevated buttons inside a Row widget. When I press Button 1, I want to disable Button 2 and when I press Button 1 again I want to enable Button 2. I have searched everywhere but I can't seem to find an answer. I am hoping someone can point me in the right direction.
You could use onPressed : null for disabled button 2.
Use boolean variable for set disable and enable.
var isDisable=true;
This is the button 1 widget and it uses setButton() method for enable and disable button 2.
//button 1
RaisedButton(
padding: const EdgeInsets.all(20),
textColor: Colors.white,
color: Colors.green,
onPressed: (){
setState((){
setButton();
});
},
child: Text('Button 1'),
)
This is the button 2 widget and we check the boolean variable ( isDisable) to check the enable and disable state in button 2.
// button 2
RaisedButton(
padding: const EdgeInsets.all(20),
textColor: Colors.white,
color: Colors.green,
onPressed: isDisable
? () => null : clickButton(),
child: Text('Button 2'),
)
This is a method that uses to change boolean variable value for enable and disable button 2.
//Method for enable and disable button2
void setButton(){
if(isDisable){
isDisable = false;
}else{
isDisable = true;
}
}
Hope this will solve your problem!

Flutter : How to create NEXT button from JSON data?

So, here is my detailDoaPage.dart that shown detailed item from a ListView.builder (based on my JSON local data) in previous page. In this detailed page, I want to create a button (Next button below) that change all attributes of this current data into next item. So the page isn't navigating, just changed the item inside it into the next data.
Anyone can help me to give me some advices for this problem? Here's my full code if you want to see it https://replit.com/join/nlmxirvq-nabilrei
Thank you :)
Your onPressed callback method in the button is empty, you need to implement it in order to trigger state change when the button is pressed.
Something like this:
Padding(
padding: const EdgeInsets.all(28),
child: Align(
alignment: Alignment.bottomRight,
child: FloatingActionButton.extended(
onPressed: () {
// here you would implement your logic,
// whether be it calling an Api to fetch data,
// or do some action on your data..Etc.
_functionToTriggerStateChange();
},
backgroundColor: Colors.white,
icon:
Icon(Icons.arrow_forward_ios_rounded, color: Colors.teal),
splashColor: Colors.teal.withOpacity(0.3),
label:
Text("Next", style: Style(styleColor: Colors.teal).body),
),
),
)

flutter: Bug? only FlatButton but not CupertinoButton showing Text

Did you ever experience this? Does it make sense to register this as a bug and how would I do this?
For one of my screens I have now this situation for the 2nd time: I insert a CupertinoButton with Text as a child, however, the text does not show up. In the 1st occurrence, 2 pixels of text appeared to be rendered (I saw 2 dots). I have also copied a CupertinoButton from another line and left it unchanged. No change, text does not appear. If I then only change the widget name from CupertinoButton to FlatButton (leaving everything else untouched), the text appears.
Since CupertinoButtons are typically working for me, I guess I would have to submit the whole project for a bug report, since isolating the issue does not to be appear like a straight forward job.
Please consider adding padding like this:
CupertinoButton(
padding: EdgeInsets.symmetric(vertical: 0, horizontal: 5),
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(12)),
child: Text(
'Login',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.white),
),
onPressed: () => {},
),