How to add ripple effect to PhysicalModel in flutter - dart

I was trying to create a login button, which will be animated on pressing on it. But while clicking on the button( on the PhysicalModel), a ripple effect is shown only on the Login text, not entirely on the physical model. How to add ripples to PhysicalModel or remove the ripple effect from MaterialButton ?
PhysicalModel(
color: Colors.teal,
borderRadius: BorderRadius.circular(50.0),
child: MaterialButton(
key: _globalKey,
child: Text("Login"),
onPressed: () {
setState(() {
if (_state == 0) {
animateButton();
}
});
},
elevation: 4.0,
minWidth: _width,
height: 20.0,
),
)

If you want to remove the splash color of the MaterialButton just change these colors to transparent.
MaterialButton(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
If you want ripple effect for your PhysicalModel:
PhysicalModel(
color: Colors.teal,
borderRadius: BorderRadius.circular(50.0),
child: RawMaterialButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0)),
padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 30.0),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
child: Text("Login"),
onPressed: () {
setState(() {});
},
elevation: 4.0,
),
)

Here is my solution, you can simply set Your PhysicModel color to transparent, while using a Ink widget set color to what you need for your child widget:
PhysicalModel(
borderRadius: BorderRadius.circular(16),
shadowColor: Colors.grey.withAlpha(10),
elevation: 16,
color: Colors.transparent,
child: Ink(
color: Theme.of(context).scaffoldBackgroundColor,
child:
It's simply, and then you can have Inkwell effect as well as keep your color.

Adding Material Touch Ripples
Flutter provides the InkWell Widget to achieve this effect.
Definition:
A rectangular area of a Material that responds to touch.
Also: The InkWell widget must have a Material widget as an ancestor. The Material widget is where the ink reactions are actually painted. This matches the material design premise wherein the Material is what is actually reacting to touches by spreading ink.
Directions
Create a Widget we want to tap
Wrap it in an InkWell Widget to manage tap callbacks and ripple
animations
InkWell(
// When the user taps the button, show a snackbar
onTap: () {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Tap'),
));
},
child: PhysicalModel(
color: Colors.teal,
borderRadius: BorderRadius.circular(50.0),
child: MaterialButton /*or a FlatButton */(
key: _globalKey,
child: Text("Login"),
onPressed: () {
setState(() {
if (_state == 0) {
animateButton();
}
});
},
elevation: 4.0,
minWidth: _width,
height: 20.0,
),
)),

Related

Cannot set fill color for OutlineButton [duplicate]

This question already has answers here:
How to set the background color of a Flutter OutlineButton?
(5 answers)
Closed 1 year ago.
The documentation for the OutlineButton says that the color property fills the button color and is transparent by default. Specifically the Flutter documentation says of the color property: "color → Color
The button's fill color, displayed by its Material, while it is in its default (unpressed, enabled) state."
But setting the color property has no effect:
OutlineButton(
color: Colors.orange,
textColor: BmsColors.primaryForegroundColor,
borderSide: BorderSide(color: BmsColors.primaryForegroundColor, width: 2.0),
shape: new RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(8.0),
),
child: Text(
this.text,
style: TextStyle(fontFamily: 'Lalezar', fontWeight: FontWeight.w400),
),
onPressed: () {},
);
If you check the source code of OutlineButton there is a method to get the fillColor
Color _getFillColor() {
if (widget.highlightElevation == null || widget.highlightElevation == 0.0)
return Colors.transparent;
final Color color = widget.color ?? Theme.of(context).canvasColor;
final Tween<Color> colorTween = ColorTween(
begin: color.withAlpha(0x00),
end: color.withAlpha(0xFF),
);
return colorTween.evaluate(_fillAnimation);
}
But this method has an if condition, it uses the color only when the highlightElevation is different from 0, and also it uses the animation of the colorTween from color.withAlpha(0x00) to color.withAlpha(0xFF).
So it change from transparent to your color when you press it.
You can create your own OutlineButton widget if you want , this a sample I made:
class MyCustomOutlineButton extends StatelessWidget {
final String text;
final VoidCallback onPressed;
final Color color;
const MyCustomOutlineButton({Key key, this.text, this.onPressed, this.color})
: super(key: key);
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.yellow, width: 2.0),
color: color,
borderRadius: BorderRadius.circular(8.0),
),
margin: EdgeInsets.all(2.0),
child: RawMaterialButton(
fillColor: color,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 14.0),
child: Text(
text,
style: TextStyle(
fontFamily: 'Lalezar',
fontWeight: FontWeight.w400,
color: Colors.yellow),
),
),
onPressed: onPressed,
),
);
}
}
Usage
MyCustomOutlineButton(
text: "Become a Member",
color: Colors.orange,
onPressed: () {
print("here");
},
),
For what you want, you can use a simple RaisedButton combined with a RoundedRectangleBorder. See eg:
Container(
color: Colors.pink,
child: RaisedButton(
color: Colors.black,
child: Text('Become a member', style: TextStyle(color: Colors.yellow)),
onPressed: () {
print('Hello');
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
side: BorderSide(color: Colors.yellow, width: 5.0)),
),
)
The border color can be specified with the BorderSide, and the filled color is just the normal color property of RaisedButton.
Hope this help.

