How to refresh text in flutter - android

I have this Text
I want when I clicked on add Icon to refresh 30 to 31
this is my code :
Row(
children: <Widget>[
Expanded(
child: Container(
height: 30.0,
width: 30.0,
child: Image.asset('assets/images/trash_can.png'),
),
),
Expanded(
child: Center(
child: Text(
mealsNum.toString(),
style: TextStyle(
fontSize: 20.0
),
),
),
),
Expanded(
child: GestureDetector(
onTap: (){
mealsNum++;
print(mealsNum);
},
child: Icon(
Icons.add,
color: Color(0xffFFD243),
),
),
),
],
),
How can I do this?

use a set state to update
onTap: (){
setState((){
mealsNum++;
});
print(mealsNum);,

You need to add setState in onTap
Row(
children: <Widget>[
Expanded(
child: Container(
height: 30.0,
width: 30.0,
child: Image.asset('assets/images/trash_can.png'),
),
),
Expanded(
child: Center(
child: Text(
mealsNum.toString(),
style: TextStyle(
fontSize: 20.0
),
),
),
),
Expanded(
child: GestureDetector(
onTap: (){
setState(() {
mealsNum++;
});
print(mealsNum);
},
child: Icon(
Icons.add,
color: Color(0xffFFD243),
),
),
),
],
),

setSate is used for rebuilding the widgets. Add setstate in onTap
Row(
children: <Widget>[
Expanded(
child: Container(
height: 30.0,
width: 30.0,
child: Image.asset('assets/images/trash_can.png'),
),
),
Expanded(
child: Center(
child: Text(
mealsNum.toString(),
style: TextStyle(
fontSize: 20.0
),
),
),
),
Expanded(
child: GestureDetector(
onTap: (){
setState(() {
mealsNum++;
});
print(mealsNum);
},
child: Icon(
Icons.add,
color: Color(0xffFFD243),
),
),
),
],
),

you should learn state management in flutter here is the link to learn the basic from flutter basic doc after you read this you maybe ask what about global state or more complicated state mangement then you can choose provider or for more advance use flutter_block here is the link for flutter_block library made with example
,have a good luck with state management

Related

How to remove padding from top of ListTile in Drawer?

I can not remove the indent from the first ListTile in any way. I think this is some kind of default indent, but I could not overcome it. Is it possible? Attached is the code and screenshot.
Drawer(
child: Container(
padding: EdgeInsets.zero,
child: Column(
children: <Widget>[
SizedBox(
height: 88.0,
width: 1000,
child: DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(
'Настройки',
style: TextStyle(
color: Colors.white,
fontSize: MainStyle.p.fontSize
),
),
),
),
Expanded(
child: Column(
children: <Widget>[
ListTile(
title: Text(
'Язык',
style: TextStyle(
fontSize: MainStyle.p.fontSize
),
),
subtitle: Text(
"Русский",
),
trailing: IconButton(
icon: Icon(Icons.edit),
color: Colors.black,
onPressed: () {
},
),
),
ListTile(
title: Text(
'Выход',
style: TextStyle(
fontSize: MainStyle.p.fontSize
),
),
onTap: () {
exit();
},
),
],
)
),
Container(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: Column(
children: <Widget>[
ListTile(
title: Text(
globalState.version,
style: TextStyle(
fontSize: 15,
color: Colors.grey
),
textAlign: TextAlign.center,
),
),
],
)
)
),
],
),
)
);
P.S. There is not any details, but StackOverflow make me write more text, because of "It looks like your post is mostly code; please add some more details."
add this line to your DrawerHeader:
margin: EdgeInsets.zero,

Dropdown menu for drawer header [Flutter]

