Thee problem with setState(), It does not work in function - flutter

class BmiCalc extends StatefulWidget {
const BmiCalc({Key? key}) : super(key: key);
#override
State<BmiCalc> createState() => _BmiCalcState();
}
class _BmiCalcState extends State<BmiCalc> {
double _value = 150;
int weight = 60;
int age = 25;
double answer = 10;
String calc = "CALCULATE";
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromARGB(255, 12, 9, 34),
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
Row(
children: [
FemaleBox("MALE", Icons.male),
FemaleBox("FEMALE", Icons.female),
],
),
Column(children: [
Container(
padding: EdgeInsets.all(32),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: _hasBeenPressedFemale
? Color.fromARGB(255, 230, 72, 124)
: colorOfLittleBox,
borderRadius: BorderRadius.circular(15),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("HEIGHT",
style: TextStyle(color: Colors.grey, fontSize: 20)),
const SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(_value.toStringAsFixed(0),
style: const TextStyle(
fontSize: 45,
color: Colors.white,
fontWeight: FontWeight.w900)),
const Text(
"cm",
style:
TextStyle(fontSize: 20, color: Colors.grey),
),
],
),
Slider(
min: 100,
max: 230,
thumbColor: Colors.pink,
value: _value,
onChanged: (value) {
setState(() {
_value = value;
});
},
),
],
))
]),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children:<Widget> [
Operation(),
Operation(),
],
),
Padding(
padding: const EdgeInsets.only(bottom: 6),
child: Container(
color: Colors.pink,
height: 55,
width: MediaQuery.of(context).size.width,
child: TextButton(
child: Text(
calc,
style: const TextStyle(
fontSize: 22,
color: Colors.white,
fontWeight: FontWeight.w900),
),
onPressed: () {
calculate();
},
),
),
)
],
),
),
),
);
}
}
Widget Operations() {
int weight = 60;
int age = 25;
return Expanded(
child: Container(
padding: EdgeInsets.all(35),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Color.fromARGB(255, 27, 28, 48),
borderRadius: BorderRadius.circular(20),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("AGE",
style: TextStyle(
color: Colors.grey, fontSize: 20)),
Text(
age.toString(),
style: TextStyle(
fontSize: 40,
color: Colors.white,
fontWeight: FontWeight.w900),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 40,
width: 40,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(45),
color: Color.fromARGB(255, 76, 79, 94),
),
child: IconButton(
iconSize: 23,
icon: Icon(FontAwesomeIcons.minus,
color: Colors.white),
onPressed: () {
setState(() {
age--;
});
},
),
),
SizedBox(
width: 5,
),
Container(
height: 40,
width: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(45),
color: Color.fromARGB(255, 76, 79, 94),
),
child: IconButton(
iconSize: 23,
icon: Icon(FontAwesomeIcons.plus,
color: Colors.white),
onPressed: () {
setState(() {
age++;
});
},
),
),
],
),
],
),
)),
);
}
in Widget Operations(), setState() has an error: The function
'setState' isn't defined.
Try importing the library that defines 'setState', correcting the name to the name of an existing function, or defining a function named
'setState'.What is the problem? How can I fix it?
Without function everything is working properly, but in function sth
is wrong

