How to fetch values from a form in flutter? - 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.

Related

How to set the size of Text field Button

I am building a Flutter application where I am trying to build simple login UI but having problem as shown in image below. One problem is the size of the email field and the password field is different and another is login button is being overflowed by 13-pixels? Also want to set the height underscore of Forget your password. How can It be solved? Thank you in advance.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Navigation Bar',
theme: ThemeData(
primaryColor: Color(0xFFC41A3B),
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var _isVisible = false;
#override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height,
),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: const [
Colors.blue,
],
begin: Alignment.topLeft,
end: Alignment.centerRight,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 2,
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 36.0,
horizontal: 24.0,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text(
'Login',
style: TextStyle(
color: Colors.white,
fontSize: 46.0,
fontWeight: FontWeight.w800,
),
),
SizedBox(
height: 10.0,
),
Text(
'Enter to the butiful world',
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontWeight: FontWeight.w300,
),
),
],
),
),
),
Expanded(
flex: 2,
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
fillColor: Color(0xffe73ded),
hintText: "E-mail",
prefixIcon: Icon(
Icons.email,
color: Colors.grey,
),
)),
SizedBox(
height: 20.0,
),
TextField(
obscureText: _isVisible ? false :true,
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
suffix: IconButton(
onPressed: () {
setState(() {
_isVisible = !_isVisible;
});
},
icon: Icon(
_isVisible ? Icons.visibility : Icons.visibility_off,
color: Colors.grey,
),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
fillColor: Color(0xffe73ded),
hintText: "Password",
prefixIcon: Icon(
Icons.lock_rounded,
color: Colors.grey,
),
)),
SizedBox(
height: 5.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () {},
child: Text(
'Forgot your password',
style: TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline,
),
),
),
],
),
SizedBox(
height: 20.0,
),
Container(
color: Colors.blue,
width: double.infinity,
child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(28),
),
),
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 16.0),
child: Text(
'Login',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
),
),
),
),
),
SizedBox( height: 2,),
RichText(
text: TextSpan(
text: 'Do you have an account?',
style: TextStyle(
color: Colors.black,
fontSize: 18,
),
children: [
TextSpan(
text: 'Register',
style: TextStyle(
color: Colors.blue,
fontSize: 18,
),
recognizer: TapGestureRecognizer()
..onTap = () {}),
],
),
),
],
),
),
),
),
],
),
),
),
);
}
}
The issue is you created a container with max height as the device.then used expanded widgets which are equally spaced. You can try increasing the flex value of the second expanded widget
Expanded(
flex: 3
)
Now the first and second expanded will be of ratio 2:3 which will give more space to the second widget

How to make value stay despite leaving the page?