Hi what I want to do is make a dropdown menu inside of a drawer header but when I tried I got constraint errors also Im curious how can I pass 2 values into it so when pressed the text will change according to it
My code:
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.20,
child: DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
children: [
Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: Column(
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
widget.name,
style: TextStyle(
color: Colors.amber,
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
),
SizedBox(
height: 3,
),
Align(
alignment: Alignment.centerLeft,
child: Text(
widget.name,
style: TextStyle(
color: Colors.white,
fontSize: 14,
fontWeight: FontWeight.w400,
),
),
),
],
),
),
),
Expanded(
child: Align(
alignment: Alignment.centerRight,
child: Icon(Icons.keyboard_arrow_down),
),
)
],
),
),
),
),
SingleChildScrollView(
child: Column(
children: [
AppListItems(Icons.favorite, 'Text', SecondRoute()),
AppListItems(Icons.favorite, 'Text', SecondRoute()),
],
),
),
],
),
);
This is how it looks like and where I want the dropdown menu to be:
Any help would be great, thanks in advance
Try below code hope its help to you.
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.20,
child: DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
children: [
Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: Column(
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
'widget.name',
style: TextStyle(
color: Colors.amber,
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
),
SizedBox(
height: 3,
),
Align(
alignment: Alignment.centerLeft,
child: Text(
'widget.name',
style: TextStyle(
color: Colors.white,
fontSize: 14,
fontWeight: FontWeight.w400,
),
),
),
],
),
),
),
Expanded(
child: DropdownButton<String>(
value: dropdownValue,
isDense: true,
icon: const Icon(Icons.arrow_downward),
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
)
],
),
),
),
),
SingleChildScrollView(
child: Column(
children: [
Icon(
Icons.favorite,
),
Icon(
Icons.favorite,
),
],
),
),
],
),
),
Your result screen->
Your dropdown->

How to add vertical spacing to widgets Flutter