I have seen your code and i have noticed that you have defined the function outside _BmiCalcState class. Define your Operations widget within the class and it should solve the problem.
Alternatively you can ask for the buildcontext within the function parameters and send the context whenever you call the function it will also solve the problem
Widget Operations (BuildContext context){//and your operations here}

Related

flutter issue: bottom overflowed by 57 pixels

Here my bottom preferredSize (preferredSize: Size.fromHeight(148.0)) in appBar.
This error raised onlu in IOS not in android. I dont know why?
On every tab this error raising, so I thought I got this error by preferredSize.
But how to solve this I am not understanding.
This error raised onlu in IOS not in android. I dont know why?
On every tab this error raising, so I thought I got this error by preferredSize.
But how to solve this I am not understanding.
this is my code
import 'package:flutter/material.dart';
import 'package:showcaseview/showcaseview.dart';
import 'package:url_launcher/url_launcher.dart' as UrlLauncher;
import 'package:dropdown_button2/dropdown_button2.dart';
import 'dart:developer';
import '../../../APIMnager/APIManager.dart';
import '../../../APIMnager/preferences.dart';
import '../../../Constants/constants.dart';
import 'action_page.dart';
import 'holding_page.dart';
import 'overview_page.dart';
import 'report_page.dart';
class PMSListDataPage extends StatelessWidget {
final panNum;
final singlePMSData;
final allPMSListData;
const PMSListDataPage(
{Key? key, this.panNum, this.singlePMSData, this.allPMSListData})
: super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: ShowCaseWidget(
onStart: (index, key) {
log('onStart: $index, $key');
},
onComplete: (index, key) {
log('onComplete: $index, $key');
print("here me");
if (index == 0) {
Preferences.saveData("showCaseCount2", "2");
}
},
blurValue: 1,
builder: Builder(
builder: (context) => PMSListDataPage1(
panNum: panNum,
singlePMSData: singlePMSData,
allPMSListData: allPMSListData)),
autoPlayDelay: const Duration(seconds: 3),
),
);
}
}
class PMSListDataPage1 extends StatefulWidget {
final panNum;
final singlePMSData;
final allPMSListData;
const PMSListDataPage1(
{Key? key, this.panNum, this.singlePMSData, this.allPMSListData})
: super(key: key);
#override
_PMSListDataPage1State createState() => _PMSListDataPage1State();
}
class _PMSListDataPage1State extends State<PMSListDataPage1> {
var selectedValue;
ApiManager apiManager = ApiManager();
final GlobalKey _one = GlobalKey();
final GlobalKey _two = GlobalKey();
final GlobalKey _three = GlobalKey();
final scrollController = ScrollController();
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
length: 4,
child: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
leading: Builder(
builder: (BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back_ios_rounded),
onPressed: () {
Navigator.pop(context);
},
tooltip: '',
);
},
),
elevation: 0,
backgroundColor: skyBlue,
title: Text(
"PMS",
style: TextStyle(fontSize: tSize16),
),
actions: [
Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: IconButton(
icon: Image.asset(
"assets/phone_icon.png",
height: 25,
width: 25,
),
onPressed: () {
UrlLauncher.launch('tel:+91$UserRMNumber');
}))
],
)
],
bottom: PreferredSize(
preferredSize: Size.fromHeight(148.0),
child: Column(
children: [
Container(
color: skyBlue,
height: 90,
child:
nameView(widget.singlePMSData, widget.allPMSListData),
),
Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: TabBar(
overlayColor:
MaterialStateProperty.all(Colors.transparent),
indicatorColor: Colors.white,
unselectedLabelColor: lightBlue,
onTap: (v) {
searchHoldingsQuery.clear();
FocusScope.of(context).unfocus();
},
indicator: UnderlineTabIndicator(
borderSide: BorderSide(
color: Colors.white,
width: 3.2,
style: BorderStyle.solid),
insets: EdgeInsets.only(left: 30.0, right: 30)),
isScrollable: true,
labelColor: Colors.white,
tabs: [
Tab(
child: Text(
'OVERVIEW',
),
),
Tab(
child: Text(
'HOLDINGS',
),
),
Tab(
child: Text(
'REPORTS',
),
),
Tab(
child: Text(
'ACTIONS',
),
),
],
),
),
],
),
),
),
body:
TabBarView(physics: NeverScrollableScrollPhysics(), children: [
Padding(
padding: const EdgeInsets.only(top: 12.0),
child: OverviewPage(),
),
Padding(
padding: const EdgeInsets.only(top: 12.0),
child: HoldingPage(),
),
Padding(
padding: const EdgeInsets.only(top: 12.0),
child: ReportPage(),
),
Padding(
padding: const EdgeInsets.only(top: 12.0),
child: ActionPage(),
),
]),
)),
);
}
}
extension WidgetExtension on _PMSListDataPage1State {
nameView(singlePMSData, allPMSListData) {
return Padding(
padding: const EdgeInsets.only(left: 20, right: 20, top: 5, bottom: 0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.white,
),
child: DropdownButtonHideUnderline(
child: DropdownButton2(
onMenuClose: () {},
onTap: () {},
isExpanded: true,
hint: Row(
children: [
SizedBox(
width: 10,
),
Expanded(
child: Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
singlePMSData['name'].toString(),
style: TextStyle(
fontSize: tSize16,
fontWeight: FontWeight.w600,
color: blackColor,
),
overflow: TextOverflow.ellipsis,
),
SizedBox(
height: 6,
),
Row(
children: [
Text(
'₹ ${singlePMSData["current_value"]}',
style: TextStyle(
fontSize: tSize16,
fontWeight: FontWeight.w600,
color: green2Color,
),
overflow: TextOverflow.ellipsis,
),
SizedBox(
width: 10,
),
],
),
],
),
],
),
),
],
),
items: allPMSListData.map<DropdownMenuItem<Object>>((option) {
return DropdownMenuItem(
value: option,
child: Container(
width: double.infinity,
alignment: Alignment.centerLeft,
// padding: const EdgeInsets.fromLTRB(0,8.0,0,6.0),
child: Row(
children: [
SizedBox(
width: 10,
),
Text(
option["name"],
style: TextStyle(
fontSize: tSize16,
fontWeight: FontWeight.w600,
color: blackColor,
),
overflow: TextOverflow.ellipsis,
),
],
),
decoration: BoxDecoration(
border: Border(
top: BorderSide(color: lightGreyColor, width: 1)))),
);
}).toList(),
selectedItemBuilder: (con) {
return allPMSListData.map<Widget>((item) {
return Row(
children: [
SizedBox(
width: 10,
),
Expanded(
child: Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
item['name'].toString(),
style: TextStyle(
fontSize: tSize16,
fontWeight: FontWeight.w600,
color: blackColor,
),
overflow: TextOverflow.ellipsis,
),
SizedBox(
height: 6,
),
Row(
children: [
Text(
"₹ ${item["current_value"]}",
style: TextStyle(
fontSize: tSize16,
fontWeight: FontWeight.w600,
color: green2Color,
),
overflow: TextOverflow.ellipsis,
),
SizedBox(
width: 10,
),
],
),
],
),
],
),
),
],
);
}).toList();
},
value: selectedValue,
onChanged: (dynamic value) {
setState(() {
});
},
icon: Container(
width: 22,
height: 22,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(3),
color: skyBlue,
),
child: Icon(
Icons.keyboard_arrow_down,
color: Colors.white,
size: 17,
),
),
iconOnClick: Container(
width: 22,
height: 22,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(3),
color: skyBlue,
),
child: Icon(
Icons.keyboard_arrow_up,
color: Colors.white,
size: 17,
),
),
iconSize: 14,
buttonHeight: 70,
buttonPadding: const EdgeInsets.only(left: 14, right: 14),
buttonDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
border: Border.all(
color: lightBlue,
),
color: Colors.white,
),
buttonElevation: 1,
itemHeight: 44,
itemPadding: const EdgeInsets.only(left: 14, right: 14),
dropdownMaxHeight: 200,
dropdownPadding: null,
dropdownDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
color: Colors.white,
),
dropdownElevation: 1,
scrollbarRadius: const Radius.circular(40),
scrollbarThickness: 3,
scrollbarAlwaysShow: false,
// offset: const Offset(-10, 0),
),
),
),
);
}
}

