How to Create Elevated Button with Icon and Text in Flutter - flutter

How to create a button that has text and an icon by using the latest button widget ElevatedButton.

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Elevated Button',
home: FlutterExample(),
);
}
}
class FlutterExample extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Elevated Button with Icon and Text')),
body: Center(
child: ElevatedButton.icon(
icon: Icon(
Icons.home,
color: Colors.green,
size: 30.0,
),
label: Text('Elevated Button'),
onPressed: () {
print('Button Pressed');
},
style: ElevatedButton.styleFrom(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(20.0),
),
),
)));
}
}

you can add Row() or Wrap() Widget with multiple children in this case Icon() and Text()
ElevatedButton(
onPressed:() {},
child: Wrap(
children: <Widget>[
Icon(
Icons.favorite,
color: Colors.pink,
size: 24.0,
),
SizedBox(
width:10,
),
Text("Click me!", style:TextStyle(fontSize:20)),
],
),
),

You can use ElevatedButton.icon constructor:
ElevatedButton.icon(
onPressed: () {
//OnPressed Logic
},
icon: const Icon(Icons.plus_one),
label: const Text(
"Button Text"
)
),

You can use ElevatedButton.icon constructor for this like
ElevatedButton.icon(
icon: const Icon(Icons.add, size: 18),
label: Text('My elevated button'),
onPressed: () {},
),

Related

I can't change my background colour of Textbutton or text in that

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
void main() => runApp(const CustomXylophoneApp());
class CustomXylophoneApp extends StatelessWidget {
const CustomXylophoneApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
TextButton(
onPressed: () {
AudioCache player = AudioCache();
player.play('assets_note1.wav');
TextButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.red);
},
child: Text('Tune1'),
),
You used style in wrong place!
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
backgroundColor: Colors.amber,
),
child: const Text(
'Tune1',
style: TextStyle(color: Colors.black),
),
),
dont use style in void functions like onPressed
You have two option to do that:
ButtonStyle:
TextButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.blue)),
child: Text(
'Tune1',
style: TextStyle(color: Colors.black),
),
),
styleFrom:
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
backgroundColor: Colors.blue,
),
child: Text(
'Tune1',
style: TextStyle(color: Colors.black),
),
),
but I notice that you want to update the background color when play button press, so you can define a variable and when the button pressed change that variable, like this:
Color backgroundColor = Colors.red;
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
TextButton(
onPressed: () {
AudioCache player = AudioCache();
player.play('assets_note1.wav');
setState(() {
backgroundColor = Colors.blue;
});
},
style: TextButton.styleFrom(
backgroundColor: backgroundColor,
),
child: Text('Tune1'),
),
....
}

remove space between bottom nav bar and bottom of screen

I created my own bottom app bar following a tutorial. However, there is a white space between the bottom nav bar and the bottom of the screen. I colored this space in white to show it. How can I make my container the actual nav bar and hide that space?
This is happening on all of my screens in the app regardless if they begin with a scaffold, column, container etc.
import 'package:flutter/material.dart';
class bottomAppBar extends StatelessWidget {
const bottomAppBar({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return BottomAppBar(
color: Colors.white,
child: Container(
color: Color(0xFF313131).withOpacity(0.7),
height: 50,
width: double.maxFinite,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
onPressed: () {
Navigator.pushNamed(context, '/');
},
icon: Icon(
Icons.home,
color: Colors.white,
),
),
IconButton(
onPressed: () {
Navigator.pushNamed(context, '/discover');
},
icon: Icon(
Icons.search,
color: Colors.white,
),
),
IconButton(
onPressed: () {
Navigator.pushNamed(context, '/mybookings');
},
icon: Icon(
Icons.hello,
color: Colors.white,
),
),
IconButton(
onPressed: () {
Navigator.pushNamed(context, '/user');
},
icon: Icon(
Icons.person,
color: Colors.white,
),
),
],
),
),
);
}
}
This happens because you use the scaffold propery from the materail app. But, never wrap the widget with the scaffold widget.
After wraping the widget in scaffold use the bottomNavigationBar property to put your BottomAppBar. Then every thing will work perfect.
Thank you.
Code (Your updated code)
import 'package:flutter/material.dart';
class bottomAppBar extends StatelessWidget {
const bottomAppBar({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomAppBar(
color: Colors.white,
child: Container(
color: Color(0xFF313131).withOpacity(0.7),
height: 50,
width: double.maxFinite,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
onPressed: () {
// Navigator.pushNamed(context, '/');
},
icon: Icon(
Icons.home,
color: Colors.white,
),
),
IconButton(
onPressed: () {
// Navigator.pushNamed(context, '/discover');
},
icon: Icon(
Icons.search,
color: Colors.white,
),
),
IconButton(
onPressed: () {
// Navigator.pushNamed(context, '/mybookings');
},
icon: Icon(
Icons.ac_unit,
color: Colors.white,
),
),
IconButton(
onPressed: () {
// Navigator.pushNamed(context, '/user');
},
icon: Icon(
Icons.person,
color: Colors.white,
),
),
],
),
),
),
);
}
}
Output Screen

flutter:: I have a question about how to use the app top bar