Flutter Remove CupertinoNavigationBar Backdrop

Is there a way to remove the blur of the CupertinoNavigationBar so it's truly transparent? I got rid of the border and color but there's a blur, for the android AppBar there isn't any blur but for the iOS AppBar it's just there. I check the code for it and there's an ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0) applied to it but how do I remove it?
Code:
CupertinoNavigationBar(
backgroundColor: Colors.transparent,
border: Border.all(
color: Colors.transparent,
width: 0.0,
style: BorderStyle.none
),
leading: IconButton(
icon: Icon(
Icons.close,
color: Colors.red,
),
onPressed: () {
Navigator.pop(context);
}),
automaticallyImplyLeading: false,
trailing: IconButton(
icon: Icon(
Icons.refresh,
color: Colors.blue,
),
onPressed: widget.onRestart
),
),
CupertinoNavigationBar has a border property. If you wish to remove the border, you can do something like this:
CupertinoNavigationBar(
border: Border(bottom: BorderSide(color: Colors.transparent)),
));
There is no API at the moment to do this. You'd need open a GitHub issue to request this feature, or alternatively fork the Flutter lib and remove that code yourself. It seems like reasonable request, and it's probably just a constructor flag, so could be a relatively quick fix for the Flutter team to implement.

flutter bottomsheet with notch (similar to bottomNavigationBar: BottomAppBar's shape property)

question 1:
how to add a notch to flutter bottom sheet around the floatingactionbutton, so that it looks like a bottomappbar (with shape of CircularNotchedRectangle()) but behave like bottomsheet (can drag to dismiss)
question 2:
how to make a bottomsheet non-dismissible yet still draggable. (similiar to android's bottomsheet)
question 3:
bottomsheet peek height, bottom sheet that dismissible, but also expandable (again, just like android bottomsheet)
is it possible to achieve what I've listed above? or i'm out of luck and have to use a 3rd party library? (if there's one?)
hope its help :)
Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.startDocked, //specify the location of the FAB
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.blue,
onPressed: () {
print('OK');
},
tooltip: "start FAB",
child: Container(
margin: EdgeInsets.all(15.0),
child: Icon(Icons.add),
),
elevation: 4.0,
),
bottomNavigationBar: BottomAppBar(
child: Container(
color: mycolor,
height: 35,
),
)
);
You can use this technique too
Scaffold(
backgroundColor: myBackgroundColor,
floatingActionButtonLocation: FloatingActionButtonLocation.startDocked, //specify the location of the FAB
floatingActionButton: CircleAvatar(
radius: 32,
backgroundColor: myBackgroundColor,
child: Padding(
padding: EdgeInsets.all(1),
child: FloatingActionButton(
backgroundColor: MyColor.textColor,
onPressed: () {
print('OK');
},
child: Container(
margin: EdgeInsets.all(15.0),
child: Icon(Icons.add),
),
elevation: 4.0,
),
),
),
bottomNavigationBar: BottomAppBar(
child: Container(
color: MyColor.textColor,
height: 35,
),
)
);

Flutter rounded profile image appBar

I'm currently new to Flutter and Dart language and I'm trying to set a profile image to my leading attribute of my appBar.
So far I've got my image to be rounded, but I can't make it smaller nor put a margin to the left side.
Here's a snippet of my code:
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
backgroundColor: Colors.blueGrey,
appBar: new AppBar(
title: new Text("Activities"),
leading: new Container(
padding: new EdgeInsets.all(15.0),
width: 10.0,
height: 10.0,
decoration: new BoxDecoration(
color: const Color(0xff7c94b6),
borderRadius: new BorderRadius.all(new Radius.circular(50.0)),
border: new Border.all(
color: Colors.white,
width: 1.0,
),
),
),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.refresh),
onPressed: () {
print("Reloading...");
setState(() {
_isLoading = true;
});
_FetchData();
},
)
],
),
// ...
And here's how it looks:
Click here
As you can see, my image is way too big and there's no margin to the left side...
How can I make the image smaller and most importantly, make a left margin similar to the refresh button right's margin?
Any help would be appreciated,
Have a good one.
Consider using Material combined with a shape: new CircleBorder() instead of manually creating a circle.
Or a CircleAvatar if that fits your needs.
Then add a Padding to control the size and margin.
return new Scaffold(
backgroundColor: Colors.blueGrey,
appBar: new AppBar(
title: new Text("Activities"),
leading: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Material(
shape: new CircleBorder(),
),
),
),
);

flutter corner radius with transparent background