I have a piece of code here which makes up the body of the login and register screens. Everything is in the position i want but i would like to space them out a little. The two input fields, title, subtitle, button and textbutton are too close together vertically.
I tried to use mainaxisalignment.spacebetween but i get a constraint error and everything i have on the screen disappears afterwards except the background.
The body used by the register and login screen:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomLeft,
colors: [Color(0xFF1F1F1F), Color(0xFF1F1F1F)],
),
image: DecorationImage(
image: AssetImage("lib/assets/images/register.png"),
alignment: Alignment.topCenter,
),
),
child: Stack(
children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('lib/assets/images/bottomleft.png'),
alignment: Alignment.bottomLeft,
),
),
),
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('lib/assets/images/toptop.png'),
alignment: Alignment.topRight,
),
),
),
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('lib/assets/images/topbig.png'),
alignment: Alignment.topCenter,
repeat: ImageRepeat.repeat),
),
),
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('lib/assets/images/top.png'),
alignment: Alignment.topCenter,
repeat: ImageRepeat.repeatX),
),
),
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 25, vertical: 250),
child: ListView(
children: [
if (onBackPressed == null) verticalSpaceLarge,
if (onBackPressed != null) verticalSpaceRegular,
if (onBackPressed != null)
IconButton(
padding: EdgeInsets.zero,
onPressed: onBackPressed,
alignment: Alignment.centerLeft,
icon: Icon(
Icons.arrow_back_ios,
color: Colors.black,
),
),
Center(
child: Text(
title!,
style: TextStyle(
color: Colors.white,
fontSize: 30.0,
fontWeight: FontWeight.bold,
fontFamily: 'Montserrat',
),
),
),
Align(
alignment: Alignment.center,
child: SizedBox(
child: Center(
child: Text(
subtitle!,
style: ktsMediumGreyBodyText,
),
),
),
),
form!,
if (onForgotPassword != null)
Align(
alignment: Alignment.centerRight,
child: GestureDetector(
onTap: onForgotPassword,
child: Center(
child: Text(
'Forgot Password?',
style: ktsMediumGreyBodyText.copyWith(),
),
),
),
),
if (validationMessage != null)
Center(
child: Text(
validationMessage!,
style: TextStyle(
color: Colors.red, fontSize: kBodyTextSize),
),
),
if (validationMessage != null) verticalSpaceRegular,
GestureDetector(
onTap: onMainButtonTapped,
child: Container(
width: double.infinity,
height: 50,
alignment: Alignment.center,
decoration: BoxDecoration(
color: kcPrimaryColor,
borderRadius: BorderRadius.circular(8),
),
child: busy!
? CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.white),
)
: Text(
mainButtonTitle!,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 14),
),
),
),
if (onCreateAccountTapped != null)
GestureDetector(
onTap: onCreateAccountTapped,
child: Center(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Don\'t have an account?",
style: TextStyle(color: Colors.white),
),
Text(
"SIGN UP",
style: TextStyle(color: kcPrimaryColor),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"OR SIGN IN WITH",
style: TextStyle(color: kcMediumGreyColor),
),
],
),
],
),
),
),
if (showTermsText!)
Center(
child: Text(
"By signing up you agree to our terms, conditions and privacy policy",
style: ktsMediumGreyBodyText,
textAlign: TextAlign.center,
),
),
],
),
),
],
),
),
);
}
}
the login screen:
Widget build(BuildContext context) {
return ViewModelBuilder<LoginScreenModel>.reactive(
builder: (context, model, child) => Scaffold(
body: AuthenticationLayout(
busy: model.isBusy,
onCreateAccountTapped: () {},
//validationMessage: model.validationMessage,
title: 'SIGN IN',
subtitle: 'please fill the form below',
mainButtonTitle: 'SIGN IN',
form: Column(
children: [
TextField(
decoration: InputDecoration(
hintText: 'Username',
hintStyle: TextStyle(color: Colors.white),
prefixIcon: const Icon(
Icons.person,
color: Colors.white,
),
),
),
TextField(
decoration: InputDecoration(
hintText: 'Password',
hintStyle: TextStyle(color: Colors.white),
prefixIcon: const Icon(
Icons.lock,
color: Colors.white,
),
),
),
],
),
onForgotPassword: () {},
),
),
viewModelBuilder: () => LoginScreenModel(),
);
}
}
Column(
children: <Widget>[
FirstWidget(),
SizedBox(height: 100),
SecondWidget(),
],
),
You should Add SizedBox() in between two Widgets, or Set Padding to your widgets or you use Container also like below:
Using SizedBox()
Column(
children:[
Widget 1(),
SizedBox(height:10),
Widget 2(),
SizedBox(height:10),
Widget 3(),
],
),
Using Padding()
Column(
children:[
Padding(
padding:EdgeInsets.all(8.0)
child: Widget 1(),
),
Padding(
padding:EdgeInsets.all(8.0)
child: Widget 2(),
),
Padding(
padding:EdgeInsets.all(8.0)
child: Widget 3(),
),
],
),
Using Container()
Column(
children:[
Container(
padding:EdgeInsets.all(8.0)
child: Widget 1(),
),
Container(
padding:EdgeInsets.all(8.0)
child: Widget 2(),
),
Container(
padding:EdgeInsets.all(8.0)
child: Widget 3(),
),
],
),
You should use padding on your choice like:
EdgeInsets.all(8.0),
EdgeInsets.only(left:8.0,top:8.0,right:8.0,bottom:8.0),
EdgeInsets.symmetric(vertical: 8.0,horizontal:8.0),
You may add some SizedBox() between or set margin to the components.
Use SizedBox widget to add vertical spacing to your widget.
For e.g
SizedBox(height: 16),

Flutter Flatbutton onpressed not working while trying to open phone dialer