This time I'm making an app top bar in flutter. I wrote code like this.
import 'package:flutter/material.dart';
class VideoAppBar extends StatelessWidget{
const VideoAppBar({Key? key}) : super(key: key);
#override
Widget build(BuildContext context){
return SafeArea(
child: Scaffold(
body: WillPopScope( // <- wraps only the Scaffold body.
child: Center(
),
onWillPop: () {
return Future(() => false);
},
),
appBar: AppBar(
backgroundColor: Colors.white,
title: Text('Very verse',
style: TextStyle(
color: Colors.black,
fontSize: 20,
),),
centerTitle: true,
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
color: Colors.black,
iconSize: 25,
icon: Icon(Icons.arrow_back),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.bookmark_outline),
iconSize: 25,
color: Colors.black,
onPressed: () {
print('GD');
}
),
],
),
),
);
}
}
I'd like to put this code simply as a function on several other pages.
AppBar(title:VideoAppBar);
I figured it could be done like this.
But it doesn't go back.
How should I write the code?
Try below Code hope its helpful to you
Create your regular class or your Widget like below:
import 'package:flutter/material.dart';
void main() {
runApp(
MyApp(),
);
}
class MyApp extends StatelessWidget {
bool shouldPop = true;
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: WillPopScope(
onWillPop: () async {
return shouldPop;
},
child: Scaffold(
appBar: appBar(context),
body: Center(
child: Text('Test'),
),
),
),
);
}
}
Create another dart file like appBar.dart and declare your Appbar Widget in dart file. call your widget any file on your need
appBar(BuildContext context) {
return AppBar(
backgroundColor: Colors.white,
title: Text(
'Very verse',
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
),
centerTitle: true,
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
color: Colors.black,
iconSize: 25,
icon: Icon(Icons.arrow_back),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.bookmark_outline),
iconSize: 25,
color: Colors.black,
onPressed: () {
print('GD');
}),
],
);
}
For WillPopScope class
For Scaffold class
For AppBar
Your result screen ->

Flutter - Icon widget

I'm using an icon widget and I want that when I press on the icon to jump to another place (Like a call icon), and an option to open another widget when I press on the icon (like the three dots icon), my problem was that in the flutter icon widget there is no onlongpress...
any sample code that may help on doing that?
this is my code for now:
child: ListTile(
leading: CircleAvatar(
radius: 25.0,
backgroundColor: Colors.brown,
),
title: Text(helpRequest.category),
subtitle: Text(helpRequest.description),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(
Icons.call,
size: 20.0,
color: Colors.brown[900],
),
onPressed: () {
},
),
IconButton(
icon: Icon(
Icons.more_vert,
size: 20.0,
color: Colors.brown[900],
),
onPressed: () {
},
),
],
),
),
Instead of using IconButton, wrap the Icon with GestureDetector which will give you both onLongPress as well as onTap (onPressed). Please see the code below -
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("Flutter Demo")),
body: MyStatefulWidget(),
),
);
}
}
class MyStatefulWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
Offset _tapPosition;
void _storePosition(TapDownDetails details) {
_tapPosition = details.globalPosition;
}
return ListTile(
leading: const CircleAvatar(
radius: 25.0,
backgroundColor: Colors.brown,
),
title: const Text("helpRequest.category"),
subtitle: const Text("helpRequest.description"),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
GestureDetector(
onTap: () => print("Tap: call"),
onLongPress: () => print("Long Press: Call"),
child: Icon(
Icons.call,
size: 20.0,
color: Colors.brown[900],
),
),
GestureDetector(
onTap: () => print("Tap: more_vert"),
onTapDown: _storePosition,
onLongPress: () async {
final RenderBox overlay =
Overlay.of(context).context.findRenderObject();
final int _selected = await showMenu(
items: [
PopupMenuItem(
value: 1,
child: Row(
children: <Widget>[
const Icon(Icons.delete),
const Text("Delete"),
],
),
),
PopupMenuItem(
value: 2,
child: Row(
children: <Widget>[
const Icon(Icons.edit),
const Text("Edit"),
],
),
)
],
context: context,
position: RelativeRect.fromRect(
_tapPosition &
const Size(40, 40), // smaller rect, the touch area
Offset.zero & overlay.size // Bigger rect, the entire screen
),
);
switch (_selected) {
case 1:
print("delete seleted");
break;
case 2:
print("edit seleted");
break;
}
},
child: Icon(
Icons.more_vert,
size: 20.0,
color: Colors.brown[900],
),
),
],
),
);
}
}

Plese Help me about PreferredSize Debug

Issue in PreferredSize required
this is my code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('flutter'),
leading: IconButton(
icon: Icon(Icons.menu),
onPressed: () {
},
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {
},
),
IconButton(
icon: Icon(Icons.more_vert),
onPressed: () {
},
)
],
flexibleSpace: SafeArea(
child: Icon(
Icons.photo_camera,
size: 75.0,
color: Colors.white70
),
),
),
bottomSheet: (
PreferredSize(
child: Container(
color: Colors.blue,
height: 50.0,
child: Center(
child: Text('Mybutton')
)
),
)
),
body: null,
)
);
}
}
PreferredSize widget need a PreferredSize so you should declare that. It can be fromWidth, fromHeight or fromRadius, I assume you need height for your case, so check below code and write your preferred height value instead of 50.
PreferredSize(
preferredSize: Size.fromHeight(50),
child: Container(