How do I make the Navigation Drawer of my Flutter app transparent? - android-studio

I have built a native Android App which has a transparent navigation drawer. I have been asked to build the same app using Flutter and I have gotten to the point where I would like to implement a transparent navigation drawer. How do I make the Navigation Drawer of my Flutter app transparent because I have been struggling on that end ? I have already tried
drawer: Drawer(
child: Container(
color: Colors.transparent,)),
The navigation drawer just remains white. I have been searching for a solution to this and cant find one. Any help would be appreciated.
I have attached images of the Native App with a transparent drawer and the Flutter version with a white Navigation drawer

I think there's a better way of doing this without messing up the entire canvases on the app. Since you want it specifically for the drawer, try this approach.
Scaffold(
drawer: Theme(
data: Theme.of(context).copyWith(
// Set the transparency here
canvasColor: Colors.transparent, //or any other color you want. e.g Colors.blue.withOpacity(0.5)
),
child: Drawer(
// All other codes goes here.
)
)
);

use the transparent color like you are currently doing but also in the drawer widget use a stack and inside it make the first widget a backdropfilter, you will need to import dart:ui. here is an example
//import this library to use the gaussian blur background
import 'dart:ui';
Scaffold(
appBar: AppBar(
title: Text('Title'),
),
drawer: Theme(
data: Theme.of(context).copyWith(canvasColor: Colors.transparent),
child: sideNav()),
body: Text('Hello Body'),
),
Drawer sideNav(){
return Drawer(
child: Stack(
children: <Widget> [
//first child be the blur background
BackdropFilter(
filter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0), //this is dependent on the import statment above
child: Container(
decoration: BoxDecoration(color: Color.grey.withOpacity(0.5))
)
),
ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Hello Drawer Title')
),
ListTitle(
leading: Icon(Icons.dashboard, color: Colors.white)
title: "Dashboard"
onTap: (){
}
)
]
)
]
)
);
}

After much tinkering around I managed to find a solution.
I edited the ThemeData and added a canvas color as described below
theme: new ThemeData(
canvasColor: Colors.transparent
),
This isn't the best way to do this, it is more of a workaround than anything.
Visual Representation

Screenshot Of drawer top whitespace
If you came here and finding the solution about how to remove the white space above the drawer and status bar then just simply use SingleChildScrollView ---> Column().
Because if you add something like ListView() then the white Space will take place above your drawer which is so irritating to see.
I know this is not the actual solution of this problem but it will help someone who needs it.

Just wrap the drawer with opacity and give the opacity a value (between 0 and 1)
Opacity(
opacity: 0.7,
child: Drawer(//your drawer here),
),

Related

Flutter: Fixed bottom button, central dynamic TextField that moves with keyboard popup

Many variations of this question exist with solutions. I have been unable to find an adequate solution. Is it possible to fix a button at the bottom of the screen which the keyboard is allowed to overlay, but also moves the other text fields up?
I've attached a screenshot example
My current solution works, but the space between the textfield and the post button is a fixed SizedBox height. This shifts the post button around on different screen heights, which is hack.
Is there a way to do something similar to how an Expanded widget fills the empty space, but within a ListView? Expanded throws an error if parent is a ListView, and a parent of Flex would squeeze all the elements when the keyboard comes up.
Widget Layout: (super simplified)
return ListView(
children: <Widget>[
Container(//The photo boxes at top
height: fixed,
),
ListTile(
leading: CircleAvatar(),
title: Container(child: TextField(),),
),
SizedBox(
height: screenHeight * 0.5 - 225
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: RaisedButton(
onPressed: doThing
child: Text('POST'),
),
),
],
),
],
);

Widget not displaying on Stack and there is no Ink effect on InkResponse

