I am making a flutter application that have shopping cart. Right now, i am just working on a single functionality of it when we select a particular product, after increase or decrease its quantity then our cart doesnot show the quantity of that item on the cart immediately. we have to go to another widget then it update the count on cart.
Note: HomeScreen comprise of two stateful widget, one is bottom navigation bar which have cart and other icons along with other icons and their respective UI's and other one is Product screen which is showing all our products, and in my product screen i used listview and in its UI i used - and + icons to increase or decrease its quantity. I am sharing the code of the widget - and +(a small portion of product screen) on which i want to implement this functionality.
This is the video link to show
https://youtu.be/3qqVpmWguys
HomeScreen:
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
//static _HomeScreenState of(BuildContext context) => context.ancestorStateOfType(const TypeMatcher<_HomeScreenState>());
int _currentindex = 0;
var cart;
final List<Widget> children = [
ProductScreen(),
OrderScreen(),
CartScreen(),
AccountScreen(),
];
List<BottomNavigationBarItem> _buildNavigationItems() {
var bloc = Provider.of<CartManager>(context);
int totalCount = bloc.getCart().length;
setState(() {
totalCount;
});
return <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Icons.reorder,
size: 30,
color: Colors.white,
),
title: Text(
'Product',
style: TextStyle(fontSize: 15, color: Colors.white),
),
),
BottomNavigationBarItem(
icon: Icon(
Icons.add_alert,
size: 30,
color: Colors.white,
),
title: Text(
'Order',
style: TextStyle(fontSize: 15, color: Colors.white),
),
),
BottomNavigationBarItem(
icon: Stack(
children: <Widget>[
Icon(
Icons.shopping_cart,
size: 30,
color: Colors.white,
),
Positioned(
bottom: 12.0,
right: 0.0,
child: Container(
constraints: BoxConstraints(
minWidth: 20.0,
minHeight: 20.0,
),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10.0),
),
child: Center(
child: Text(
'$totalCount',
style: TextStyle(
fontSize: 12,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
)
],
),
title: Text(
'Cart',
style: TextStyle(fontSize: 15, color: Colors.white),
),
),
BottomNavigationBarItem(
icon: Icon(
Icons.lock,
size: 30,
color: Colors.white,
),
title: Text(
'Account',
style: TextStyle(fontSize: 15, color: Colors.white),
),
),
];
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: Colors.white,
resizeToAvoidBottomPadding: true,
body: children[_currentindex],
bottomNavigationBar: BottomNavigationBar(
fixedColor: Colors.transparent,
backgroundColor: Colors.orange,
onTap: onNavigationTapbar,
currentIndex: _currentindex,
items: _buildNavigationItems(),
type: BottomNavigationBarType.fixed,
),
),
);
}
void onNavigationTapbar(int index) {
setState(() {
_currentindex = index;
});
}
}
ProductScreen incrementor or decrementor:
class TEProductIncrementor extends StatefulWidget {
var product;
TEProductIncrementor({
this.product,
});
#override
_TEProductIncrementorState createState() => new _TEProductIncrementorState();
}
class _TEProductIncrementorState extends State<TEProductIncrementor> {
int totalCount = 0;
#override
Widget build(BuildContext context) {
var cartmanager = CartManager();
void decrementsavecallback() {
// bloc.decreaseToCart(widget.ctlist);
//CartManager().updateToCart(totalCount.toString(), widget.ctlist);
setState(() {
if (totalCount > 0) {
totalCount--;
cartmanager.updateToCart(totalCount.toString(),widget.product);
}
});
}
void increasesavecallback() {
setState(() {
totalCount++;
cartmanager.updateToCart(totalCount.toString(),widget.product);
});
}
return Container(
margin: EdgeInsets.only(top: 8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10), color: Colors.orange),
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
iconSize: 30,
icon: new Icon(
Icons.remove,
),
onPressed: () {
decrementsavecallback();
},
),
Text(
totalCount.toString(),
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
IconButton(
iconSize: 30,
icon: new Icon(Icons.add),
onPressed: () {
increasesavecallback();
},
)
],
),
),
);
}
}
You can pass the entire function into a widget just implemented like below
class Parent extends StatefulWidget {
#override
_ParentState createState() => _ParentState();
}
class _ParentState extends State<Parent> {
#override
Widget build(BuildContext context) {
return Button(
(){
setState(() {
///define your logic here
});
}
);
}
}
class Button extends StatelessWidget {
final Function onTap;
Button(this.onTap);
#override
Widget build(BuildContext context) {
return FlatButton(
onPressed: onTap,
);
}
}
But if your project is pretty big then I will recommend you to use any state management libraries just like redux or mobx.
https://pub.dev/packages/mobx
You can use BLoC pattern for this,
Here is full answer and demo code that you can check.
Why not use keys?
Assign at both widget a GlobalKey and then, when you need to update that state you just need to call
yourGlobalKeyName.currentState.setState((){
//your code
});
Related
I have an AppBottomNavigation for five different states. In the first state, HomeScreen, I have a button. By tapping on this button, the state is to be changed to the third element of the AppBottomNavigation.How exactly can I implement this? I have already tried several times to call the method "bottomTapped" with index 2 via ScannderDummy. Unfortunately without success.
app_bottom_navigation.dart
class AppBottomNavigation extends StatefulWidget {
#override
_AppBottomNavigationState createState() => _AppBottomNavigationState();
}
class _AppBottomNavigationState extends State<AppBottomNavigation> {
int _selectedIndex = 0;
List<dynamic> menuItems = [
...
];
PageController pageController = PageController(
initialPage: 0,
keepPage: true,
);
Widget buildPageView() {
return PageView(
controller: pageController,
onPageChanged: (index) {
pageChanged(index);
},
children: <Widget>[
HomeScreen(),
CategoryScreen(),
ScannerScreen(),
CategoryScreen(),
ProfileSettingsScreen(),
],
);
}
pageChanged(int index) {
setState(() {
_selectedIndex = index;
});
}
bottomTapped(int index) {
setState(() {
_selectedIndex = index;
pageController.jumpToPage(index);
});
}
#override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.dark));
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.white,
showUnselectedLabels: true,
unselectedItemColor: Colors.grey,
type: BottomNavigationBarType.fixed,
selectedLabelStyle: GoogleFonts.outfit(
textStyle:
TextStyle(height: 1.5, fontSize: 10, fontWeight: FontWeight.bold),
),
unselectedLabelStyle: GoogleFonts.outfit(
textStyle: TextStyle(
height: 1.5,
fontSize: 10,
),
),
items: menuItems.map((i) {
return BottomNavigationBarItem(
icon: (i['icon']),
activeIcon: (i['icon']),
label: i['label'],
);
}).toList(),
currentIndex: _selectedIndex,
selectedItemColor: primaryColor,
onTap: (index) {
bottomTapped(index);
},
),
body: buildPageView(),
);
}
}
scanner_dummy.dart (Element from home_screen.dart)
class ScannerDummy extends StatelessWidget {
final AppBottomNavigation bn = new AppBottomNavigation();
#override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
},
child: Container(
margin:
EdgeInsets.only(top: 6, bottom: 28.0, left: 28.0, right: 28.0),
padding: EdgeInsets.symmetric(
horizontal: 16.0,
),
height: 125,
width: double.infinity,
decoration: BoxDecoration(
color: darkGrey,
borderRadius: BorderRadius.all(Radius.circular(15)),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
LineIcons.retroCamera,
color: Colors.white,
size: 35.0,
),
Text(
'Artikel scannen & verkaufen',
textAlign: TextAlign.right,
style: GoogleFonts.outfit(
textStyle: TextStyle(
color: Colors.white,
fontSize: 16,
height: 2,
)),
),
],
)),
);
}
}
Thank you very much for your help.
Play with this widget. I am using callback method.
class ItemB extends StatefulWidget {
final VoidCallback callback;
ItemB({Key? key, required this.callback}) : super(key: key);
#override
State<ItemB> createState() => _ItemBState();
}
class _ItemBState extends State<ItemB> {
#override
Widget build(BuildContext context) {
return Column(
children: [
Text("B"),
ElevatedButton(
onPressed: widget.callback,
child: Text("switch Tab"),
),
],
);
}
}
class HomeWidgetX extends StatefulWidget {
const HomeWidgetX({Key? key}) : super(key: key);
#override
State<HomeWidgetX> createState() => _HomeWidgetXState();
}
class _HomeWidgetXState extends State<HomeWidgetX> {
int currentTab = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: [
Text("A"),
ItemB(
callback: () {
setState(() {
currentTab = 0;
});
},
)
][currentTab]),
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentTab,
onTap: (value) {
currentTab = value;
setState(() {});
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.abc),
label: "",
),
BottomNavigationBarItem(
icon: Icon(Icons.ac_unit),
label: "",
),
],
),
);
}
}
well, I'm trying to create a really simple app with a bottom navigation bar and for so I created a list that lists what to be displayed in each page, the problem is that I wana have a floating button that increments a counter everytime I press it but when I do the thing to display on screen the counter inside the list, it gives me the error mesage: The instance member 'counter' can't be accessed in an initializer.
Just to clarify, the problem is right on the beggining of the code on the list, where I put the ${counter}
My code:
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
));
}
class MyApp extends StatefulWidget {
#override
_State createState() => new _State();
}
class _State extends State<MyApp> {
int counter = 0;
void resetCounter() {
setState(() {
counter = 0;
});
}
void incrementCounter() {
setState(() {
counter++;
});
}
List<Widget> _pages = <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(),
Container(
padding: EdgeInsets.all(15),
margin: EdgeInsets.all(10),
color: Colors.grey[300],
child: Text(
'lets see how many times you click it',
style: TextStyle(
fontSize: 15,
fontFamily: 'IndieFlower',
fontWeight: FontWeight.bold,
),
),
),
Text(
'button clicks - ${counter}',
style: TextStyle(
fontFamily: 'IndieFlower',
fontWeight: FontWeight.bold,
color: Colors.blue[600],
fontSize: 20,
letterSpacing: 3,
),
),
],
),
Icon(
Icons.camera,
size: 150,
),
];
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('button test'),
centerTitle: true,
backgroundColor: Colors.blue[400],
),
body: Center(
child: _pages.elementAt(_selectedIndex),
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
child: Icon(
Icons.restart_alt
),
onPressed: () {
resetCounter();
},
heroTag: null,
),
SizedBox(
height: 10,
),
FloatingActionButton(
child: Icon(
Icons.plus_one
),
backgroundColor: counter == 10 ? Colors.grey : Colors.blue,
onPressed: () =>
counter < 10 ? incrementCounter() : ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(
'limit reached',
style: TextStyle(
fontWeight: FontWeight.bold,
),
))),
heroTag: null,
)
]
),
bottomNavigationBar: BottomNavigationBar(
items: const<BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'home',
),
BottomNavigationBarItem(
icon: Icon(Icons.add_photo_alternate_outlined),
label: 'page 2',
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
);
}
}
Two solutions:
The quickest fix is to add late keyword, like so:
late List<Widget> _pages = <Widget>[
...
];
Another solution is to move your List<Widget> _pages = [...]; variable declaration inside the build method.
Both solutions achieves the same goal: delay accessing the variable ($counter) until when it's needed (i.e. when building the widget).
By the way, new keyword is no longer needed in Dart. You should remove all of them. For example, createState() => new _State() should just be createState() => _State().
Here is a builder that I will call 11 times, and I want to change the other 10 buttons color to grey when I click in one of the 11 buttons, how do I do that?
Widget buildElevatedButton(int number, Color buttonColor) {
return Padding(
padding: EdgeInsets.all(5.0),
child: Column(
children: <Widget>[
ElevatedButton(
onPressed: () {},
child: Text(
"$number",
textAlign: TextAlign.center,
),
style: ElevatedButton.styleFrom(
shadowColor: Colors.grey,
primary: buttonColor,
minimumSize: Size(70, 70),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
textStyle: TextStyle(
fontSize: 40,
fontWeight: FontWeight.bold,
),
),
),
],
),
);
}
I think what you are looking for is a Stateless Widget class that takes a callback, which is passed to the button. The state of the buttons needs to be maintained by the root widget (you lift the state up).
The flutter documentation on state are quite good: https://flutter.dev/docs/development/ui/interactive
Here is a working example:
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _selectedButton = null;
void setSelectedButton(int index) {
setState(() {
_selectedButton = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Wrap(children: [
for (int i = 0; i < 10; i++)
CustomElevatedButton(
i,
_selectedButton == null
? Colors.red
: _selectedButton == i
? Colors.red
: Colors.grey, () {
setSelectedButton(i);
}),
]),
),
);
}
}
class CustomElevatedButton extends StatelessWidget {
final int number;
final Color buttonColor;
final void Function() onPressed;
CustomElevatedButton(this.number, this.buttonColor, this.onPressed);
#override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(5.0),
child: Column(
children: <Widget>[
ElevatedButton(
onPressed: onPressed,
child: Text(
'${number + 1}',
textAlign: TextAlign.center,
),
style: ElevatedButton.styleFrom(
shadowColor: Colors.grey,
primary: buttonColor,
minimumSize: Size(70, 70),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
textStyle: TextStyle(
fontSize: 40,
fontWeight: FontWeight.bold,
),
),
),
],
),
);
}
}
Explanation: The MyHomePage stateful widget is the parent and maintains the state (i.e. it keeps track of which button is pressed). The child is a stateless widget with your custom button. The state is lifted up to the place where the buttons are actually built.
You can create a Map for checking the state
Map<int, bool> buttons = {
0: false,
1: false,
2: false,
3: false,
4: false,
};
and then inside your builder when the button is clicked toggle the Map buttons and change state to true
ListView.builder(
itemCount: buttons.length,
itemBuilder: (context, index) => ElevatedButton(
onPressed: () {
setState((){
buttons[index] = true;
});
buttons.forEach((k, v) {
if(index != k){
buttons[k]=false;
}
});
},
style: ButtonStyle(
backgroundColor: buttons[index]?MaterialStateProperty.all(Colors.blue): MaterialStateProperty.all(Colors.grey),
),
child: Text(
"Button $index",
),
),
),
I want to put a custom toolbar on the keypad like the image above. Is it possible in flutter? or should I write code on the iOS or Android side?
You can copy paste run full code below
Please see working demo below
You can use package https://pub.dev/packages/keyboard_overlay
Step 1: Use with HandleFocusNodesOverlayMixin
Step 2: Use FocusNodeOverlay for focusNode
Step 3: Use GetFocusNodeOverlay and set _focusNodeOverlay = GetFocusNodeOverlay(
Step 4: TextField use TextField(focusNode: _focusNodeOverlay,
code snippet
class _MyHomePageState extends State<MyHomePage>
with HandleFocusNodesOverlayMixin {
FocusNodeOverlay _focusNodeOverlay;
#override
void initState() {
_focusNodeOverlay = GetFocusNodeOverlay(
child: TopKeyboardUtil(
Container(
color: Colors.white,
height: 45,
width: MediaQueryData.fromWindow(ui.window).size.width,
child: Row(
children: [
GestureDetector(
child: Icon(Icons.save),
onTap: () => print("click"),
),
...
Spacer(),
Container(
width: 60,
child: Center(
child: DoneButtonIos(
backgroundColor: Colors.white,
textColor: Colors.green,
label: 'Post',
onSubmitted: () {
print("submit");
},
platforms: ['android', 'ios'],
),
),
),
],
),
),
),
);
working demo
full code
import 'package:flutter/material.dart';
import 'package:keyboard_overlay/keyboard_overlay.dart';
import 'dart:ui' as ui;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with HandleFocusNodesOverlayMixin {
FocusNodeOverlay _focusNodeOverlay;
#override
void initState() {
_focusNodeOverlay = GetFocusNodeOverlay(
child: TopKeyboardUtil(
Container(
color: Colors.white,
height: 45,
width: MediaQueryData.fromWindow(ui.window).size.width,
child: Row(
children: [
GestureDetector(
child: Icon(Icons.save),
onTap: () => print("click"),
),
GestureDetector(
child: Icon(Icons.computer),
onTap: () => print("click"),
),
GestureDetector(
child: Icon(Icons.home),
onTap: () => print("click"),
),
Spacer(),
Container(
width: 60,
child: Center(
child: DoneButtonIos(
backgroundColor: Colors.white,
textColor: Colors.green,
label: 'Post',
onSubmitted: () {
print("submit");
},
platforms: ['android', 'ios'],
),
),
),
],
),
),
),
);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
focusNode: _focusNodeOverlay,
style: TextStyle(color: Colors.grey),
decoration: InputDecoration(
labelText: 'Type Something',
labelStyle: TextStyle(color: Colors.black),
fillColor: Colors.orange,
hintStyle: TextStyle(
color: Colors.grey,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black, width: 1.0),
),
),
),
],
),
),
);
}
}
Yes there is a way around in the flutter to achieve this.
Create a widget of the toolbar you want to add.
Set it visible on input focus.
For reference I am sharing the code how I achieve that.
class InputDoneView extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
color: Style.lightGrey,
child: Align(
alignment: Alignment.topRight,
child: Padding(
padding: const EdgeInsets.only(top: 1.0, bottom: 1.0),
child: CupertinoButton(
padding: EdgeInsets.only(right: 24.0, top: 2.0, bottom: 2.0),
onPressed: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: Text(
"Done",
style: TextStyle(color: Style.primaryColor,fontWeight: FontWeight.normal)
),
),
),
),
);
}
}
To call this in your main view when input field is focused in and out.
showOverlay(BuildContext context) {
if (overlayEntry != null) return;
OverlayState overlayState = Overlay.of(context);
overlayEntry = OverlayEntry(builder: (context) {
return Positioned(
bottom: MediaQuery.of(context).viewInsets.bottom, right: 0.0, left: 0.0, child: InputDoneView());
});
overlayState.insert(overlayEntry);
}
removeOverlay() {
if (overlayEntry != null) {
overlayEntry.remove();
overlayEntry = null;
}
}
Good day! I have here some block of codes of my MainMenu page and Drawer.
I need to pass data from MainMenu which is a statefulwidget to Drawer which is also a statefulwidget so that I can use the datas and method from MainMenu.
Can someone help me or reproduce my code below.
class MainMenu extends StatefulWidget {
final VoidCallback signOut;
MainMenu(this.signOut);
#override
_MainMenuState createState() => _MainMenuState();
}
class _MainMenuState extends State<MainMenu> {
int index = 0;
List<Widget> list = [
HomeScreen(),
Stations(),
AccountPage(),
];
signOut() {
setState(() {
widget.signOut();
});
}
int currentIndex = 0;
String selectedIndex = 'TAB: 0';
String email = "", id = "", fname= "";
TabController tabController;
getPref() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
id = preferences.getString('id');
email = preferences.getString('email');
fname = preferences.getString('fname');
});
print("id:" + id);
print("user:" + email);
print("address:" + fname);
}
#override
void initState() {
// TODO: implement initState
super.initState();
getPref();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
onPressed: () {
signOut();
},
icon: Icon(Icons.lock_open),
)
],
backgroundColor: Color(0xFF262AAA),
iconTheme: IconThemeData(color: Colors.lightBlue),
centerTitle: true,
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('DVO',
style: TextStyle(color: Colors.lightBlue,fontWeight: FontWeight.w700),
),
SizedBox(width: 1.3),
Text(
'REPORT',
style: TextStyle(color: Colors.white,fontWeight: FontWeight.w700),
),
],
),
elevation: 0,
),
body: list[index],
drawer: MyDrawer(onTap: (lol, i) {
setState(() {
index = i;
Navigator.pop(lol);
});
}),
),
);
}
}
class MyDrawer extends StatefulWidget {
#override
_MyDrawerState createState() => _MyDrawerState();
}
class _MyDrawerState extends State<MyDrawer> {
Function onTap;
_MyDrawerState(
{this.onTap
});
#override
Widget build(BuildContext context) {
return SizedBox(
width: MediaQuery
.of(context)
.size
.width * 0.7,
child: Drawer(
child: Container(
color: Colors.white,
child: ListView(
padding: EdgeInsets.all(0),
children: <Widget>[
UserAccountsDrawerHeader(
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
image: AssetImage("assets/badge.jpg"),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.8), BlendMode.dstATop)),
),
accountEmail: Text("dummy#gmail.com"),
accountName: Text("Dummy",
style: TextStyle(color: Colors.white,fontWeight: FontWeight.w700, fontSize: 25),
),
currentAccountPicture: CircleAvatar(
backgroundColor: Colors.grey[400],
child: Icon(
Icons.perm_identity,
color: Colors.white,
),
),
),
ListTile(
selected: true,
leading: Icon(Icons.announcement, color: Colors.cyan,size: 26.0),
title: Text("News And Announcements",
style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
),
onTap: () => onTap(context, 0),
),
ListTile(
leading: Icon(Icons.place,color: Colors.cyan, size: 30.0),
title: Text("Stations",
style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
),
onTap: () => onTap(context, 1),
),
ListTile(
leading: Icon(Icons.settings,color: Colors.cyan, size: 30.0),
title: Text("Account Settings",
style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
),
onTap: () => onTap(context, 2),
),
Divider(
height: 595,
thickness: 0.5,
color: Colors.white.withOpacity(0.3),
indent: 32,
endIndent: 32,
),
ListTile(
leading: Icon(Icons.exit_to_app,color: Colors.cyan, size: 30.0),
onTap: () {
//widget.signOut();
},
title: Text("Logout",
style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
),
),
],
),
),
),
);
}
}
I'm getting this error on build widget in MainMenu.
The named parameter 'onTap' isn't defined.
Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'onTap'.
This part:
Function onTap;
_MyDrawerState({this.onTap});
This parameter and its presence in the constructor should be in the MyDrawer public class rather than a private State class.
The specified error comes because MyDrawer class doesn't have this.
You can access onTap function in _MyDrawerState through the widget variable which is an instance of MyDrawer class