I am trying to make an edit page for users to update their details. I am able to edit the details. But when I leave the page the values go back to the original value it was before editing. How do I make the value stay?
Here is how the screen looks like -
Screen Picture
I did wrap the values in set state thinking that they would remain after leaving the page but they dont.
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:my_plate/screens/home_screen.dart';
import 'package:my_plate/screens/user_guide_screen.dart';
import 'package:my_plate/widgets/app_drawer.dart';
class EditProfilePage extends StatefulWidget {
static String routeName = '/profile';
#override
_EditProfilePageState createState() => _EditProfilePageState();
}
class _EditProfilePageState extends State<EditProfilePage> {
final myController = TextEditingController();
final myController1 = TextEditingController();
String name = 'carolyn1234';
String email = 'carolyn#gmail.com';
final formKey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: true,
appBar: AppBar(
title: Text('Profile'),
backgroundColor: Color(0xff588157),
elevation: 1,
// leading: IconButton(
// icon: Icon(
// Icons.arrow_back,
// color: Colors.white,
// ),
// onPressed: () {
// Navigator.of(context).pushNamed(MainScreen.routeName);
// },
// ),
actions: [
IconButton(
icon: Icon(Icons.library_books_outlined),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => UserGuideListScreen(),
));
},
),
],
),
body: Container(
padding: EdgeInsets.only(left: 16, top: 25, right: 16),
child: GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: Form(
key: formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: ListView(
children: [
Text(
"Profile",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25, fontWeight: FontWeight.w800),
),
SizedBox(
height: 15,
),
Center(
child: Stack(
children: [
Container(
width: 130,
height: 130,
decoration: BoxDecoration(
border: Border.all(
width: 4,
color: Theme.of(context)
.scaffoldBackgroundColor),
boxShadow: [
BoxShadow(
spreadRadius: 2,
blurRadius: 10,
color: Colors.black.withOpacity(0.1),
offset: Offset(0, 10))
],
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
"https://i.postimg.cc/gj4CDtjX/image.png",
))),
),
Positioned(
bottom: 0,
right: 0,
child: Container(
height: 40,
width: 40,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: 4,
color:
Theme.of(context).scaffoldBackgroundColor,
),
color: Color(0xff588157),
),
child: Icon(
Icons.edit,
color: Colors.white,
),
)),
],
),
),
SizedBox(
height: 35,
),
Text(
'User Details',
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20),
),
SizedBox(height: 10),
Text('Username',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w400,
color: Colors.black54)),
SizedBox(height: 5),
Container(
child: Text(name),
// margin: const EdgeInsets.all(2.0),
padding: const EdgeInsets.all(10.0),
width: 2000,
decoration: BoxDecoration(
border: Border.all(color: Colors.blueGrey)),
alignment: Alignment.topLeft,
),
SizedBox(height: 10),
Text('Email',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w400,
color: Colors.black54)),
SizedBox(height: 5),
Container(
child: Text(email),
// margin: const EdgeInsets.all(2.0),
padding: const EdgeInsets.all(10.0),
width: 2000,
decoration: BoxDecoration(
border: Border.all(color: Colors.blueGrey)),
alignment: Alignment.topLeft,
),
SizedBox(
height: 35,
),
Text(
'Edit Details',
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20),
),
SizedBox(
height: 15,
),
TextFormField(
controller: myController,
// initialValue: 'bla123',
// enabled: false,
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff588157),
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff588157),
width: 0.0,
),
),
label: Text('Username',
style: TextStyle(color: Colors.black)),
),
validator: (username) {
if (username == null || username.isEmpty)
return 'Field is required.';
else if (username.length < 8)
return 'Please enter a description that is at least 8 characters.';
else
return null;
},
),
SizedBox(
height: 15,
),
TextFormField(
controller: myController1,
// initialValue: 'bla#gmail.com',
// enabled: false,
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff588157),
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff588157),
width: 0.0,
),
),
label:
Text('Email', style: TextStyle(color: Colors.black)),
),
validator: (email) {
if (email == null || email.isEmpty)
return 'Field is required.';
String pattern = r'\w+#\w+\.\w+';
if (!RegExp(pattern).hasMatch(email))
return 'Invalid E-mail Address format.';
return null;
},
),
// DropdownButtonFormField(
// // onChanged: null,
// hint: Text('Female'),
// decoration: InputDecoration(
// label: Text('Gender'),
// ),
// items: [
// DropdownMenuItem(child: Text('Female'), value: 'female'),
// DropdownMenuItem(child: Text('Male'), value: 'male'),
// ],
// onChanged: (String? value) {},
// ),
SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
onPressed: () {
final isValidForm = formKey.currentState!.validate();
if (isValidForm) {
setState(() {
name = myController.text;
email = myController1.text;
});
}
setState(() {
// name = myController.text;
// email = myController1.text;
});
},
color: Color(0xff588157),
padding: EdgeInsets.symmetric(horizontal: 30),
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
child: Text(
"Update Details",
style: TextStyle(
fontSize: 15,
letterSpacing: 2.2,
color: Colors.white),
),
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
onPressed: () {
showAlertDialog();
// setState(() {
//
//
// // name = myController.text;
// // email = myController1.text;
// });
},
color: Color(0xff588157),
padding: EdgeInsets.symmetric(horizontal: 30),
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
child: Text(
"Delete Account",
style: TextStyle(
fontSize: 15,
letterSpacing: 2.2,
color: Colors.white),
),
)
],
)
],
),
),
),
),
drawer: AppDrawer());
}
void showAlertDialog() {
// set up the buttons
Widget cancelButton = TextButton(
child: Text("No", style: TextStyle(color: Color(0xff588157))),
onPressed: () {
Navigator.pop(context);
},
);
Widget continueButton = TextButton(
child: Text("Yes", style: TextStyle(color: Color(0xff588157))),
onPressed: () {
setState(() {
name = '';
email = '';
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Account deleted successfully!'),
));
Navigator.pop(context);
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("AlertDialog"),
content: Text("Are you sure you want to submit this form?"),
actions: [
cancelButton,
continueButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}

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,
),
),
),
),
],
),
),
),
),
],
);
}
}

