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'),
))
],
),
),
);
}
}
Related
I don't know what it's wrong with my code, PopupMenuButton it's not working,
I have run the flutter update, yet, it seems not to be working.
Below is my code
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:oga_bliss/screen/add_prop_page.dart';
import '../widget/notice_me.dart';
import '../widget/property_app_bar.dart';
import '../widget/property_tile_widget.dart';
enum SampleItem { itemOne, itemTwo, itemThree }
class ProductPropertyPage extends StatefulWidget {
const ProductPropertyPage({Key? key}) : super(key: key);
#override
State<ProductPropertyPage> createState() => _ProductPropertyPageState();
}
class _ProductPropertyPageState extends State<ProductPropertyPage> {
final GlobalKey _menuKey = GlobalKey();
SampleItem? selectedMenu;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
const PropertyAppBar(title: 'Property'),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
NoticeMe(
title: 'Oops!',
desc: 'Your bank account is not yet verify!',
icon: Icons.warning,
icon_color: Colors.red,
border_color: Colors.red,
btnTitle: 'Verify Now',
btnColor: Colors.blue,
onTap: () {},
),
PropertyTileWidget(
props_image_name:
'https://ogabliss.com/project_dir/property/45164d94bc96f243362af5468841cd44.jpg',
props_name: 'Newly Built 3 Bedroom flat',
props_desc:
'this newly built bedroom flat its covered with the latest and later type of euqipment that will keep your day going well',
props_price: '90000000000000',
props_type: 'Buy',
props_bedroom: '100',
props_bathroom: '290',
props_toilet: '100',
onTap: () {
_popUpBUtton();
},
),
PropertyTileWidget(
props_image_name:
'https://ogabliss.com/project_dir/property/45164d94bc96f243362af5468841cd44.jpg',
props_name: 'Newly Built 3 Bedroom flat',
props_desc:
'this newly built bedroom flat its covered with the latest and later type of euqipment that will keep your day going well',
props_price: '90000000000000',
props_type: 'Rent',
props_bedroom: '100',
props_bathroom: '290',
props_toilet: '100',
onTap: () {
_popUpBUtton();
},
),
],
),
),
)
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Get.to(
() => AddPropertyPage(),
transition: Transition.upToDown,
);
},
child: const Icon(Icons.add),
),
);
}
Widget _popUpBUtton() => PopupMenuButton<String>(
enabled: true,
itemBuilder: (context) {
return [
const PopupMenuItem(
child: Text('Submit'),
value: "One",
),
const PopupMenuItem(
child: Text('Edit'),
value: "Two",
),
const PopupMenuItem(
child: Text('Delete'),
value: "Three",
),
];
},
);
}
You can't open any widget by tapping another widget ... instead of trying this
#override
Widget build(BuildContext context) {
return Column(
children: [
_popUpBUtton(),
]); }
Widget _popUpBUtton() => PopupMenuButton<String>(
child: PropertyTileWidget(
props_image_name:
'https://ogabliss.com/project_dir/property/45164d94bc96f243362af5468841cd44.jpg',
props_name: 'Newly Built 3 Bedroom flat',
props_desc:
'this newly built bedroom flat its covered with the latest and later type of euqipment that will keep your day going well',
props_price: '90000000000000',
props_type: 'Buy',
props_bedroom: '100',
props_bathroom: '290',
props_toilet: '100',
),
itemBuilder: (context) {
return [
const PopupMenuItem(
value: "One",
child: Text('Submit'),
),
const PopupMenuItem(
value: "Two",
child: Text('Edit'),
),
const PopupMenuItem(
value: "Three",
child: Text('Delete'),
),
];
},
);
}
i have the following simple full code
import 'dart:developer';
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
const Test({Key? key}) : super(key: key);
#override
State<Test> createState() => _TestState();
}
class _TestState extends State<Test> {
List myListWidget = [];
late bool isColorWhie = false;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){
setState(() {
myListWidget.add(
Container(
width: 50,
height: 50,
color: isColorWhie?Colors.white:Colors.red,
)
);
});
},
child: Scaffold(
backgroundColor: Colors.green,
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
...myListWidget,
TextButton(
onPressed: (){
setState(() {
isColorWhie = !isColorWhie; // here never update
log('done');
});
},
child: const Text('tab to Change color',style: TextStyle(color: Colors.white),)
)
],
),
),
),
);
}
}
i tap on any point on screen to add Container into myListWidget thn call setState(() {}); to update ui.
everything fine now but when i change the isColorWhie to true it should change the color to white but it never update !
i am totally confused why it does not update ? And how could i handle with this ?
For base color change, I am using a separate button, also switching the list value.
One thing variable does update the UI, you need to handle state inside the item(state-management property) or reinitialize the variable to get update state.
class Test extends StatefulWidget {
const Test({Key? key}) : super(key: key);
#override
State<Test> createState() => _TestState();
}
class _TestState extends State<Test> {
List<bool> myListWidgetState = [];
bool isColorWhie = false;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
setState(
() {
myListWidgetState.add(isColorWhie);
},
);
},
child: Scaffold(
backgroundColor: Colors.green,
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
...myListWidgetState.map(
(e) {
return Container(
width: 50,
height: 50,
color: e ? Colors.white : Colors.red,
);
},
),
TextButton(
onPressed: () {
myListWidgetState = myListWidgetState.map((e) => !e).toList();
setState(() {});
print(isColorWhie);
},
child: const Text(
'tab to Change color',
style: TextStyle(color: Colors.white),
),
),
TextButton(
onPressed: () {
setState(() {
isColorWhie = !isColorWhie;
});
print(isColorWhie);
},
child: const Text(
'tab to Change base color',
style: TextStyle(color: Colors.white),
),
),
],
),
),
),
);
}
}
Since you create a container as an object in GestureDetector and save it to your list, it will not change. It is now permanently saved (of course as long as you do not delete the element) as an entry in your list.
Your logic works exactly as you programmed it. For example, if you were to recompile the app and press the TextButton and then anywhere on your screen, a white container would also appear.
If you want to dynamically change the color of all containers at once, then you can do the following:
class _TestState extends State<Test> {
int containerCounter = 0;
late bool isColorWhie = false;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
setState(() {
containerCounter++;
});
},
child: Scaffold(
backgroundColor: Colors.green,
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 50,
child: ListView.builder(
shrinkWrap: true,
itemCount: containerCounter,
itemBuilder: ((context, index) {
return Container(
height: 50,
color: isColorWhie ? Colors.white : Colors.red,
);
}),
),
),
TextButton(
onPressed: () {
setState(() {
isColorWhie = !isColorWhie; // here never update
});
},
child: const Text(
'tab to Change color',
style: TextStyle(color: Colors.white),
))
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
class CounterScreen extends StatefulWidget
{
const CounterScreen({Key? key}) : super(key: key);
#override
State<CounterScreen> createState() {
return _CounterScreenState();
}
}
class _CounterScreenState extends State<CounterScreen> {
int number=1;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Counter"
),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(onPressed: (){ number--;} , child: Text("Minus",)),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Text("$number",style: TextStyle(color: Colors.black,fontSize: 30,),),
),
TextButton(onPressed: (){ number++;print(number);}, child: Text("Plus",)),
],
),
),
);
}
}
you have to use setstate where you want to update the UI. so this line should be
TextButton(onPressed: (){ number--;} , child: Text("Minus",)),
Like this
TextButton(
onPressed: () {
setState(() {
number--;
});
},
child: const Text(
"Minus",
));
the thing I will like to ask about is that I want to pass the data in the barcode to a new page after scanning. Is there anyway can I do it?
Hope anyone can solve my problem <3
Had been doing this for my intern for 4 days and I have no clue at all. Hope can solve it as fast as possible
Below is my code:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:meditation_app/widgets/category_card.dart';
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({Key key}) : super(key: key);
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String _scanBarcode = 'Unknown';
#override
void initState() {
super.initState();
}
Future<void> scanBarcodeNormal() async {
String barcodeScanRes ;
try {
barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
'#ff6666', 'Cancel', true, ScanMode.BARCODE);
print(barcodeScanRes);
} on PlatformException {
barcodeScanRes = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_scanBarcode = barcodeScanRes; //Here is the thing I will like to pass to another page
});
}
#override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('EzyMerchant'),
actions: <Widget>[
IconButton(
icon: SvgPicture.asset("assets/icons/setting.svg",color: Colors.white,),
onPressed: () {
Navigator.pushNamed(context, '/setting');
},
),
],
),
body: Stack(
children: <Widget>[
SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical:20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Good Morning \nAdmin",
style: Theme.of(context)
.textTheme
.headline4
.copyWith(fontWeight: FontWeight.w900),
),
SizedBox(height: 50),
Expanded(
child: Container(
child: GridView.count(
crossAxisCount: 2,
childAspectRatio: .85,
crossAxisSpacing: 20,
mainAxisSpacing: 20,
children: <Widget>[
Container(
child: CategoryCard(
title: "Add",
svgSrc: ("assets/icons/Hamburger.svg"),
press: () => scanBarcodeNormal(), //Here is the scan function
),
),
CategoryCard(
title: "Redeem",
svgSrc: "assets/icons/Excrecises.svg",
press: () {},
),
CategoryCard(
title: "Wallet",
svgSrc: "assets/icons/Meditation.svg",
press: () {},
),
CategoryCard(
title: "Voucher",
svgSrc: "assets/icons/yoga.svg",
press: () {},
),
],
),
),
),
],
),
),
)
],
),
);
}
}
You can use arguments property in Navigator.pushNamed like
Navigator.pushNamed(context, '/setting',arguments: _scanBarcode);
I have a listview which has Slideable
I want a longPress gesture to activate a Slideable Action.
I can get a longpress message, but I do not know what to do to cause the sliding action to work.
This is a custom Tile Widget I need to edit.
This is what it looks like when I swipe.
I want the same thing to happen when I release the longpress
Code is below:
class BookmarkListTile extends StatelessWidget {
// final BookmarkPageViewModel bookmarkViewmodel;
// final int index;
const BookmarkListTile(
{Key? key, required this.bookmark, this.onTap, this.onDelete})
: super(key: key);
final Bookmark bookmark;
final Function(Bookmark bookmark)? onDelete;
final Function(Bookmark bookmark)? onTap;
#override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onLongPressUp: () {
print("longpress");
// add code here to cause the Slidable Action to happen
},
child: Slidable(
actionPane: const SlidableDrawerActionPane(),
secondaryActions: [
IconSlideAction(
icon: Icons.delete,
color: Colors.red,
onTap: () {
if (onDelete != null) onDelete!(bookmark);
},
)
],
child: ListTile(
onTap: () {
if (onTap != null) onTap!(bookmark);
},
title: Text(bookmark.note),
subtitle: Text(PaliScript.getScriptOf(
language:
context.read<ScriptLanguageProvider>().currentLanguage,
romanText: bookmark.bookName!)),
trailing: SizedBox(
width: 100,
child: Row(
children: [
Text('${AppLocalizations.of(context)!.page} -'),
Expanded(
child: Text(
'${bookmark.pageNumber}',
textAlign: TextAlign.end,
)),
],
),
),
),
));
}
}
Try maybe this:
Widget build(BuildContext context) {
return Slidable(
actionPane: const SlidableDrawerActionPane(),
secondaryActions: [
IconSlideAction(
icon: Icons.delete,
color: Colors.red,
onTap: () {
if (onDelete != null) onDelete!(bookmark);
},
)
],
child: Builder(builder: (context) =>
GestureDetector(
behavior: HitTestBehavior.translucent,
onLongPress: () {
openSlidable(context);
},
child: ListTile(
onTap: () {
if (onTap != null) onTap!(bookmark);
},
title: Text(bookmark.note),
subtitle: Text(PaliScript.getScriptOf(
language:
context.read<ScriptLanguageProvider>().currentLanguage,
romanText: bookmark.bookName!)),
trailing: SizedBox(
width: 100,
child: Row(
children: [
Text('${AppLocalizations.of(context)!.page} -'),
Expanded(
child: Text(
'${bookmark.pageNumber}',
textAlign: TextAlign.end,
)),
],
),
),
))));
}
void openSlidable(BuildContext context) {
final slidable = Slidable.of(context);
final isClosed = slidable.renderingMode == SlidableRenderingMode.none;
if (isClosed) {
Future.delayed(Duration.zero, () {
if (slidable.mounted) {
slidable.open(actionType: SlideActionType.secondary);
}
});
}
}
This will automatically open your slidable if it was closed. Child of Slidable should be wrapped in Builder in order to work.