How to resize image inside of a leading property? - image

I am trying to add a logo before the title in the App Bar but it seems the image only takes the width and height of the leading property.
import 'package:flutter/material.dart';
class Home extends StatelessWidget {
Widget build(context){
return Scaffold(
appBar: AppBar(
title:Text('Hi, Andi Loshi'),
backgroundColor: Color.fromRGBO(230, 1, 1,1),
leading: new Image.asset("assets/images/logo.png",
fit:BoxFit.cover,
height:20.00,
width:20.00
),
),
body: Text('Body will reside here')
);
}
}

Though you can not modify size of leading you can add image before title like below in appbar, title please check below code.
import 'package:flutter/material.dart';
void main() => runApp(Home());
class Home extends StatelessWidget {
Widget build(context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Container(
child: Row(
children: [
Image.asset(
"assets/images/logo.png",
fit: BoxFit.cover,
),
SizedBox(
width: 10,
),
Text('Hi, Andi Loshi')
],
),
),
backgroundColor: Color.fromRGBO(230, 1, 1, 1),
),
body: Text('Body will reside here'),
),
);
}
}

#Mussa Kalokola you can just wrap the image in leading with padding that will make it smaller.
I think that's the easiest solution for that.
eg.
appBar: AppBar(
title: Text(_getScreenTitle(context, _selected)),
leading: const Padding(
padding: EdgeInsets.all(16.0),
child: ClipOval(
child: Image(
image: AssetImage('images/test.png'),
),
),
),
),

You can wrap your leading widget inside a Center Widget or set leadingWidth property inside AppBar.
example:
appBar: AppBar(
title: Text('Your Title'),
//leadingWidth: 38.0,
leading: Center(
child: Image(
image: AssetImage('images/test.png'),
),
),

Widgets inside an AppBar have limited size. If you want a custom one, you can implement it from scratch, this article can help you.

You can use the leadingWidth property in the AppBar.
class Home extends StatelessWidget {
Widget build(context){
return Scaffold(
appBar: AppBar(
title:Text('Hi, Andi Loshi'),
backgroundColor: Color.fromRGBO(230, 1, 1,1),
leading: new Image.asset("assets/images/logo.png",
leadingWidth: 80,
fit:BoxFit.cover,
height:20.00,
width:20.00
),
),
body: Text('Body will reside here')
);
}
}

Related

Flutter Image How starting a gif in next build at specific frame from where it left off in previous build

I am trying to design a splash screen. There is a gif that shows in a splash screen. There is no problem of showing the gif.
The problem is that the gif restarts from first frame when the page rebuilds. In my use case, rebuild is expected since a provider listens a firebase user.
My question is that, how can i ensure the gif starts in next build at specific frame from where it left off in previous build.
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:provider_architecture/provider_architecture.dart';
import 'package:myproject/core/viewmodels/startup_view_model.dart';
class StartUpView extends StatelessWidget {
#override
Widget build(BuildContext context) {
FirebaseUser user = Provider.of<FirebaseUser>(context);
ScreenUtil.init(context, width: 411, height: 683, allowFontScaling: true);
return ViewModelProvider<StartUpViewModel>.withConsumer(
viewModel: StartUpViewModel(),
builder: (context, model, child) {
model.handleStartUpLogic(user);
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Image.asset('assets/images/lock.gif', height: 100.h)
],
),
),
);
},
);
}
}
That's interesting. My problem is the complete opposite of yours.
I want my gif to start from the beginning each time I load the gif, but it always starts at the frame that it left off last time.
return SafeArea(
child: Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.black,
title: Text(
getImageName(),
),
centerTitle: true,
),
body: Center(
child: Column(
children: <Widget>[
Expanded(
child: Image (
height: height * 0.75,
width: width * 0.75,
image: AssetImage(getGuideImagePath()),
)
),
However, for your problem, this implementation may help you.
https://www.youtube.com/watch?v=xZ9vdVkI4Vc&feature=youtu.be
https://github.com/aliyigitbireroglu/flutter-image-sequence-animator/tree/master/image_sequence_animator

Is there a way to exclude a BottomAppBar from animations in Flutter?

I tried to wrap it in a Hero widget, as that should achieve what I want. This works with BottomNavigationBar, but not with BottomAppBar, which gives this error: Scaffold.geometryOf() called with a context that does not contain a Scaffold. I tried to give it a context by using Builder, but that did not work either. Here is a sample app to showcase the behaviour:
void main() {
runApp(
MaterialApp(
home: PageOne(),
),
);
}
Widget _bottomNavigationBar() {
return BottomNavigationBar(items: [
BottomNavigationBarItem(icon: Icon(Icons.menu), title: Text('menu')),
BottomNavigationBarItem(icon: Icon(Icons.arrow_back), title: Text('back')),
]);
}
Widget _bottomAppBar() {
return BottomAppBar(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(icon: Icon(Icons.menu), onPressed: null),
IconButton(icon: Icon(Icons.arrow_back), onPressed: null),
],
),
);
}
class PageOne extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: Hero(
tag: 'bottomNavigationBar',
child: _bottomAppBar(),
),
body: Center(
child: IconButton(
iconSize: 200,
icon: Icon(Icons.looks_two),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => PageTwo()),
),
),
),
);
}
}
class PageTwo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: Hero(
tag: 'bottomNavigationBar',
child: _bottomAppBar(),
),
body: Center(
child: IconButton(
iconSize: 200,
icon: Icon(Icons.looks_one),
onPressed: () => Navigator.pop(context),
),
),
);
}
}
The problem seems to be the animation that is used with the Navigation stack. Therefore, getting rid of the animation during the page load will stop this animation. I added the PageRouteBuilder to the PageOne class in your example to get rid of the Navigation stack animation. Use the code below to replace the PageOne class from your example.
class PageOne extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: _bottomAppBar(),
body: Center(
child: IconButton(
iconSize: 200,
icon: Icon(Icons.looks_two),
onPressed: () => Navigator.push(
context,
PageRouteBuilder(
pageBuilder: (context, anim1, anim2) => PageTwo(),
transitionsBuilder: (context, anim1, anim2, child) =>
Container(child: child),
),
),
),
),
);
}
}
There are additional ways to control the animation for Navigation here
(Oh, and I got rid of the Hero() widget)
I have solved this by wrapping the Row with a Hero widget in BottomAppBar. This still allows page transitions, and does not animate the BottomAppBar as intended.
BottomAppBar(
child: Hero(
tag: 'bottomAppBar',
child: Material(
child: Row(
...
),
),
),
);
However, this has laggy animations when using a CircularNotchedRectangle.