Flutter : Radio button

i want to turn my three buttons to radio button , so When you click on any of them, the border color and background color are changed , like the one in the middle
my button code
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.white,
width: 1,
),
),
height: 65,
width: 350,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('1 WEEK' , style: TextStyle(
fontSize: 18,
color: Colors.white,
),),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text('2.99' , style: TextStyle(
fontSize: 18,
color: Colors.white,
),),
Text('/Week' , style: TextStyle(
fontSize: 11,
color: Colors.white60,
),),
],
),
Container(
width: 25,
height: 25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(
color: Colors.white,
width: 1,
),
),
),
],
),
),
Radiobuttonwidget
//this array help us to manage the state of radio button.
var ischecked = [true, false, false];
class MyRadioButton extends StatefulWidget {
const MyRadioButton({Key? key}) : super(key: key);
#override
State<MyRadioButton> createState() => _MyRadioButtonState();
}
class _MyRadioButtonState extends State<MyRadioButton> {
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[0]=true;
ischecked[1]=false;
ischecked[2]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[0]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[0]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[1]=true;
ischecked[0]=false;
ischecked[2]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[1]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[1]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[0]=false;
ischecked[2]=true;
ischecked[1]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[2]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[2]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
],
);
}
}
SAmpleCode Dartpad live
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
shrinkWrap: true,
children: [MyRadioButton()],
),
// This trailing comma makes auto-formatting nicer for build methods.
);
}
}
var ischecked = [true, false, false];
class MyRadioButton extends StatefulWidget {
const MyRadioButton({Key? key}) : super(key: key);
#override
State<MyRadioButton> createState() => _MyRadioButtonState();
}
class _MyRadioButtonState extends State<MyRadioButton> {
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[0]=true;
ischecked[1]=false;
ischecked[2]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[0]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[0]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[1]=true;
ischecked[0]=false;
ischecked[2]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[1]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[1]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[0]=false;
ischecked[2]=true;
ischecked[1]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[2]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[2]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
],
);
}
}

flutter problem : _dependents.isempty' is not true flutter ios