Below is my code which I expect to render a round-corner container with a transparent background.
return new Container(
//padding: const EdgeInsets.all(32.0),
height: 800.0,
//color: const Color(0xffDC1C17),
//color: const Color(0xffFFAB91),
decoration: new BoxDecoration(
color: Colors.green, //new Color.fromRGBO(255, 0, 0, 0.0),
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(40.0),
topRight: const Radius.circular(40.0))
),
child: new Container(
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(40.0),
topRight: const Radius.circular(40.0))
),
child: new Center(
child: new Text("Hi modal sheet"),
)
),
However this is what it renders, it renders a white container (expected transparent) with a round corner radius. Any help?
If you wrap your Container with rounded corners inside of a parent with the background color set to Colors.transparent I think that does what you're looking for. If you're using a Scaffold the default background color is white. Change that to Colors.transparent if that achieves what you want.
new Container(
height: 300.0,
color: Colors.transparent,
child: new Container(
decoration: new BoxDecoration(
color: Colors.green,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(40.0),
topRight: const Radius.circular(40.0),
)
),
child: new Center(
child: new Text("Hi modal sheet"),
)
),
),
If you want to round corners with transparent background, the best approach is using ClipRRect.
return ClipRRect(
borderRadius: BorderRadius.circular(40.0),
child: Container(
height: 800.0,
width: double.infinity,
color: Colors.blue,
child: Center(
child: new Text("Hi modal sheet"),
),
),
);
As of May 1st 2019, use BottomSheetTheme.
MaterialApp(
theme: ThemeData(
// Draw all modals with a white background and top rounded corners
bottomSheetTheme: BottomSheetThemeData(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(10))
)
)
),
Introduced recently, using a theme to control the sheets style should be the best route.
If you want to theme different bottom sheets differently, include a new Theme object in the appropriate subtree to override the bottom sheet theme just for that area.
If for some reason you'd still like to override the theme manually when launching a bottom sheet, showBottomSheet and showModalBottomSheet now have a backgroundColor parameter. Use it like this:
showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
builder: (c) {return NavSheet();},
);
/// Create the bottom sheet UI
Widget bottomSheetBuilder(){
return Container(
color: Color(0xFF737373), // This line set the transparent background
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(16.0),
topRight: Radius.circular( 16.0)
)
),
child: Center( child: Text("Hi everyone!"),)
),
);
}
Call this to show the BotoomSheet with corners:
/// Show the bottomSheet when called
Future _onMenuPressed() async {
showModalBottomSheet(
context: context,
builder: (widgetBuilder) => bottomSheetBuilder()
);
}
It's an old question. But for those who come across this question...
The white background behind the rounded corners is not actually the container. That is the canvas color of the app.
TO FIX: Change the canvas color of your app to Colors.transparent
Example:
return new MaterialApp(
title: 'My App',
theme: new ThemeData(
primarySwatch: Colors.green,
canvasColor: Colors.transparent, //----Change to this------------
accentColor: Colors.blue,
),
home: new HomeScreen(),
);
Scaffold(
appBar: AppBar(
title: Text('BMI CALCULATOR'),
),
body: Container(
height: 200,
width: 170,
margin: EdgeInsets.all(15),
decoration: BoxDecoration(
color: Color(
0xFF1D1E33,
),
borderRadius: BorderRadius.circular(5),
),
),
);
Use transparent background color for the modalbottomsheet and give separate color for box decoration
showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context, builder: (context) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft:Radius.circular(40) ,
topRight: Radius.circular(40)
),
),
padding: EdgeInsets.symmetric(vertical: 20,horizontal: 60),
child: Settings_Form(),
);
});
showModalBottomSheet(
context: context,
builder: (context) => Container(
color: Color(0xff757575), //background color
child: new Container(
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(40.0),
topRight: const Radius.circular(40.0))
),
child: new Center(
child: new Text("Hi modal sheet"),
)
),
)
This is will make your container color the same as the background color. And there will be a child container of the same height-width with blue color.
This will make the corner with the same color as the background color.
Three related packages for covering this issue with many advanced options are:
sliding_up_panel
modal_bottom_sheet
sliding_panel
class MyApp2 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: AppBarTheme(
elevation: 0,
color: Colors.blueAccent,
)
),
title: 'Welcome to flutter ',
home: HomePage()
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int number = 0;
void _increment(){
setState(() {
number ++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueAccent,
appBar: AppBar(
title: Text('MyApp2'),
leading: Icon(Icons.menu),
// backgroundColor: Colors.blueAccent,
),
body: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(200.0),
topRight: Radius.circular(200)
),
color: Colors.white,
),
)
);
}
}
If both containers are siblings and the bottom container has rounded corners
and the top container is dynamic then you will have to use the stack widget
Stack(
children: [
/*your_widget_1*/,
/*your_widget_2*/,
],
);

Resources