How to add a text to an image in flutter

How to add a text to an image in flutter web
How do I add text on the go to an image on flutter web? Is there widget for image editing. How can we achieve this?
You can use either custom painting (other answer found by #F-1) or stack other widget on top of your image (here is excellent example, with text on top of images as you might want.
import 'package:flutter/material.dart';
void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Add Text to an Image"),
),
body:Container(
margin: EdgeInsets.only(top: 50.0),
child: Stack(
children: <Widget>[
Image.network("http://www.panama-offshore-services.com/wp-
content/uploads/2014/08/Tech-business-panama.jpg"),
Container(
margin: EdgeInsets.only(top:20.0),
),
Text("This is text",
style: TextStyle(
color: Colors.red,
fontSize: 18.0
),)
],
),
)
),
);
}
}
// use stack to add text on image

Flutter navigation drawer hamburger icon color change

Hamburger icon color of navigation drawer is not changing. Its black by default. I want to change the this icon color in flutter, I am stuck, help me to change this icon color. here is my code.
class Test extends StatefulWidget {
#override
_TestState createState() => new _TestState();
}
class _TestState extends State<Test> {
#override
Widget build(BuildContext context) {
return new Scaffold(
drawer: new Drawer(),
appBar: new AppBar(
title: new Text("Navigation Drawer")
),
),
);
}
}
Add iconTheme to your AppBar
#override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(),
appBar: AppBar(
title: Text("Navigation Drawer"),
iconTheme: IconThemeData(color: Colors.green),
),
);
}
You can also check other solutions here.
You can also use following in Theme's data property
Theme(
data: ThemeData(primaryIconTheme: IconThemeData(color: Colors.red)), // use this
child: Scaffold(),
)
Or
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.menu, color: Colors.red), // set your color here
onPressed: () {},
),
),
To change color of your icon use this
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: AppBar(title: new Text('List view example'),
leading: new Icon(Icons.menu,color: Colors.green,),
),
),
);
}
Icon(Icons.menu,color: Colors.green,) define color inside Icon
Use iconTheme in Appbar like this:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("App Bar"),
iconTheme: IconThemeData(color: Colors.black),
),
drawer: Drawer(),
);
}
This is the only solution to make the button clickable otherwise you need to openDrawer onTap.
AppBar(
iconTheme: const IconThemeData(
size: 40, //change size on your need
color: Colors.black, //change color on your need
),
),
Using The iconTheme for Appbar is not currently working with useMaterial3 = true, And all these answers defined a leading icon for the Appbar without telling how to implement it's onPress behavior, So the best way to change the Drawers icon or it's color is this :
Declare the key for Scaffold :
final scaffoldKey = GlobalKey<ScaffoldState>();
And apply it to Scaffold:
Scaffold(
key: scaffoldKey,
drawer: Drawer()
)
Then , Apply the drawer icon like below with click action:
AppBar(
title: Text("My AppBar"),
leading: IconButton(
icon: Icon(Icons.person),
onPressed: (){
if(scaffoldKey.currentState!.isDrawerOpen){
scaffoldKey.currentState!.closeDrawer();
//close drawer, if drawer is open
}else{
scaffoldKey.currentState!.openDrawer();
//open drawer, if drawer is closed
}
},
),
)
You can change it from main.dart easily this way-
return MaterialApp(
title: 'XYZ',
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: AppBarTheme(
iconTheme: IconThemeData(color: Colors.black),
actionsIconTheme: IconThemeData(color: Colors.blue),
backgroundColor: theme.backgroundColor,
elevation: 0,
),
),