when i play audio that time my song is loading that time showing error in IOS and when back from that page then i got this error "_dependents.isempty' is not true".
For android its working fine.
this is my card's page from here I going to details page where I am playing my song
import 'package:audioplayers/audioplayers.dart';
import 'package:cwc/constants/constants.dart';
import 'package:cwc/ui/video/video_player.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import '../podcast_details_page.dart';
class PodcastCompoentPurple extends StatefulWidget {
final cwcAudioListData;
const PodcastCompoentPurple({Key? key, this.cwcAudioListData,}) : super(key: key);
#override
State<PodcastCompoentPurple> createState() => _PodcastCompoentPurpleState();
}
class _PodcastCompoentPurpleState extends State<PodcastCompoentPurple> {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(18, 0, 18, 0),
child: GestureDetector(
onTap: () async{
isBtnHide=false;
await Navigator.push(context, MaterialPageRoute(builder: (context){
return PodcastDetailsPage(cwcAudioListData:widget.cwcAudioListData);
}));
print("s3 $playing");
if (playing==true) {
//pause song
var res = await audioPlayer.pause();
if (res == 1) {
setState(() {
playing = false;
print("s2 $playing");
});
}
}
print("s1 $playing");
},
child: Container(
height: 234,
// width: 260,
width: double.infinity,
decoration: BoxDecoration(
color: widget.cwcAudioListData['packages'][0]["name"]
.toString()
.toLowerCase() ==
"starter"
? Color(0xff69C583).withOpacity(.25)
:widget.cwcAudioListData['packages'][0]["name"]
.toString()
.toLowerCase() ==
"explorer"
? Color(0xFFC691D3).withOpacity(0.25)
: Color(0xFFF6931E).withOpacity(0.25),
// color: Colors.red,
borderRadius: const BorderRadius.all(
Radius.circular(10),
),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 145,
// width: 300,
width: double.infinity,
decoration: BoxDecoration(
color: widget.cwcAudioListData['packages'][0]["name"]
.toString()
.toLowerCase() ==
"starter"
? Color(0xff69C583).withOpacity(.25)
: widget.cwcAudioListData['packages'][0]["name"]
.toString()
.toLowerCase() ==
"explorer"
? Color(0xFFC691D3).withOpacity(0.25)
: Color(0xFFF6931E).withOpacity(0.25),
// color: Colors.red,
borderRadius: const BorderRadius.all(
Radius.circular(10),
),
image: DecorationImage(
image: NetworkImage('${widget.cwcAudioListData['coverImage']}'),
fit: BoxFit.cover,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: CircleAvatar(
backgroundColor: Color(0xffF1F2F6).withOpacity(0.5),
radius: 23,
child: Icon(
Icons.headphones,
size: 24,
color: Color(0xffF1F2F6).withOpacity(1),
),
),
),
],
),
),
SizedBox(
height: 10,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${widget.cwcAudioListData['title']}',
style: GoogleFonts.poppins(
fontWeight: FontWeight.w600,
fontSize: 14,
color: Color(0xFF444444)),textAlign: TextAlign.center,
),
Text(
'${widget.cwcAudioListData['description']}',
maxLines: 2,
style: GoogleFonts.poppins(
fontSize: 12, color: Color(0xFF444444)),
),
],)
],
),
),
),
),
);
}
}
This is my details page.
import 'package:audioplayers/audioplayers.dart';
import 'package:cwc/constants/constants.dart';
import 'package:cwc/constants/top_card.dart';
import 'package:cwc/ui/video/video_screen.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
class PodcastDetailsPage extends StatefulWidget {
final cwcAudioListData;
const PodcastDetailsPage({Key? key, this.cwcAudioListData}) : super(key: key);
#override
_PodcastDetailsPageState createState() => _PodcastDetailsPageState();
}
class _PodcastDetailsPageState extends State<PodcastDetailsPage> {
Duration duration = new Duration();
Duration position = Duration();
var url = '';
#override
void initState() {
url=widget.cwcAudioListData['upload'].toString();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xffffffff).withOpacity(1),
body: SafeArea(
child: Stack(
children: [
Container(
width: double.infinity,
height: 350,
decoration: BoxDecoration(
color: Color(0xffF6931E),
image: DecorationImage(
image: NetworkImage('${widget.cwcAudioListData["coverImage"]}'),
fit: BoxFit.cover,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(18, 12, 0, 0),
child: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: const CircleAvatar(
radius: 23,
backgroundColor: Colors.white,
child: Icon(Icons.arrow_back_ios_new,
color: Colors.black),
),
),
),
const SizedBox(
height: 138,
),
Padding(
padding: EdgeInsets.fromLTRB(18, 0, 18, 15),
child: Container(
height: 80,
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 270.0),
child: Container(
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(24.0),
topLeft: Radius.circular(24.0),
),
),
// height: 50,
width: double.infinity,
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(19, 33, 0, 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${widget.cwcAudioListData["title"]}',
style: GoogleFonts.poppins(
fontSize: 18,
color: Color(0xff444444),
fontWeight: FontWeight.w600,
),
),
Text(
"${formateDat(DateTime.parse(widget.cwcAudioListData['uploadDate']))} at ${DateFormat("hh:mm a").format(DateFormat("yyyy-MM-ddTHH:mm:ssZ").parseUTC("${widget.cwcAudioListData['uploadDate']}").toLocal())}",
// 'Sept, 10th 2021',
style: GoogleFonts.poppins(
fontSize: 12,
color: Color(0xff8F9698),
fontWeight: FontWeight.w600,
),
),
Text(
'FREE to watch',
style: GoogleFonts.poppins(
fontSize: 14,
color: Color(0xff69C583),
fontWeight: FontWeight.w500,
),
),
],
),
)
],
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 20, 0, 0),
child: Text(
'Video Description',
style: GoogleFonts.poppins(
fontSize: 18,
color: Color(0xFF69C583),
fontWeight: FontWeight.w500),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 8, 17, 22),
child: Text(
'${widget.cwcAudioListData["description"]}',
style: GoogleFonts.poppins(
fontSize: 13,
fontWeight: FontWeight.normal,
color: Color(0xff444444),
),
),
),
],
),
),
),
)
],
),
),
bottomNavigationBar: isBtnHide == false
? Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
child: Row(
children: [
SizedBox(
width: 30,
),
Expanded(
child: ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Color(0xFF158998)),
shape: MaterialStateProperty.all<
RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(
color: Colors.teal, width: 0.0)))),
onPressed: () async {
setState(() {
isBtnHide = true;
});
getAudio();
},
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Text(
'Listen Now',
style: GoogleFonts.poppins(fontSize: 14),
),
),
),
),
const SizedBox(
width: 30,
),
],
),
),
)
: Container(
color: Colors.white,
height: 80,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 29.0),
child:
duration.toString().split(".")[0].toString() == "0:00:00" ? Padding(
padding: const EdgeInsets.all(8.0),
child: CircularProgressIndicator(color: selectBlueColor,),
): InkWell(
onTap: () {
getAudio();
},
child: Icon(
playing == false ||
position.inSeconds.toDouble() ==
duration.inSeconds.toDouble()
? Icons.play_circle_outline
: Icons.pause_circle_outline,
size: 40,
color: selectBlueColor,
),
),
),
Column(
children: [
slider(),
Row(
children: [
Text(
"${position.toString().split(".")[0]}",
style: TextStyle(fontWeight: FontWeight.w700),
),
SizedBox(
width: MediaQuery.of(context).size.width / 2.55,
),
Text(
"${duration.toString().split(".")[0]}",
style: TextStyle(fontWeight: FontWeight.w300),
),
],
)
],
)
],
),
),
);
}
Widget slider() {
return Container(
width: MediaQuery.of(context).size.width / 1.3,
child: Slider.adaptive(
activeColor: selectBlueColor,
inactiveColor: Colors.grey[350],
value: position.inSeconds.toDouble(),
min: 0.0,
max: duration.inSeconds.toDouble(),
onChanged: (double value) {
print(duration.inSeconds.toDouble());
setState(() {
audioPlayer.seek(Duration(seconds: value.toInt()));
});
}));
}
Future<void> getAudio() async {
// playing is false by default
print(playing);
if (playing) {
//pause song
var res = await audioPlayer.pause();
if (res == 1) {
setState(() {
playing = false;
});
}
} else {
//play song
var res = await audioPlayer.play(url, isLocal: true);
if (res == 1) {
setState(() {
print(res);
playing = true;
});
}
}
audioPlayer.onDurationChanged.listen((Duration d) {
// print('Max duration: $d');
if (mounted) {
setState(() => duration = d);
}
});
audioPlayer.onAudioPositionChanged.listen((Duration dd) {
if (mounted) {
setState(() {
position = dd;
});
}
});
}
}
formateDat(DateTime date) {
final DateFormat formatter = DateFormat.MMM();
String formatted = formatter.format(date);
formatted = "${formatted} " + "${date.day}";
var week = DateFormat('EEEE').format(date);
var we = week[0] + week[1] + week[2];
return "$we, " + formatted;
}
This is my error
this is my video recording.