Hello new to flutter here. I am trying to open phone dialer with a predefined phone number after pressing Call button that is Flatbutton inside Positioned, but it is not working and does not show any error either. I tried printing some values on onpressed as well but it did not print any. I have called this widget in another dart file.
Here i am using url_launcher package to launch the phone dialog.
If any other alternative way please help.
(Removed some unimportant design codes below)
class CallCard extends StatefulWidget {
#override
_CallCardState createState() => _CallCardState();
}
class _CallCardState extends State<CallCard> {
#override
Widget build(BuildContext context) {
return Positioned(
bottom: -170,
child: Container(
child: Column(
children: <Widget>[
SizedBox(
height: 15,
),
Text(
'Are you feeling well today?',
style: TextStyle(
fontSize: 24.0,
),
),
Text(
'Give us a call or visit our website.',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.w300,
),
),
SizedBox(
height: 23.0,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
FlatButton(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15),
color: Color(0xff9ce47c),
onPressed: () {
final String phone = "01-4441577";
launch(phone);
print('here');
},
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.black),
borderRadius: BorderRadius.circular(50),
),
child: Row(
children: <Widget>[
Icon(
LineAwesomeIcons.phone,
size: 22,
),
SizedBox(
width: 5.0,
),
Text(
'Call Now',
style: TextStyle(
fontSize: 16,
),
),
],
),
),
SizedBox(
width: 1.0,
),
],
),
),
],
)
],
),
));
}
}
This is the code where I have called this widget (inside Stack)
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Stack(
alignment: Alignment.topCenter,
overflow: Overflow.visible,
children: <Widget>[
_backgroundCover(),
//content inside header
Positioned(
top: 80,
left: 30,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
//crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Hello User',
style: TextStyle(
fontSize: 36,
fontWeight: FontWeight.w500,
color: Colors.black,
),
),
SizedBox(width: 10.0,),
// Padding(padding: EdgeInsets.only(right: 200.0)),
IconButton(
icon: Icon(
LineAwesomeIcons.power_off,
size: 40.0,
),
onPressed: () {
DialogHelper.exit(context);
// await _auth.signOut();
}),
],
),
),
CallCard(),
],
),
],
),
),
);
Did you try putting the print before launch? It could be that launch is getting stuck and then the print never gets reached (this has happened to me with certain packages).
I am not sure how launch works with phone numbers but you may want to look into if youre using it properly.
I'm ok with this code.
launch('tel:$yourPhoneNo');

Create Sign-in-with-Google button in Flutter in accordance with the terms