I am working on my first Flutter App and I have encountered two problems.
- The Container widget housing my Image widget does not get displayed on the Stack widget.
There is no Ink effect when tapping the InkResponse container
I have tried rewriting the Container to display the image and by using Image.Network for the url instead of assets but to no avail.
final appLogo = new Container(
child: new Image(
image: new AssetImage('assets/discord.png')
),
);
List<Widget> _getApps(List apps) {
List<Widget> _appWidgets = <Widget>[];
// make a Grid tile of the Apps
for (int i = 0; i < apps.length; i++) {
_appWidgets.add(
new InkResponse(
child: Center(
child: new Stack(
children: <Widget>[
appLogo,
new Container(height: 102, width: 102, color: Colors.red),
],
)
)
),
);
}
return _appWidgets;
}
appLogo should be displayed on the red box (Container) from my expectation and there should be a Splash when I tap on the InkResponse widget.
First of all, your image needs to be added to your pubspec.yaml file like so:
flutter:
assets:
- images/
Once you have that, you can access all your images inside "/images" or any folder name inside your project you want. Now you can access your image in this way:
Image.asset("images/myimage.jpg") // again, this in an example
Bear in mind that images are not affected by the ripple material effect, only the background of it (if the image is inside a bigger container with "empty" space). Second, you need an InkWell with onTap: method in order to show the ripple, to finish everything, you need a Material widget as this one provides the necessary effects.
So, if you want to see a the ripple effect behind the image and having it inside a Stack, you'll need to do something like:
Material(
child: Stack(
children: <Widget>[
InkWell(
onTap: () {}, // The ripple only shows up if you have a onTap method.
child: Container(
height: 300, // 300 is a random value but has bigger height than the image itself.
child: Image.asset("images/myimage.jpg"),
),
),
],
),
)

Flutter: How do you make a card clickable?

I just have a simple Card like new Card(child: new Text('My cool card')) and I want to be able to click anywhere on it to run some function, except there's no onPressed method for a Card. I could add a button to the bottom, but that's not ideal for this situation.
Anyone know how to make the whole card clickable?
Flutter use composition over properties.
Wrap the desired widget into a clickable one to achieve what you need.
Some clickable widgets : GestureDetector, InkWell, InkResponse.
GestureDetector(
onTap: () => ......,
child: Card(...),
);
Flutter provides the InkWell Widget. by registering a callback you can decide what happens when user clicks on the card (called tap in flutter). InkWell also implements Material Design ripple effect
Card(
child: new InkWell(
onTap: () {
print("tapped");
},
child: Container(
width: 100.0,
height: 100.0,
),
),
),
I think you can also use InkWell apart from GestureDetector just wrap the card inside InkWell() Widget
InkWell(
onTap: (){ print("Card Clicked"); }
child: new Card(),
);
You can use Inkwell and insert splashColor which, at the click of the user, creates the rebound effect with the chosen color, on the card ..
This is mainly used in material design.
return Card(
color: item.completed ? Colors.white70 : Colors.white,
elevation: 8,
child: InkWell(
splashColor: "Insert color when user tap on card",
onTap: () async {
},
),
);
Wrap a card in GestureDetector Widget like a below:
GestureDetector(
onTap: () {
// To do
},
child: Card(
),
),
Another way is as follows:
InkWell(
onTap: () {
// To do
},
child: Card(),
),
In Flutter, InkWell is a material widget that responds to touch action.
InkWell(
child: Card(......),
onTap: () {
print("Click event on Container");
},
);
GestureDetector is a widget that detects the gestures.
GestureDetector(
onTap: () {
print("Click event on Container");
},
child: Card(.......),
)
Difference
InkWell is a material widget and it can show you a Ripple Effect whenever a touch was received.
GestureDetector is more general-purpose, not only for touch but also for other gestures.
The most preferred way is to add ListTile as Card child. Not only does ListTile contain the method onTap it also helps you in making Card interesting.
Card(
child: ListTile(
title: Text('Title')
leading: CircleAvatar(
backgroundImage: AssetImage('assets/images/test.jpg'),
),
onTap: () {
print('Card Clicked');
},
),
),
You also can insert a card into a TextButton:
TextButton clickableCard = TextButton(child: card, onPressed: onCardClick, style: [...]);
This brings the advantage, that you get some features for free. For example in Flutter Web, you get a mousover effect and the cursor changes to the hand so that ths user knows, he can click there. Other additional features can be customised using the style.
Do something on tap/click of 'child' in Flutter:-
Code:-your code looks like:
child: Card(------
------------
--------),
Step1:- Put your mouse cursor on Card then, press- Alt+Enter(in windows) select wrap with widget.
Step2:- Change your default widget into GestureDetector.
final code:-
child: GestureDetector(
onTap: YourOnClickCode(),
child: Card(------
------------
--------),
),
Most of the answers are brilliant but I just want to share mine for the one who wants to make/show a ripple effect on Tap of card or list tile.
Card(
child: TextButton(
onPressed: ()=> ...,
child: ListTile(
title: Text('title'),
),
),
);

