Change the content of dialog in Flutter - flutter

After I clicked the button in the dialog, the content would change from a list of buttons into a form with textfields. However, I am frustrated in codes to achieve it.

You just need a stateful dialog and a bool state. Here is an example:
class MyDialog extends StatefulWidget {
const MyDialog({Key? key}) : super(key: key);
#override
MyDialogState createState() => MyDialogState();
}
class MyDialogState extends State<MyDialog> {
/// When this value is false, it shows list of buttons
/// When this value is true, it shows list of textfields
bool isForm = false;
#override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('AlertDialog Title'),
// Here, we conditionally change content
content: isForm
? Column(
children: [
TextFormField(),
TextFormField(),
TextFormField(),
TextFormField(),
],
)
: Column(
children: [
TextButton(onPressed: () {}, child: Text('Button')),
TextButton(onPressed: () {}, child: Text('Button')),
TextButton(onPressed: () {}, child: Text('Button')),
TextButton(onPressed: () {}, child: Text('Button')),
],
),
actions: [
TextButton(
// Here isForm is switched to change the content
onPressed: () => setState(() => isForm = !isForm),
child: const Text('Switch'),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('OK'),
),
],
);
}
}
Use it with showDialog(context: context, builder: (context) => MyDialog())

Related

How can I change IOS and Android settings in Flutter?

I'm working on a drawing app that uses Apple Pad and Stylus.
In order to use stylus smoothly in this app, it must disable scribble option in the stylus setting.
If so, how do I access the settings in the IOS or Android on the setting and change the settings options?
And can I check what the current settings value are before I access setting? For example, can I check if the scribble in stylus setting is enabled?
Check out this package:
https://pub.dev/packages/app_settings/example
import 'dart:async';
import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
/// Main method to return runApp.
void main() => runApp(MyApp());
/// This is the main app stateful widget.
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
/// This is the app state.
class _MyAppState extends State<MyApp> {
#override
void initState() {
/// Call out to intialize platform state.
initPlatformState();
super.initState();
}
/// Initialize platform state.
Future<void> initPlatformState() async {
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
}
/// Widget build method to return MaterailApp.
#override
Widget build(BuildContext context) {
var actionItems = getListOfActionButtons();
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('App Settings Example App'),
),
body: GridView.count(
crossAxisCount: 2,
childAspectRatio: 2,
children: List.generate(actionItems.length, (index) {
return Center(
child: ButtonTheme(
colorScheme: ColorScheme.dark(),
minWidth: 150.0,
child: actionItems[index],
));
}))));
}
List<Widget> getListOfActionButtons() {
var actionItems = <Widget>[];
actionItems.addAll([
ElevatedButton(
child: Text("WIFI"),
onPressed: () {
AppSettings.openWIFISettings();
},
),
ElevatedButton(
child: Text("Location"),
onPressed: () {
AppSettings.openLocationSettings();
},
),
ElevatedButton(
child: Text("Security"),
onPressed: () {
AppSettings.openSecuritySettings();
},
),
ElevatedButton(
child: Text("Lock & Password"),
onPressed: () {
AppSettings.openLockAndPasswordSettings();
},
),
ElevatedButton(
child: Text("App Settings"),
onPressed: () {
AppSettings.openAppSettings();
},
),
ElevatedButton(
child: Text("Bluetooth"),
onPressed: () {
AppSettings.openBluetoothSettings();
},
),
ElevatedButton(
child: Text("Data Roaming"),
onPressed: () {
AppSettings.openDataRoamingSettings();
},
),
ElevatedButton(
child: Text("Date"),
onPressed: () {
AppSettings.openDateSettings();
},
),
ElevatedButton(
child: Text("Display"),
onPressed: () {
AppSettings.openDisplaySettings();
},
),
ElevatedButton(
child: Text("Notification"),
onPressed: () {
AppSettings.openNotificationSettings();
},
),
ElevatedButton(
child: Text("Sound"),
onPressed: () {
AppSettings.openSoundSettings();
},
),
ElevatedButton(
child: Text("Internal Storage"),
onPressed: () {
AppSettings.openInternalStorageSettings();
},
),
ElevatedButton(
child: Text("Battery optimization"),
onPressed: () {
AppSettings.openBatteryOptimizationSettings();
},
),
ElevatedButton(
child: Text("NFC"),
onPressed: () {
AppSettings.openNFCSettings();
},
),
ElevatedButton(
child: Text("VPN"),
onPressed: () {
AppSettings.openVPNSettings(
asAnotherTask: true,
);
},
),
ElevatedButton(
child: Text("Device Settings"),
onPressed: () {
AppSettings.openDeviceSettings(
asAnotherTask: true,
);
},
),
ElevatedButton(
child: Text("Accessibility"),
onPressed: () {
AppSettings.openAccessibilitySettings(
asAnotherTask: true,
);
},
),
ElevatedButton(
child: Text("Developer"),
onPressed: () {
AppSettings.openDevelopmentSettings(
asAnotherTask: true,
);
},
),
ElevatedButton(
child: Text("Hotspot"),
onPressed: () {
AppSettings.openHotspotSettings(
asAnotherTask: true,
);
},
),
]);
return actionItems;
}
/// Dispose method to close out and cleanup objects.
#override
void dispose() {
super.dispose();
}
}