Customize bottom navigation bar in flutter

I need to create a nav bar whose icons will look like this when active:
But mine looks like:
I usually use bottom_navy_bar.
So here is the code for it:
BottomNavyBarItem(
icon: _currentIndex == 0
? _buildActiveIcon(
'assets/icons/navigation/home_white_icon.png')
: _buildInactiveIcon("assets/icons/navigation/home_icon.png"),
activeColor: Colors.black,
inactiveColor: CustomColors.white,
title: Text(
"Home",
),
),
Code for active and inactive icon:
Widget _buildInactiveIcon(String assetUrl) {
return Container(
height: 30,
width: 30,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(60),
color: CustomColors.white,
),
child: Center(
child: ImageIcon(
AssetImage(
assetUrl,
),
color: CustomColors.black,
),
),
);
}
Widget _buildActiveIcon(String assetUrl) {
return Container(
height: 30,
width: 30,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(60),
color: Colors.black,
),
child: Center(
child: ImageIcon(
AssetImage(
assetUrl,
),
color: Colors.white,
),
),
);
}
So can this be done with the help pf any package or do I have to create some sort of custom navbar?
This is your solution:
Full page Screen Shot
BottomNavigationBarItem
BottomNavigatonBarItem Code:-
BottomNavigationBarItem(
icon: Container(
width: 140,
height: 35,
decoration: const BoxDecoration(
color: Color(0xffA3C2D4),
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Stack(
children: [
Container(
width: 35,
height: 35,
decoration: const BoxDecoration(
color: Colors.black, shape: BoxShape.circle),
child: Icon(
Icons.notifications,
color: Colors.white,
),
),
const Center(
child: Padding(
padding: EdgeInsets.only(left: 20.0),
child: Text(
"Notification",
style: TextStyle(color: Colors.black),
),
))
],
),
),
label: '',
),
Full Code:-
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: const Center(
child: Text(
"Screen 1",
style: TextStyle(fontSize: 28),
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: 0,
selectedItemColor: Colors.amber[800],
onTap: (v) {},
elevation: 0.0,
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Container(
width: 140,
height: 35,
decoration: const BoxDecoration(
color: Color(0xffA3C2D4),
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Stack(
children: [
Container(
width: 35,
height: 35,
decoration: const BoxDecoration(
color: Colors.black, shape: BoxShape.circle),
child: const Icon(
Icons.home,
color: Colors.white,
),
),
const Center(
child: Padding(
padding: EdgeInsets.only(left: 20.0),
child: Text(
"Home",
style: TextStyle(color: Colors.black),
),
))
],
),
),
label: '',
),
BottomNavigationBarItem(
icon: Container(
width: 140,
height: 35,
decoration: const BoxDecoration(
color: Color(0xffA3C2D4),
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Stack(
children: [
Container(
width: 35,
height: 35,
decoration: const BoxDecoration(
color: Colors.black, shape: BoxShape.circle),
child: const Icon(
Icons.notifications,
color: Colors.white,
),
),
const Center(
child: Padding(
padding: EdgeInsets.only(left: 20.0),
child: Text(
"Notification",
style: TextStyle(color: Colors.black),
),
))
],
),
),
label: '',
),
],
),
);
}
}
You can use persistent_bottom_nav_bar
Check also the package Salomon_bottom_navigation

flutter how to make card in leading of listview