How should I customize DropdownButtons and DropdownMenuItems in Flutter?

The default DropdownButton with DropdownMenuItems returns a light-grey dropdown. How should I customize the dropdown (e.g. background color, dropdown width)? I can change the style property in both DropdownButton and DropdownMenuItem, like this:
return new DropdownButton(
value: ...,
items: ...,
onChanged: ...,
style: new TextStyle(
color: Colors.white,
),
);
but this doesn't change the dropdown's background color.
Should I copy DropdownMenu and extend it? Does Flutter plan to add customization for this widget in the near future?
You can accomplish this by wrapping the DropdownButton in a Theme widget and overriding the canvasColor.
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
State createState() => new MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
int _value = 42;
#override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.blue.shade200,
),
child: new DropdownButton(
value: _value,
items: <DropdownMenuItem<int>>[
new DropdownMenuItem(
child: new Text('Foo'),
value: 0,
),
new DropdownMenuItem(
child: new Text('Bar'),
value: 42,
),
],
onChanged: (int value) {
setState(() {
_value = value;
});
},
),
),
),
);
}
}
I was able to change the background for the Dropdown by wrapping it in a Container with the color property set.
Before:
After:
Here's the code:
Define these values in the widget state:
final items = ['One', 'Two', 'Three', 'Four'];
String selectedValue = 'Four';
then use this code
Container(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(10)),
// dropdown below..
child: DropdownButton<String>(
value: selectedValue,
onChanged: (String newValue) =>
setState(() => selectedValue = newValue),
items: items
.map<DropdownMenuItem<String>>(
(String value) => DropdownMenuItem<String>(
value: value,
child: Text(value),
))
.toList(),
// add extra sugar..
icon: Icon(Icons.arrow_drop_down),
iconSize: 42,
underline: SizedBox(),
),
);
As Collin said, your DropdownMenuItem will follow your ThemeData class. Not only its backgroundColor will match the canvasColor in your ThemeData class, but also it will follow the same TextStyle.
So, for a quick example:
new ThemeData(
fontFamily: "Encode Sans", //my custom font
canvasColor: _turquoise, //my custom color
//other theme data)
Furthermore, if you want to control the width of the menu, you can feed its child property a new Container and add the desired width, check the following GIF, I started with width: 100.0 then hot reloaded after changing it to 200.0, notice how the width was manipulated, just make sure you use a suitable width so that you do not get overflow problems later on when you use the menu within a more complex layout.
class TestPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title:new Text ("Test"),
),
body: new Center(
child: new DropdownButton(items: new List.generate(20, (int index){
return new DropdownMenuItem(child: new Container(
child: new Text ("Item#$index"),
width: 200.0, //200.0 to 100.0
));
})
, onChanged: null)
),
);
}
}
If you want the DropdownButton to fill the space that it is in, use the property isExpanded and set it to true
DropdownButton<String>(
isExpanded: true,
)
You can do something very simple in the latest version of Flutter.
The DropdownButton class has an inbuilt variable called 'dropdownColor' which can be assigned any color you need directly, without changing any 'ThemeData'. Automatically changes the color of the dropdown menu items as well.
Use this for color
DropdownButtonFormField(
items: null,
onChanged: null,
dropdownColor: Colors.red,
),
https://api.flutter.dev/flutter/material/DropdownButton/style.html will help you to figure out some stylings.
DropdownButton(
dropdownColor: Colors.grey,
value: this.repeatType,
onChanged: (String? value) {
print(value);
setState(() {
this.repeatType = value!;
});
},
selectedItemBuilder: (BuildContext context) {
return this.repeatTypes.map((String value) {
return Text(
this.repeatType,
style: const TextStyle(color: Colors.white),
);
}).toList();
},
items: this
.repeatTypes
.map((item) => DropdownMenuItem(
child: Text(
item,
style: TextStyle(color: Colors.green),
),
value: item,
))
.toList())
You can wrap it with container like this:
Container(
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.white)
),
child: DropdownButton(
dropdownColor: Colors.white,
style: TextStyle(
color: Colors.black,
backgroundColor: Colors.white,
),
value: 'ar',
items: [
DropdownMenuItem(child: Text('English'), value: 'en'),
DropdownMenuItem(child: Text('العربية'), value: 'ar'),
],
),
)
The output:
It's so simple now,
Just use the dropdownColor property inside the DropdownButton widget like this:
DropdownButton(
dropdownColor: Colors.red, // here you change the background color
value: 'Your value',
items: [ ],
)
you can use the dropdown_button2 package.
You can use the dropdownDecoration property to customize the dropdown menu appearance. This is the best package I found to fully customize a DropdownButton

Resources