Display Widget onPressed button Flutter

My goal is to display certain widgets based on clicks in a menu.
What I need when I click a title, is to shows a certain widget.
Here is the full code:
class SideMenu extends StatelessWidget {
const SideMenu({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Drawer(
child: SingleChildScrollView(
// it enables scrolling
child: Column(
children: [
DrawerHeader(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: defaultPadding * 3,
),
Image.asset(
"assets/logo/logo_icon.png",
scale: 5,
),
SizedBox(
height: defaultPadding,
),
Text("Récolt"),
],
)),
DrawerListTile(
title: "Tableau de bord",
svgSrc: "assets/icons/menu_dashbord.svg",
press: () {},
),
DrawerListTile(
title: "Utilisateurs",
svgSrc: "assets/icons/menu_profile.svg",
press: () {},
),
DrawerListTile(
title: "Collaborateurs",
svgSrc: "assets/icons/menu_doc.svg",
press: () {},
),
DrawerListTile(
title: "Producteurs",
svgSrc: "assets/icons/menu_store.svg",
press: () {},
),
DrawerListTile(
title: "Paramètres",
svgSrc: "assets/icons/menu_setting.svg",
press: () {},
),
],
),
),
);
}
}
I've got a extern page for display my widget like this :
SizedBox(height: defaultPadding),
ListUserEntreprise(),
SizedBox(height: defaultPadding),
ListUserCollab(),
I don't know how to do it, I tried with using a boolean in onPressed but it didn't work, I don't know really how to declare it and use it with an external page. Can someone help me, please?
use StatefulWidget and try with a boolean with the onPressed, setState() to update UI
Example:
import 'package:flutter/material.dart';
class SideMenu extends StatefulWidget {
const SideMenu({
Key? key,
}) : super(key: key);
#override
State<SideMenu> createState() => _SideMenuState();
}
class _SideMenuState extends State<SideMenu> {
bool _isShow = false;
#override
Widget build(BuildContext context) {
return Drawer(
child: SingleChildScrollView(
// it enables scrolling
child: Column(
children: [
DrawerListTile(
title: "Tableau de bord",
svgSrc: "assets/icons/menu_dashbord.svg",
press: () {
setState(() {
_isShow = !_isShow;
});
},
),
Visibility(
visible: _isShow,
child: Container(
child: Text('Tableau de bord'),
))
],
),
),
);
}
}

Flutter save page content after routing