I want to make UI like my upper image but I cant make like that But it is not being made as you are seeing in the image below.
I want to make leading of ListTile part like this .
But I can't make it please help me.
this is my code and UI
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.only(top: 30, left: 10, right: 10),
child: Column(
children: [
Row(
children: [
Text(
'Your rooms',
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
Spacer(),
Card(
elevation: 1,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Row(
children: [
Text('add',
style: TextStyle(fontSize: 20, color: Colors.grey)),
Icon(
Icons.add,
size: 20,
color: Colors.grey,
)
],
),
),
)
],
),
SizedBox(
height: 20,
),
Container(
height: 110,
width: 380,
// color: Colors.red,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Color(0xffbbf0e3),
),
child: Center(
child: ListTile(
leading: Container(
// height: 10,
width: 100,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0)),
elevation: 1,
color: Colors.white,
child: Container(
decoration: BoxDecoration(
// borderRadius: BorderRadius.circular(25),
shape: BoxShape.circle,
// color: Colors.red,
),
child: CircleAvatar(
radius: 25,
backgroundImage: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkNWsvdJGKCRYW_Wb-K40Po2JQ8LpooWoilw&usqp=CAU'),
),
),
),
),
// leading: Card(
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(30.0)),
// elevation: 1,
// color: Colors.white,
// child: Container(
// height: 80,
// width: 75,
// decoration: BoxDecoration(
// // borderRadius: BorderRadius.circular(25),
// shape: BoxShape.circle,
// // color: Colors.red,
// ),
// child: CircleAvatar(
// radius: 25,
// backgroundImage: NetworkImage(
// 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkNWsvdJGKCRYW_Wb-K40Po2JQ8LpooWoilw&usqp=CAU'),
// ),
// )),
title: Text(
"Living room",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w700),
),
subtitle: Text(
'3 turned on',
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 20,
color: Colors.green),
),
trailing: Card(
elevation: 1,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
width: 50,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('5',
style: TextStyle(
fontSize: 20, color: Colors.grey)),
SizedBox(
width: 1,
),
Icon(
Icons.electrical_services,
textDirection: TextDirection.rtl,
size: 20,
color: Colors.grey,
)
],
),
),
),
),
),
),
)
],
),
),
);
}
}
I am making like this .
I removed some unnecessary Lines of Code and voilĂ  there you go:
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.only(top: 30, left: 10, right: 10),
child: Column(
children: [
Row(
children: [
Text(
'Your rooms',
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
Spacer(),
Card(
elevation: 1,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Row(
children: [
Text('add',
style: TextStyle(fontSize: 20, color: Colors.grey)),
Icon(
Icons.add,
size: 20,
color: Colors.grey,
)
],
),
),
)
],
),
SizedBox(
height: 20,
),
Container(
height: 110,
width: 380,
// color: Colors.red,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Color(0xffbbf0e3),
),
child: Center(
child: ListTile(
leading: CircleAvatar( // I ONLY CHANGED THIS PART
radius: 25,
backgroundImage: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkNWsvdJGKCRYW_Wb-K40Po2JQ8LpooWoilw&usqp=CAU'),
),
// leading: Card(
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(30.0)),
// elevation: 1,
// color: Colors.white,
// child: Container(
// height: 80,
// width: 75,
// decoration: BoxDecoration(
// // borderRadius: BorderRadius.circular(25),
// shape: BoxShape.circle,
// // color: Colors.red,
// ),
// child: CircleAvatar(
// radius: 25,
// backgroundImage: NetworkImage(
// 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkNWsvdJGKCRYW_Wb-K40Po2JQ8LpooWoilw&usqp=CAU'),
// ),
// )),
title: Text(
"Living room",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w700),
),
subtitle: Text(
'3 turned on',
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 20,
color: Colors.green),
),
trailing: Card(
elevation: 1,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
width: 50,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('5',
style: TextStyle(
fontSize: 20, color: Colors.grey)),
SizedBox(
width: 1,
),
Icon(
Icons.electrical_services,
textDirection: TextDirection.rtl,
size: 20,
color: Colors.grey,
)
],
),
),
),
),
),
),
)
],
),
),
);
}
}
Output