Flutter LinearLayout weight alternative

I'm Android developer learning flutter. I want my screen looks like this:
|---------------------------------|
| |
| Babe I miss you |
| |
|---------------------------------|
"Babe" and "I miss you" should be two separate elements.
With Android xml I would solve this with LinearLayout and two TextViews with weight=1 for each. What's the alternative for flutter?
P.S. I know that you can solve it with FrameLayout or with RelativeLayout but I want closest to LinearLayout behavior.
Flutter's Row widget is equivalent to android's LinearLayout with android:orientation="horizontal",
and Column widget is equivalent to android's LinearLayout with android:orientation="vertical".
flex property of Flexible widget is equivalent weight property, you can wrap the Text widgets in a Flexible widget and specify the flex property.
Example:
new Row(
children: <Widget>[
new Flexible(child: new Text("Babe"), flex: 1,),
new Flexible(child: new Text("I miss you"), flex: 1,)
],
)
Using an Expanded widget can also produce a LinearLayout effect like so:
Row(
children: <Widget>[
Expanded(
child: Container(child: Text("Babe")),
flex: 2,
),
Expanded(
child: Container(child: Text("I don't miss you"),alignment: Alignment.centerRight),
flex: 2,
),
],
),
Considering the layout in the question you want to display both the texts at the end of screen one at the beginning other at the end you can simply add MainAxisAlignment.spaceBetween property to the row
so you need to modify your code to
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
new Text('Babe'),
new Text('I miss you')
]
)
I dont understand why first answer was accepted because it simply adds both the text to right most end of the screen,
Tip: Incase you need some padding around each text wrap each of the text in a Container and add desired padding and margin
You can use row with MainAxisAlignment which will allow you to place elements as per your expectation
Widget textSection = new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
new Text('Babe'),
new Text('I miss you')
)
)
Screenshot:
Use Spacer which comes with a flex property (defaults to 1)
Row(
children: <Widget>[
Text('Hello'),
Spacer(), // <-- Use this
Text('World'),
],
)
new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget> [
new Text('Babe', style: AppStyle.AppBarTextStyle,),
new Text('I miss you',style: AppStyle.AppBarTextStyle,)
]
)
)
We can use FractionallySizedBox widget to give weight sum for both width and height.
FractionallySizedBox(
heightFactor: .5,
widthFactor: 1.0,
alignment: Alignment.topCenter,
child: child,
)
For more info:
Link

Flutter Align widget rect does not detect the onTap gesture

I have a couple of image buttons laid out as follows :
The layout code is :
new Container(
height: 48.0,
child: new Row(
children: <Widget>[
new GestureDetector(
onTap: cancelTurn,
child: new Align(child: new Image.asset("assets/cancel_button.png"), widthFactor: 1.5),
),
new Expanded(child: new Container()),
new GestureDetector(
onTap: confirmTurn,
child: new Align(child: new Image.asset("assets/confirm_button.png"), widthFactor: 1.5),
),
],
),
)
I am using Align widget to hopefully increase the clickable area of the GestureDetector. However, my clicks only work when I click inside the Image widget rect. If you notice, the bounds/rects for Align and Image are different. Align is extended beyond the Image child by using the widthFactor property (set to 1.5)
Any ideas on why the Align widget might not be detecting the tap gesture in its bounds.
Try passing a hitTestBehavior of HitTestBehavior.opaque to the GestureDetector.
Other tips:
Instead of Align, consider a Padding.
Consider using an IconButton with Icons.close and Icons.check instead of embedding a custom png. You could wrap it in a Material to draw the black circle and it will even clip the ink splash for you automatically.
To get the desired effect, you could replace the GestureDetector elements for InkWell. This will give a feedback to the user (due to the Ink animation) and will be activated when the whole area is pressed.
To do that, your container should look like this:
new Container(
height: 48.0,
child: new Row(
children: <Widget>[
new InkWell(
onTap: cancelTurn,
child: new Align(child: new Image.asset("assets/cancel_button.png"), widthFactor: 1.5),
),
new Expanded(child: new Container()),
new InkWell(
onTap: confirmTurn,
child: new Align(child: new Image.asset("assets/confirm_button.png"), widthFactor: 1.5),
),
],
),
)

Resources