How do I pass data to a screen while using auto_route in flutter?

I have a home page with 5 bottomnavbar items (just like instagram) and I need to pass a boolean to one of the pages. I'm using auto_route to navigate.
AutoRoute(
path: '/home',
name: 'HomeRouter',
page: Home,
children: [
AutoRoute(
path: 'timeline',
name: 'TimelineRouter',
page: Timeline,
),
AutoRoute(
path: 'profile',
name: 'ProfileRouter',
page: EmptyRouterPage,
children: [
AutoRoute(
path: '',
page: Profile, <-----
),
AutoRoute(
path: ":currentUserId",
name: "EditProfileRouter",
page: EditProfile,
),
],
),
],
)
I need to pass a boolean to the profile page but I'm not sure how to do that using auto_route. Any hints?
When I passed a boolean, the page received a null value for some reason. What could be the possible reason for getting a null value?
This answer is coming straight from the auto_route API documentation:
AutoRoute automatically detects and handles your page arguments for
you, the generated route object will deliver all the arguments your
page needs including path/query params.
e.g. The following page widget will take an argument of type Book.
class BookDetailsPage extends StatelessWidget {
const BookDetailsPage({required this.book});
final Book book;
...
The generated BookDetailsRoute will deliver the same arguments to it's
corresponding page.
router.push(BookDetailsRoute(book: book));
I used a bool value on the profile page to remove the leading icon while I am calling on the bottom navbar so I am sharing my code I Hope this will work for you.Thanks
////Bottom Navigation page
import 'package:flutter/material.dart';
import 'package:traveling/helpers/AppColors.dart';
import 'package:traveling/screens/Employee/home/Home.dart';
import 'package:traveling/screens/Employee/profile/Profile.dart';
import 'package:traveling/screens/Employee/setting/Setting.dart';
class EmployeeBottomNavBar extends StatefulWidget {
const EmployeeBottomNavBar({Key? key}) : super(key: key);
#override
_EmployeeBottomNavBarState createState() => _EmployeeBottomNavBarState();
}
class _EmployeeBottomNavBarState extends State<EmployeeBottomNavBar> {
int pageIndex = 0;
bool visible = true;
List<Widget> pageList = <Widget>[EmployeeHome(), EmployeeProfile(leadingIcon: false,), Setting()];
#override
Widget build(BuildContext context) {
return Scaffold(
body: pageList[pageIndex],
bottomNavigationBar: BottomNavigationBar(
fixedColor: Colors.redAccent[400],
currentIndex: pageIndex,
onTap: (value) {
setState(() {
pageIndex = value;
});
},
// type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
activeIcon: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xff2161c0),
),
child: Icon(
Icons.home,
color: AppColors.white,
),
),
icon: Icon(
Icons.home,
color: Color(0xff2161c0),
),
label: ""),
BottomNavigationBarItem(
activeIcon: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xff2161c0),
),
child: Icon(
Icons.person,
color: AppColors.white,
),
),
icon: Icon(
Icons.person,
color: AppColors.baseLightBlueColor,
),
label: ""),
BottomNavigationBarItem(
activeIcon: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: AppColors.baseLightBlueColor,
),
child: Icon(
Icons.settings,
color: AppColors.white,
),
),
icon: Icon(
Icons.settings,
color: AppColors.baseLightBlueColor,
),
label: ""),
]
)
);
}
}
///////Profile page
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:traveling/helpers/AppColors.dart';
import 'package:traveling/helpers/AppStrings.dart';
class EmployeeProfile extends StatefulWidget {
final bool leadingIcon;
EmployeeProfile({Key? key, required this.leadingIcon}) : super(key: key);
#override
_EmployeeProfileState createState() => _EmployeeProfileState();
}
class _EmployeeProfileState extends State<EmployeeProfile> {
bool? visible;
double? _width;
File ?_image;
final picker = ImagePicker();
Future<void>_showChoiceDialog(BuildContext context)
{
return showDialog(context: context,builder: (BuildContext context){
return AlertDialog(
title: Text("Choose option",style: TextStyle(color: AppColors.hotelListDarkBlue),),
content: SingleChildScrollView(
child: ListBody(
children: [
Divider(height: 1,color: AppColors.baseLightBlueColor,),
ListTile(
onTap: (){
Navigator.pop(context,_getImage(ImageSource.gallery));
},
title: Text("Gallery",style: TextStyle(color: AppColors.hotelListDarkBlue),),
leading: Icon(Icons.account_box,color: AppColors.baseLightBlueColor,),
),
Divider(height: 1,color: AppColors.baseLightBlueColor,),
ListTile(
onTap: (){
Navigator.pop(context,_getImage(ImageSource.gallery));
},
title: Text("Camera",style: TextStyle(color: AppColors.hotelListDarkBlue),),
leading: Icon(Icons.camera,color: AppColors.baseLightBlueColor,),
),
],
),
),);
});
}
#override
Widget build(BuildContext context) {
_width = MediaQuery.of(context).size.width;
return Scaffold(
body: ListView(
shrinkWrap: true,
children: [
Container(
width: _width!,
color: AppColors.white,
child: Stack(
children: [
Column(
children: [
Material(
color: AppColors.baseLightBlueColor,
elevation: 15,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.only(bottomRight: Radius.circular(30)),
),
child: Container(
height: 180,
decoration: BoxDecoration(
color: AppColors.baseLightBlueColor,
// AppColors.blue,
borderRadius:
BorderRadius.only(bottomRight: Radius.circular(30)),
),
),
),
Container(
// color: AppColors.green,
width: _width! * 0.90,
height: 140,
margin: EdgeInsets.only(top: 60),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
// crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppStrings.personalInformation,
// "Informations personelles",
style: TextStyle(
color: Color(0xff919AAA),
),
)
],
),
Row(
children: [
Container(
height: 100,
width: 50,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppStrings.name,
style: TextStyle(
fontSize: 15,
color: AppColors.hotelListDarkBlue,
),
),
Text(
AppStrings.email,
style: TextStyle(
fontSize: 15,
color: AppColors.hotelListDarkBlue,
),
),
Text(
AppStrings.phone,
// "Phone",
style: TextStyle(
fontSize: 15,
color: AppColors.hotelListDarkBlue,
),
),
],
),
),
SizedBox(
height: 100,
width: 30,
),
Container(
height: 100,
width: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Laure Beno",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: AppColors.hotelListDarkBlue,
),
),
Text(
"laure.beno#gmail.com",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: AppColors.hotelListDarkBlue,
),
),
Text("+33 (0)6 45 23 65 77",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: AppColors.hotelListDarkBlue,
)),
],
),
),
],
),
],
),
),
Container(
width: _width! * 0.87,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
AppStrings.myPoetryModes,
style: TextStyle(
color: AppColors.hotelListLightGreyColor,
),
),
Text(
"+ " + AppStrings.add,
style: TextStyle(
color: AppColors.hotelListLightGreyColor,
),
),
],
),
),
SizedBox(
height: 20,
),
Container(
width: _width! * 0.90,
child: atmCard(
cardName: "VISA",
image: "assets/Visa.png",
height: 10)),
Container(
width: _width! * 0.90,
child: atmCard(
cardName: "Master Card",
image: "assets/MasterCard.png",
height: 20)),
],
),
Positioned(
top: 80,
child: _image==null?Stack(
children: [
Container(
height: 150,
width:_width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 120,
width: 120,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(60),
border: Border.all(color: AppColors.white, width: 2.0),
image: DecorationImage(
image: AssetImage("assets/managerPicture.jpeg"),fit: BoxFit.cover,
)
/*gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Color(0xff00519E),
AppColors.blue,
Colors.blue.shade900,
],
)*/
),
/* child: ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: Image.asset("assets/manager.jpeg",height: 100,width: 100,)
//Icon(Icons.person, size: 100),
// child: Image.asset("assets/logo/profile.png", fit: BoxFit.fill),
),*/
),
],
),
),
Positioned(
top: 60,
right: 0,
left: 100,
bottom: 0,
child: GestureDetector(
onTap: (){
_showChoiceDialog(context);
},
child: Container(
height: 10,
width: _width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 35,
width: 35,
decoration: BoxDecoration(
color: AppColors.profiePicBackground,
borderRadius: BorderRadius.circular(60),
//border: Border.all(color: Colors.blue.shade900, width: 2.0),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: Icon(MdiIcons.squareEditOutline,color: AppColors.popUpBlueColor,size: 20,),
),
),
],
),
),
),)
],
):Stack(
children: [
Container(
height: 150,
width:_width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 120,
width: 120,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image: FileImage(_image!),
),
border: Border.all(color: AppColors.white, width: 2.0),
),
),
],
),
),
Positioned(
top: 60,
right: 0,
left: 100,
bottom: 0,
child: GestureDetector(
onTap: (){
_showChoiceDialog(context);
},
child: Container(
height: 10,
width: _width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 35,
width: 35,
decoration: BoxDecoration(
color: AppColors.profiePicBackground,
borderRadius: BorderRadius.circular(60),
//border: Border.all(color: Colors.blue.shade900, width: 2.0),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: Icon(MdiIcons.squareEditOutline,color: AppColors.popUpBlueColor,size: 20,),
),
),
],
),
),
),)
],
)
),
Positioned(
top: 40,
child: Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.only(left:8,right: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
widget.leadingIcon == true
? Align(
alignment: Alignment.topLeft,
child: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Icon(
MdiIcons.arrowLeft,
color: Colors.white,
)),
)
: Container(),
Align(
alignment: Alignment.topCenter,
child: Text(AppStrings.myAccount,
style: TextStyle(
fontSize: 15,
color: AppColors.white,
fontWeight: FontWeight.bold)),
),
Align(
alignment: Alignment.topRight,
child: GestureDetector(
onTap: () {},
child: Icon(
MdiIcons.pencilOutline,
color: AppColors.white,
)),
),
],
),
)
),
],
),
),
],
),
);
}
//ImageSource: Camera and Gallery.
_getImage(ImageSource imageSource) async
{
PickedFile? imageFile = await picker.getImage(source: imageSource);
//if user doesn't take any image, just return.
if (imageFile == null) return;
setState(
() {
//Rebuild UI with the selected image.
_image = File(imageFile.path);
},
);
}
Widget atmCard({String? image, String? cardName, double? height}) {
return GestureDetector(
onTap: () {
// Navigator.push(
// context, MaterialPageRoute(builder: (context) => Class()));
},
child: Card(
elevation: 2,
margin: EdgeInsets.only(bottom: 15, right: 5, left: 5),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Container(
height: 80,
// width: _width! * 0.90,
padding: EdgeInsets.only(
// top: 5,
left: 20,
// right: 20,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15), color: Colors.white),
child: Row(
children: [
Image.asset(
image.toString(),
height: height,
),
//Icon(Icons.atm,color: AppColors.hotelListDarkBlue,),
SizedBox(
width: 30,
),
Container(
width: MediaQuery.of(context).size.width * 0.62,
height: 50,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
cardName.toString(),
// "MasterCard",
style: TextStyle(
color: AppColors.hotelListDarkBlue,
fontSize: 12,
fontWeight: FontWeight.bold),
),
Text(
"4******12345678",
style: TextStyle(
fontSize: 14,
color: AppColors.hotelListLightGreyColor),
),
],
),
)
],
),
),
),
);
}
}
You can define your path arguments like this in autoRoute class :
AutoRoute(page: TrackAttendancePage, path: 'track-attendance/:id')
Then in your page pass them as argument using #PathParam
class TrackAttendancePage extends StatefulWidget {
final String registerId;
const TrackAttendancePage(#PathParam('id') this.registerId,{Key? key})
: super(key: key);
}

How to fetch values from a form in flutter?

As per the image I have to fetch all the values from the required field i.e. from the TextFormField, CustomRadioButton & as well as from the patient age as per my requirement. On click of submit button I have to fetch those values so that I can pass them through the provider.
As I am very new to flutter, I can fetch the value from the TextFormField but couldnot able to fetch from rest of the two.
here is my UI below.
below is the code for CustomRadioButton
Container(
width: 200,
child: CustomRadioButton(
customShape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
elevation: 0,
absoluteZeroSpacing: false,
unSelectedColor: Theme.of(context).canvasColor,
buttonLables: [
'Male',
'Female',
],
buttonValues: [
"MALE",
"FEMALE",
],
buttonTextStyle: ButtonTextStyle(
textStyle: TextStyle(fontSize: 10)),
radioButtonValue: (value) {
print(value);
},
width: 80,
height: 60,
enableShape: true,
selectedBorderColor: Colors.green,
unSelectedBorderColor: Color(0xFFD0D7EB),
selectedColor: Colors.green,
),
),
for the age picker(/do not know I am correct or not because I had just hardcoded the text 12, but want + and - should work./)
Container(
width: 100.0,
height: 55.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
InkWell(
onTap: () {},
child: Container(
height: 25.0,
width: 25.0,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(7.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Center(
child: Icon(
Icons.remove,
color: Colors.black,
size: 20.0,
),
),
),
),
Text(
'12',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 20.0,
color: Colors.black),
),
InkWell(
onTap: () {},
child: Container(
height: 25.0,
width: 25.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Center(
child: Icon(
Icons.add,
color: Colors.black,
size: 20.0,
),
),
),
)
],
),
)
],
),
SizedBox(height: 80),
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: 40,
child: RaisedButton(
color: Color(0xFF888DA1),
child: Text('Submit', style: TextStyle(fontFamily: 'Poppins-Regular',
fontSize: 14, color: Colors.white)),
onPressed: (){
submit();
}
),
),
],
This one is the function where I will fetch those values
submit() async{
print(nameController.text);
}
Please help me how to do this!
So here what i do first set defaultSelected: "MALE", and then store value in one variable and when you change the value then you
get this value from radioButtonValue: this method then simply add this value to my local variable. After that for age i only added simple
increment and decrement logic and set this value to text
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
accentColor: Colors.blue,
),
home: SelectionScreen(),
);
}
}
class SelectionScreen extends StatefulWidget{
#override
State<StatefulWidget> createState() {
// TODO: implement createState
return _selectionScreen();
}
}
class _selectionScreen extends State<SelectionScreen>{
var gender = "MALE";
int age = 12;
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: Container(
color: Colors.white,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Column(
children: [
Container(
width: 200,
child: CustomRadioButton(
customShape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
elevation: 0,
absoluteZeroSpacing: false,
unSelectedColor: Theme.of(context).canvasColor,
buttonLables: [
'Male',
'Female',
],
buttonValues: [
"MALE",
"FEMALE",
],
buttonTextStyle: ButtonTextStyle(
textStyle: TextStyle(fontSize: 10)),
radioButtonValue: (value) {
setState(() {
gender = value.toString();
print("=--->>${gender}");
});
},
width: 80,
height: 60,
enableShape: true,
selectedBorderColor: Colors.green,
unSelectedBorderColor: Color(0xFFD0D7EB),
selectedColor: Colors.green,
defaultSelected: "MALE",
),
),
Container(
width: 100.0,
height: 55.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
InkWell(
onTap: () {
setState(() {
age--;
});
},
child: Container(
height: 25.0,
width: 25.0,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(7.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Center(
child: Icon(
Icons.remove,
color: Colors.black,
size: 20.0,
),
),
),
),
Text(
age.toString(),
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 20.0,
color: Colors.black),
),
InkWell(
onTap: () {
setState(() {
age++;
});
},
child: Container(
height: 25.0,
width: 25.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7.0),
border: Border.all(
color: Color(0xFFD0D7EB),
)
),
child: Center(
child: Icon(
Icons.add,
color: Colors.black,
size: 20.0,
),
),
),
)
],
),
),
SizedBox(height: 80),
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: 40,
child: RaisedButton(
color: Color(0xFF888DA1),
child: Text('Submit', style: TextStyle(fontFamily: 'Poppins-Regular',
fontSize: 14, color: Colors.white)),
onPressed: (){
print("============>> Radio Button Value: $gender");
print("============>> Age: $age");
}
),
),
],),
],
),),
);
}
}
The most direct way is to build a model class that will have the default values for each form element. Then, when building the form, construct an instance of the form data class, and use the members to establish the value: for each item. Wire up each of the onChanged: callbacks to validate the new value, and store it back into the form data instance. On submit, send the data along its merry way.