I have the numOfItems set in this class and I want to use it in another class AddToCart, but I cant pass the variable successfully.. here is a sample code, please how can I do this, I cannot seem to figure it out, What I want to know is the best way to pass data after I have setState to the other class..
static int numOfItems = 1;
#override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
buildOutlineButton(
icon: Icons.remove,
press: () {
if(numOfItems > 1){
setState(() {
numOfItems--;
});
}
},
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: kDefaultPaddin / 2),
child: Text(
//"01",
numOfItems.toString().padLeft(2, "0"),
style: Theme.of(context).textTheme.headline,
),
),
buildOutlineButton(
icon: Icons.add,
press: () {
setState(() {
numOfItems++;
});
}),
],
);
}
SizedBox buildOutlineButton({IconData icon, Function press}) {
return SizedBox(
width: 32,
height: 32,
child: OutlineButton(
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
onPressed: press,
child: Icon(icon),
),
);
}
}```
here is the second class where I want to use the numOfItems in the second class, how can I access the variable in this class.
```class AddToCart extends StatelessWidget {
const AddToCart({
Key key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Padding(
padding: const EdgeInsets.symmetric(vertical: kDefaultPaddin),
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(right:kDefaultPaddin),
height:50,
width: size.width * 0.3,
decoration: BoxDecoration(
border: Border.all(color:primaryColorDark),
borderRadius: BorderRadius.circular(18),
),
child: IconButton(
icon: SvgPicture.asset(
"assets/icons/add_to_cart.svg", color: primaryColorDark,), //svgPicture.asser
onPressed: () {
// on press of the cart button
},
),
),
Expanded(
child: SizedBox(
height:50,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18)
),
color: primaryColor,
onPressed: (){
addProductToCart(context);
},
child: Text(
"Buy Now".toUpperCase(),
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.bold,
color: Colors.white,
)
),
),
),
)
],
),
);
}
void addProductToCart(BuildContext context) {
String quantityToSend = numOfItems
}
}
}```
What I want to know is the best way to pass data after I have setState to the other class..
You can use constructor to pass variable
Example:
Screen1:
class _TabsPageState extends State<TabsPage> {
int args = 3;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FlatButton(
color: Colors.blueAccent,
child: Text('Navigate to home', style: TextStyle(color: Colors.white),),
onPressed: () {
print('123');
Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => Tab2Page(args)));
},
),
),
);
}
}
Screen2:
class Tab2Page extends StatelessWidget {
final int args;
Tab2Page(this.args);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(args.toString()),
),
);
}
}
If you need only this variable " If it widget " just use the name of class x and then . then the name of static variable
i.e x.numOfItems
If this a another screen and you want to pass the data to this screen you can do it like this
class SecondScreen{
var anyVariable;
XScreen(this.anyVariable);
.......
......
}
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(anyVariable :numOfItems),
),
);
},
Related
I am using flutter. I want to show different widgets when I tap on different options. On selecting option A, the option A widget is shown. On selecting option B, the option B widget is shown below the options bar and vice versa (like a tab bar). The code is attached below. I am glad if someone helps. ..
import 'package:flutter/material.dart';
class Cards extends StatefulWidget {
const Cards({Key? key}) : super(key: key);
#override
State<Cards> createState() => _CardsState();
}
class _CardsState extends State<Cards> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Cards"),
),
body: Padding(
padding: const EdgeInsets.only(top: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
optionCards("A", "assets/icons/recycle.png", context, "1"),
optionCards("B", "assets/icons/tools.png", context, "2"),
optionCards("C", "assets/icons/file.png", context, "3"),
],
),
),
);
}
Widget optionCards(
String text, String assetImage, BuildContext context, String cardId) {
return Container(
width: 100,
height: 100,
decoration: ShapeDecoration(
color: Colors.grey,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(5),
),
),
),
child: SingleChildScrollView(
child: Column(
children: [
const Padding(
padding: EdgeInsets.only(top: 13),
child: IconButton(
onPressed: null,
icon: Icon(Icons.file_copy),
),
),
Text(
text,
style: const TextStyle(
fontSize: 14,
fontFamily: 'CeraPro',
color: Color.fromRGBO(0, 0, 0, 1),
),
),
],
),
),
);
}
Widget optiona() {
return Container();
}
Widget optionb() {
return Container();
}
Widget optionc() {
return Container();
}
}
class Cards extends StatefulWidget {
const Cards({Key? key}) : super(key: key);
#override
State<Cards> createState() => _CardsState();
}
class _CardsState extends State<Cards> {
Widget? selectedOption;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Cards"),
),
body: Padding(
padding: const EdgeInsets.only(top: 20),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
InkWell(
onTap: (){
setState(() {
selectedOption = optiona();
});
},
child: optionCards("A", "assets/icons/recycle.png", context, "1")
),
InkWell(
onTap: (){
setState(() {
selectedOption = optionb();
});
},
child: optionCards("B", "assets/icons/tools.png", context, "2")
),
InkWell(
onTap: (){
setState(() {
selectedOption = optionc();
});
},
child: optionCards("C", "assets/icons/file.png", context, "3")
),
],
),
// options
if(selectedOption != null) selectedOption!
],
),
),
);
}
Widget optionCards(
String text, String assetImage, BuildContext context, String cardId) {
return Container(
width: 100,
height: 100,
decoration: const ShapeDecoration(
color: Colors.grey,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(5),
),
),
),
child: SingleChildScrollView(
child: Column(
children: [
const Padding(
padding: EdgeInsets.only(top: 13),
child: IconButton(
onPressed: null,
icon: Icon(Icons.file_copy),
),
),
Text(
text,
style: const TextStyle(
fontSize: 14,
fontFamily: 'CeraPro',
color: Color.fromRGBO(0, 0, 0, 1),
),
),
],
),
),
);
}
Widget optiona() {
return Container();
}
Widget optionb() {
return Container();
}
Widget optionc() {
return Container();
}
}
You can use the Visibility widget to wrap the widgets which you want to hide or show and keep track of which one to show through a variable. Then you can set the visible property accordingly.
import 'package:flutter/material.dart';
class Cards extends StatefulWidget {
const Cards({Key? key}) : super(key: key);
#override
State<Cards> createState() => _CardsState();
}
class _CardsState extends State<Cards> {
var showOption = "";
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Cards"),
),
body: Padding(
padding: const EdgeInsets.only(top: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
optionCards("A", "assets/icons/recycle.png", context, "1"),
optionCards("B", "assets/icons/tools.png", context, "2"),
optionCards("C", "assets/icons/file.png", context, "3"),
],
),
),
);
}
Widget optionCards(
String text, String assetImage, BuildContext context, String cardId) {
return Container(
width: 100,
height: 100,
decoration: ShapeDecoration(
color: Colors.grey,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(5),
),
),
),
child: SingleChildScrollView(
child: Column(
children: [
const Padding(
padding: EdgeInsets.only(top: 13),
child: IconButton(
onPressed: null,
icon: Icon(Icons.file_copy),
),
),
Text(
text,
style: const TextStyle(
fontSize: 14,
fontFamily: 'CeraPro',
color: Color.fromRGBO(0, 0, 0, 1),
),
),
],
),
),
);
}
Widget optiona() {
return Visibility(visible: showOption == "A", child: Container());
}
Widget optionb() {
return Visibility(visible: showOption == "B", child: Container());
}
Widget optionc() {
return Visibility(visible: showOption == "C", child: Container());
}
Now you can change the showOption variable whenever you want to show another option.
https://github.com/flutter-devs/flutter_folding_cell_demo/blob/master/lib/demo_screen.dart
I'm trying to follow the code in the link above.
But in the SimpleFoldingCell part, an error named 'The class 'SimpleFoldingCell' doesn't have a default constructor.' occurs.
Could
Is there a way to resolve this error in Dart?
class Notific extends StatefulWidget{
#override
_State createState() => _State();
}
class _State extends State<Notific>{
late List<TechnologyModel> _technologyList;
#override
void initState() {
super.initState();
_technologyList = [
TechnologyModel(title: "Application Development",),
TechnologyModel(title: "Research & Development",),
TechnologyModel(title: "Big Data & Analytics",),
TechnologyModel(title: "Support Services",),
TechnologyModel(title: "QA & Software Testing",),
];
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.pink[200],
title: Text("Folding Cell Demo",
style: TextStyle(color: Colors.white),),
),
body: Container(
child: ListView.builder(
itemCount: _technologyList.length,
itemBuilder: (context, index) {
return SimpleFoldingCell(
frontWidget: _buildFrontWidget(index),
innerTopWidget: _buildInnerWidget(index),
innerBottomWidget: _buildInnerBottomWidget(),
cellSize: Size(MediaQuery.of(context).size.width, 125),
padding: EdgeInsets.all(15),
animationDuration: Duration(milliseconds: 200),
borderRadius: 10,
onOpen: () => print('$index cell opened'),
onClose: () => print('$index cell closed'),
);
},
),
),
);
}
Widget _buildFrontWidget(int index) {
return Builder(
builder: (BuildContext context) {
return Container(
color: Colors.cyan[100],
alignment: Alignment.bottomCenter,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Aeologic Technology",
style: TextStyle(
fontSize:20.0,
color: Colors.black,
),
),
SizedBox(height: 10,),
FlatButton(
onPressed: () {
final foldingCellState = context
.findAncestorStateOfType<SimpleFoldingCellState>();
foldingCellState?.toggleFold();
},
child: Text(
"OPEN",
),
textColor: Colors.white,
color: Colors.indigoAccent[100],
splashColor: Colors.white.withOpacity(0.5),
),
],
),
);
},
);
}
Widget _buildInnerBottomWidget() {
return Builder(
builder: (context) {
return Container(
color: Colors.blueGrey[50],
alignment: Alignment.bottomCenter,
child: Padding(
padding: EdgeInsets.only(bottom: 10),
child: FlatButton(
onPressed: () {
final foldingCellState = context
.findAncestorStateOfType<SimpleFoldingCellState>();
foldingCellState?.toggleFold();
},
child: Text(
"Close",
),
textColor: Colors.white,
color: Colors.redAccent[100],
splashColor: Colors.white.withOpacity(0.5),
),
),
);
}
);
}
Widget _buildInnerWidget(int index) {
return Builder(
builder: (context) {
return Container(
color: Colors.pink[100],
padding: EdgeInsets.only(top: 10),
child: Align(
alignment: Alignment.center,
child: Text(
_technologyList[index].title,
style: TextStyle(
fontSize:20.0,
color: Colors.black,
),
),
),
);
},
);
}
}
class TechnologyModel {
String title;
TechnologyModel({
required this.title,
});
}
For some reason they've created only a named constructor.
return SimpleFoldingCell.create(
frontWidget: _buildFrontWidget(index),
innerWidget: _buildInnerWidget(index),
// innerTopWidget: _buildInnerWidget(index),
// innerBottomWidget: _buildInnerBottomWidget(),
cellSize: Size(MediaQuery.of(context).size.width, 125),
padding: EdgeInsets.all(15),
animationDuration: Duration(milliseconds: 200),
borderRadius: 10,
onOpen: () => print('$index cell opened'),
onClose: () => print('$index cell closed'),
);
It looks like the demo you're looking at is a bit outdated, since some of those properties don't exist anymore (see commented code).
I have a Bottom Sheet which has sets of buttons. I use the buttons to change the value of pinString and a text to show the pinString. The value of the text does not update when button is clicked. How to fix this
showNumberPad(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (context) {
return StatefulBuilder(builder: (context, setState) {
return Container(
height: 550.0,
child: Column(
children: <Widget>[
Text(
"$pinString",
),
KeyboardNumber(
n: 1,
onPressed: () {
pinIndexSetup("1");
setState1() {
pinString = "New Pin";
}
},
),
],
),
);
});
},
);
}
KeyboardNumber is a custom Stateful widget where I want to pass the onPressed as a parameter.
Code for keyboardNumber:
class KeyboardNumber extends StatefulWidget {
final int n;
final onPressed;
const KeyboardNumber({Key key, this.n, this.onPressed}) : super(key: key);
#override
_KeyboardNumberState createState() => _KeyboardNumberState();
}
class _KeyboardNumberState extends State<KeyboardNumber> {
#override
Widget build(BuildContext context) {
return Container(
width: 60.0,
height: 60.0,
decoration: BoxDecoration(
color: teal2,
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
alignment: Alignment.center,
child: FlatButton(
padding: EdgeInsets.all(8.0),
onPressed: widget.onPressed,
height: 90.0,
child: Text(
"${widget.n}",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
);
}
}
you define pinString in another state and change it in bottomeSheet state, you must define it in this line :
showModalBottomSheet(
context: context,
builder: (context) {
String pinString = 'hi';
return StatefulBuilder(builder: (BuildContext context, StateSetter setState){
return Container(
height: 550.0,
child: Column(
children: <Widget>[
Text(
"$pinString",
),
FlatButton(
child: Text("Update"),
onPressed: () {
setState(() => pinString = 'new');
},
),
],
),
);
});
},
);
}
Please note that the new setState will override your main widget setState but sure you can just rename it so you would be able to set state of your parent widget and the modal's
Here is the updated code.
showNumberPad(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (context) {
return StatefulBuilder(builder: (context, SetState1) {
return Container(
height: 550.0,
child: Column(
children: <Widget>[
Text(
"$pinString",
),
FlatButton(
child: Text("Update"),
onPressed: () {
SetState1(() {
pinString = "New Pin";
});
},
),
],
),
);
});
},
);
}
im trying to make my button open up something similar to this: FAB version of what i want
This is what my code looks like:
return Card(
color: Colors.yellow[100],
child: ListTile(
trailing: IconButton(icon: Icon(Icons.more_vert),
onPressed: () {},
),
leading: Text(document['date'] + '\n' + document['time'], textAlign: TextAlign.center,),
subtitle: Text(
'Loctaion: ' + document['location'],
),
title: Text(document['name'],
textAlign: TextAlign.left, style: TextStyle(fontSize: 20.0)),
),
);
}
I want the IconButton to open up something similar to the SpeedDial, to make the user choose between accept, deny, or choose later for the event.
My ListTile with the Icon.more_vert that i want to be able to open a speeddial
You can copy paste run full code below
You can use https://pub.dev/packages/flutter_portal
Basically use Overlay and show circle button
You can change childAnchor and menuAnchor, and circle button size per your request
It's too long to describe all the detail, please see working demo and full code below
code snippet
ListTile(
trailing: IconButton(
icon: Icon(Icons.more_vert),
onPressed: () => setState(() => showMenu = true),
),
...
return ModalEntry(
visible: showMenu,
onClose: () => setState(() => showMenu = false),
childAnchor: Alignment.topRight,
menuAnchor: Alignment.bottomRight,
menu: Menu(
children: [
ClipOval(
child: Material(
color: Colors.blue, // button color
child: InkWell(
splashColor: Colors.red, // inkwell color
onTap: () {
setState(() => showMenu = false);
},
child: SizedBox(width: 40, height: 40, child: Icon(Icons.menu)),
),
),
),
working demo
full code
import 'package:flutter/material.dart';
import 'package:flutter_portal/flutter_portal.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
builder: (_, child) => Portal(child: child),
home: Scaffold(
appBar: AppBar(
title: const Text('Example'),
),
body: Container(
padding: const EdgeInsets.all(10),
alignment: Alignment.centerLeft,
child: ContextualMenuExample(),
),
),
);
}
}
class ContextualMenuExample extends StatefulWidget {
ContextualMenuExample({Key key}) : super(key: key);
#override
_ContextualMenuExampleState createState() => _ContextualMenuExampleState();
}
class _ContextualMenuExampleState extends State<ContextualMenuExample> {
bool showMenu = false;
#override
Widget build(BuildContext context) {
return ModalEntry(
visible: showMenu,
onClose: () => setState(() => showMenu = false),
childAnchor: Alignment.topRight,
menuAnchor: Alignment.bottomRight,
menu: Menu(
children: [
ClipOval(
child: Material(
color: Colors.blue, // button color
child: InkWell(
splashColor: Colors.red, // inkwell color
onTap: () {
setState(() => showMenu = false);
},
child: SizedBox(width: 40, height: 40, child: Icon(Icons.menu)),
),
),
),
ClipOval(
child: Material(
color: Colors.blue, // button color
child: InkWell(
splashColor: Colors.red, // inkwell color
onTap: () {
setState(() => showMenu = false);
},
child: SizedBox(
width: 40, height: 40, child: Icon(Icons.description)),
),
),
),
ClipOval(
child: Material(
color: Colors.blue, // button color
child: InkWell(
splashColor: Colors.red, // inkwell color
onTap: () {
setState(() => showMenu = false);
},
child: SizedBox(
width: 40, height: 40, child: Icon(Icons.settings)),
),
),
)
],
),
child: Container(
child: Card(
color: Colors.yellow[100],
child: ListTile(
trailing: IconButton(
icon: Icon(Icons.more_vert),
onPressed: () => setState(() => showMenu = true),
),
leading: Text(
"date time",
textAlign: TextAlign.center,
),
subtitle: Text(
'Loctaion',
),
title: Text('name',
textAlign: TextAlign.left, style: TextStyle(fontSize: 20.0)),
),
),
),
);
}
}
class Menu extends StatelessWidget {
const Menu({
Key key,
#required this.children,
}) : super(key: key);
final List<Widget> children;
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 10),
child: Card(
elevation: 8,
child: IntrinsicWidth(
child: Column(
mainAxisSize: MainAxisSize.min,
children: children,
),
),
),
);
}
}
class ModalEntry extends StatelessWidget {
const ModalEntry({
Key key,
this.onClose,
this.menu,
this.visible,
this.menuAnchor,
this.childAnchor,
this.child,
}) : super(key: key);
final VoidCallback onClose;
final Widget menu;
final bool visible;
final Widget child;
final Alignment menuAnchor;
final Alignment childAnchor;
#override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: visible ? onClose : null,
child: PortalEntry(
visible: visible,
portal: menu,
portalAnchor: menuAnchor,
childAnchor: childAnchor,
child: IgnorePointer(
ignoring: visible,
child: child,
),
),
);
}
}
I have 2 widget and I put the first one into a variable. When I click a button in the first widget, I will call setState and put the second widget to the variable to rebuild. However, it didn't work.
I try to call setState in the parent widget of them and it work. Anyone know what wrong with it and how to solve this?
This is my code:
Widget checkinWidget = CheckInTemplate();
Column(
children: <Widget>[
Expanded(
flex: 3,
child: Container(),
),
Expanded(
flex: 1,
child: AnimatedSwitcher(
duration: const Duration(seconds: 1),
child: checkinWidget
),
)
],
),
I put the first widget CheckInTemplate to a variable and in CheckInTemplate, I have a button:
RaisedButton(
padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 60),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0)
),
color: Colors.white,
onPressed: (){
setState(() {
checkinWidget = CaptureCameraTemplate();
});
},
child: Text('Check in',
style: TextStyle(
fontSize: 25,
fontFamily: 'FredokaOne'
),),
),
When I click the button, nothing happen.
I developed one example for this, kindly check the below code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: WidgetHandling(),
);
}
}
class WidgetHandling extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _WidgetHandling();
}
}
class _WidgetHandling extends State<WidgetHandling> {
Widget widgetHolder;
void initState() {
widgetHolder = firstWidget();
super.initState();
}
Widget firstWidget() {
return Container(
color: Colors.white,
child: Center(
child: FlatButton(
color: Colors.blue,
textColor: Colors.white,
padding: EdgeInsets.all(8.0),
splashColor: Colors.blueAccent,
onPressed: () {
setState(() {
widgetHolder = secondWidget();
});
},
child: Text(
"First Widget",
style: TextStyle(fontSize: 20.0),
),
),
),
);
}
Widget secondWidget() {
return Container(
color: Colors.grey,
child: Center(
child: FlatButton(
color: Colors.blue,
textColor: Colors.white,
padding: EdgeInsets.all(8.0),
splashColor: Colors.blueAccent,
onPressed: () {
setState(() {
widgetHolder = firstWidget();
});
},
child: Text(
"Second Widget",
style: TextStyle(fontSize: 20.0),
),
),
),
);
}
#override
Widget build(BuildContext context) {
return widgetHolder;
}
}
You might have added this line,
Widget checkinWidget = CheckInTemplate();
in build method.
Remove it from build method and put to out side of build and then it will work.