I'd like to add a "Sign in with Google" Button to my Flutter app. This button should be in accordance with the terms of Google.
My problem is, that the button I've create looks really awful.
I used the images which Google provides on their website but I don't know if I'm doing right with the code for the button.
Widget _createLoginButtonGoogle() {
return new Expanded(
child: new Container(
margin: EdgeInsets.fromLTRB(30.0, 5.0, 30.0, 5.0),
child: new RaisedButton(
color: const Color(0xFF4285F4),
shape: _createButtonBorder(),
onPressed: () {},
child: new Row(
children: <Widget>[
new Image.asset(
'res/images/icons/google/btn_google_dark_normal_mdpi.9.png',
height: 48.0,
),
new Expanded(
child: _createButtonText('Sign in with Google', Colors.white),
),
],
),
),
),
);
}
What I want is that my button looks like the original Google button
And not like my version
Can anyone tell me how to create the original google button? Is there a better way than creating a RaisedButton?
There Is A Pretty Awesome Package Called flutter_signin_button on pub.dev.
You Can Use It It Has Sign In Buttons For
Email
Google
Facebook
GitHub
LinkedIn
Pinterest
Tumblr
Twitter
Apple
With Some Supporting Dark Mode Also With Mini Buttons!
First Add It To Your pubspec.yaml
dependencies:
...
flutter_signin_button:
then import it into your file:
import 'package:flutter_signin_button/flutter_signin_button.dart';
and use the buttons like this:
SignInButton(
Buttons.Google,
onPressed: () {},
)
Here Is A Preview Of All The Buttons:
I'm giving you a general idea, replace Android icon with your Google image using Image.asset(google_image)
InkWell(
onTap: () {},
child: Ink(
color: Color(0xFF397AF3),
child: Padding(
padding: EdgeInsets.all(6),
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Icon(Icons.android), // <-- Use 'Image.asset(...)' here
SizedBox(width: 12),
Text('Sign in with Google'),
],
),
),
),
)
you can use Padding property of raised button also use property of Row widget and mainAxisSize of button. Following code may help you to understand clearly.
new Container(
margin: EdgeInsets.fromLTRB(30.0, 5.0, 30.0, 5.0),
child: new RaisedButton(
padding: EdgeInsets.only(top: 3.0,bottom: 3.0,left: 3.0),
color: const Color(0xFF4285F4),
onPressed: () {},
child: new Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Image.asset(
'res/images/icons/google/btn_google_dark_normal_mdpi.9.png',
height: 48.0,
),
new Container(
padding: EdgeInsets.only(left: 10.0,right: 10.0),
child: new Text("Sign in with Google",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),)
),
],
)
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.white,
onPrimary: Colors.black,
),
onPressed: () {
googleLogin();
},
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 8, 0, 8),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Image(
image: AssetImage("assets/google_logo.png"),
height: 18.0,
width: 24,
),
Padding(
padding: EdgeInsets.only(left: 24, right: 8),
child: Text(
'Sign in with Google',
style: TextStyle(
fontSize: 20,
color: Colors.black54,
fontWeight: FontWeight.w600,
),
),
),
],
),
),
),
for more you can change the Text font to Roboto Medium and make the size 14x, based on the branding rule https://developers.google.com/identity/branding-guidelines#font
use this one
child: InkWell(
onTap: () {},
child: Container(
height: 50,
width: CustomWidth(context) - 100,
color: Colors.blueAccent,
child: Row(
children: [
Container(
margin: EdgeInsets.symmetric(horizontal: 3),
height: 45,
width: 50,
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
image: NetworkImage("https://cdn-icons-png.flaticon.com/512/2991/2991148.png"))),
),
SizedBox(
width: 10,
),
DefaultTextView(
text: "SignUp With Google",
color: AppColors.whiteColor,
fontweight: FontWeight.bold,
)
],
),
),
),
You can use this class and just need to pass function and add google icon in your assets/images folder and change the icon name in code, and you are good to go.
import 'package:flutter/material.dart';
import 'package:social_app/constants/colors.dart';
class GoogleSignInButton extends StatefulWidget {
final function;
final String buttonText;
const GoogleSignInButton(
{super.key, required this.function, required this.buttonText});
#override
State<StatefulWidget> createState() => GoogleSignInButtonState();
}
class GoogleSignInButtonState extends State<GoogleSignInButton> {
bool isButtonClicked = false;
#override
Widget build(BuildContext context) {
return Container(
height: 50,
width: double.infinity,
child: ElevatedButton(
onPressed: isButtonClicked
? () {}
: () async {
setState(() {
isButtonClicked = true;
});
await widget.function();
setState(() {
isButtonClicked = false;
});
},
style: ButtonStyle(
padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.zero),
shape: isButtonClicked
? MaterialStateProperty.all<CircleBorder>(CircleBorder())
: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
)),
backgroundColor: MaterialStateProperty.all<Color>(mainColor),
),
child: isButtonClicked
? Container(
height: 25,
width: 25,
child: CircularProgressIndicator(
color: Colors.white,
),
)
: Container(
padding: EdgeInsets.all(5),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(3)),
color: Colors.white,
),
padding: EdgeInsets.all(5),
child: Image.asset("assets/images/google_icon.png"),
),
SizedBox(
width: 30,
),
Text(
"Sign in with google",
style: TextStyle(fontSize: 20),
)
],
),
),
),
);
}
}
For google logo image on white background and raised button with blue background:
Container(
margin: EdgeInsets.fromLTRB(30.0, 5.0, 30.0, 5.0),
child: new RaisedButton(
padding: EdgeInsets.all(1.0),
color: const Color(0xff4285F4),
onPressed: () async {
_signInWithGoogle();
},
child: new Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
),
child: Image.asset(
Images.googleLogo,
height: 18.0,
),
),
Container(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
child: new Text(
"Sign in with Google",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
)),
],
)),
),

Categories

Resources