i have a favorite icon that exist on page (A) that changes it's color whenever i click, the problem is when i move to another page and go back to page (A) icon goes to regular color, it doesn't save the changes on page , is there a way to keep the changes even after moving from page to page ?
Thanks.
If you push and pop back, then you can find the saved state from the route. How-ever if you pop back, current page state will be removed from stack. To handle this, you need to use state-management property. You can use StateProvider in this case, or the thing that suit for your project.
Demo:
class PageA extends StatefulWidget {
PageA({Key? key}) : super(key: key);
#override
_PageAState createState() => _PageAState();
}
class _PageAState extends State<PageA> {
Color color = Colors.deepOrange;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("PAge A"),
),
body: Center(
child: Column(
children: [
Container(
width: 400,
height: 400,
color: color,
alignment: Alignment.center,
child: Text("init Color:deepOrange "),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => PageB(),
));
},
child: Text("Move to B"),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
color = Colors.deepPurple;
});
},
),
);
}
}
class PageB extends StatefulWidget {
const PageB({Key? key}) : super(key: key);
#override
_PageBState createState() => _PageBState();
}
class _PageBState extends State<PageB> {
Color color = Colors.greenAccent;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("PAge B"),
),
body: Center(
child: Column(
children: [
Container(
width: 400,
height: 400,
color: color,
alignment: Alignment.center,
child: Text("init Color:greenAccent "),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PageC(),
),
);
},
child: Text("Move to C"),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Move Back to A, you can find last saved state"),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
color = Colors.black;
});
},
),
);
}
}
class PageC extends StatefulWidget {
PageC({Key? key}) : super(key: key);
#override
_PageCState createState() => _PageCState();
}
class _PageCState extends State<PageC> {
Color color = Colors.amberAccent;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("PAge C"),
),
body: Center(
child: Column(
children: [
Container(
width: 400,
height: 400,
color: color,
child: Center(child: Text("init Color:amberAccent ")),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PageB(),
),
);
},
child: Text("Push to B, will assing new state on route"),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
"pop to B\n if you pop then current state of Page B will be visible"),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
color = Colors.pink;
});
},
),
);
}
}

Flutter: Passing Data between Classes Resets the Other Value

I try to pass sRegions value from Regions class to the AddAd class
and passing sCategory value from Categories class to AddAd class
but when I click and pass this value , it resets the other value to initial value
I want to the values inside AddAd class to be separated and do not affect each other
Here is AddAd() class:
class AddAd extends StatefulWidget {
final String sRegion;
final String sCategory;
AddAd(this.sRegion,this.sCategory, {Key key}):super(key: key);
#override
_AddAdState createState() => _AddAdState();
}
ListTile(
title: Text("Location"),
subtitle: widget.sRegion == null ? Text("Where exactly") : Text(widget.sRegion),
),
ListTile(
title: Text("Category"),
subtitle: widget.sCategory == null ? Text("Department") : Text(widget.sCategory),
),
and here is how I pass data from Regions class:
child: Text(item),
onPressed: () {
sRegion = item;
Navigator.push(context, new MaterialPageRoute(builder: (context) => AddAd(sRegion, sCategory)));
print(sRegion);
},
and here how I pass data from Categories class:
class _RegionsState extends State<Regions> {
String sRegion;
String sCategory;
#override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(
title: Text("Location"),
actions: <Widget>[
FlatButton(
child: Text('Cancel', style: TextStyle(color: Colors.white),),
onPressed: (){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=>AddAd(sRegion, sCategory)));
},
)
],
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(top:15.0),
child: Container(
child: Column(
children: <Widget>[
for(var item in regions) FlatButton(
child: Text(item), onPressed: (){
sRegion = item;
Navigator.push(context, new MaterialPageRoute(builder: (context) => AddAd(sRegion, sCategory)));
print(sRegion);
},),
Divider(),
],
),
),
),
),
),
);
}
}

flutter alert dialog for updating value

I want to update value in alert dialog i am using following:
Future showAlert() async {
await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Alert in Dialog'),
content: Container(
height: 40.0,
width: 60.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_itemCount != 1
? new IconButton(
icon: new Icon(Icons.remove),
onPressed: () => setState(() => _itemCount--),
)
: new Container(),
new Text(_itemCount.toString()),
new IconButton(
icon: new Icon(Icons.add),
onPressed: () => setState(() => _itemCount++))
],
),
),
actions: <Widget>[
new FlatButton(
child: new Text('CANCEL'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
I am calling this function in Listview Builder
GestureDetector(
child: Align(
alignment: Alignment.topRight,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Icon(Icons.add_box,
color: Color(0xFFfccd01)),
),
),
onTap: () {
showAlert();
/* FutureBuilder<String>(
future: Add_to_cart(USER_ID,
snapshot.data[index].id, "1"),
builder: (context, snapshot) {
print(snapshot.data);
});*/
},
),
But on + click my value of alert dialog is not going to update after i close and reopen it it will update but i want to update on tap of icon button.
You can use StreamBuilder to update within Dialog or that screen also on single event thru Streams
You must insert AlertDialog into Statefulll Widget
see this example:
class ShowDialog extends StatefulWidget {
#override
_ShowDialogState createState() => _ShowDialogState();
}
class _ShowDialogState extends State<ShowDialog> {
#override
Widget build(BuildContext context) {
return AlertDialog(
//your container
);
}
}
and call ShowDialog into showDialog()