how to make border of container like this in flutter? - flutter

I want to make a container border like this but don't know what should I use?

TRy following this code . You have to customize it more to suit your exact needs. Here we are using BoxDecoration properties to set every corner radius. Check at https://dartpad.dev/?id=61468d155191404e24d99404ebb297ea. This one matches the desgn consideration to a certain level but not exact. This one is simple , other wise to make exact the same as you shown you can use ClipPath.
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(right: 16,left: 16,top: 16,bottom: 64),
height: MediaQuery.of(context).size.height*0.80,
width: MediaQuery.of(context).size.width-32,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(0),
bottomLeft: Radius.circular(MediaQuery.of(context).size.width*.4),
bottomRight: Radius.circular(32.0),
topRight: Radius.circular(0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey.withOpacity(1),
offset: Offset(2, 2),
blurRadius: 10.0),
],
),
);
}
}

Related

Flutter container gets disappear

Don't know what I am missing here. Want to make the Container UI like the Material Filled Text Field. Just want to know whether we can use BorderSide and borderRadius together or they work separately. And using Container only How can I achieve this?
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Container(
height: 50.0,
width: 500.0,
padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 2.0),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(width: 2.0, color: Color(0xFFFF7F7F7F)),
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(2),
topRight: Radius.circular(2),
),
color: Color(0xFFBFBFBF),
),
child: const Text('OK',
textAlign: TextAlign.center,
style: TextStyle(color: Color(0xFF000000))),
),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Text('Hello, World!', style: Theme.of(context).textTheme.headline4);
}
}
I think that flutter can't handle this configuration.
You are appling bottom border in the container, and also a border radius.
Read this post: A borderRadius can only be given for uniform borders
Also read this: Add border to a Container with borderRadius in Flutter
In the second post, you may find a way to achieve what you are looking for.

How to define Custom BorderRadius class in flutter?

to standardize the design and avoid mistakes and reduce build time,
I'm trying to define a BorderRadius class that takes a double corner value as an argument so It can be used in many places throughout the app
i'm getting lots of errors with defining the class constructors and can't really get the solution here
import 'package:flutter/material.dart';
class Borderz extends BorderRadius{
final BorderRadius enBorderz;
final double corner;
static const double zeroCorner = 0.0,
const Borderz({
#required this.corner,
this.enBorderz = BorderRadius.only(
topLeft: Radius.circular(corner),
topRight: Radius.circular(corner),
bottomLeft: Radius.circular(corner),
bottomRight: Radius.circular(zeroCorner),
)
}) : super(zeroCorner : zeroCorner)
}
some serious fundamental mistake here in this code that makes me feel embarrassed, appreciate your help
You can copy paste run full code below
You can directly call super.only and no need to use enBorderz
class Borderz extends BorderRadius {
final double corner;
static const double zeroCorner = 0.0;
Borderz({
#required this.corner,
}) : super.only(
topLeft: Radius.circular(corner),
topRight: Radius.circular(corner),
bottomLeft: Radius.circular(corner),
bottomRight: Radius.circular(zeroCorner),
);
}
...
Container(
width: 100,
height: 100,
child: Center(
child: Text(
'test',
),
),
decoration: BoxDecoration(
borderRadius: Borderz(corner: 10.0),
color: Colors.blue,
),
),
working demo
full code
import 'package:flutter/material.dart';
class Borderz extends BorderRadius {
final double corner;
static const double zeroCorner = 0.0;
Borderz({
#required this.corner,
}) : super.only(
topLeft: Radius.circular(corner),
topRight: Radius.circular(corner),
bottomLeft: Radius.circular(corner),
bottomRight: Radius.circular(zeroCorner),
);
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 100,
height: 100,
child: Center(
child: Text(
'test',
),
),
decoration: BoxDecoration(
borderRadius: Borderz(corner: 10.0),
color: Colors.blue,
),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

How to create square avatar with rounded corners on flutter?

I want to create a widget similar to CircleAvatar, but not rounded.
This is CircleAvatar:
And this is the avatar I want to create:
I want to know if there is a default widget to do this, as CircleAvatar for rounded avatars.
There are so many ways to achieve it but I will only make use one.
Wrap a ClipRRect() widget around a child widget(this could be an image or any other relevant widget like a Container used in my example). Then, pass BorderRadius.circular(20.0) value to borderRadiusproperty of ClipRRect(). That is the active lines of code that create the effect.
Check below for my example:
ClipRRect(
borderRadius: BorderRadius.circular(20.0),//or 15.0
child: Container(
height: 70.0,
width: 70.0,
color: Color(0xffFF0E58),
child: Icon(Icons.volume_up, color: Colors.white, size: 50.0),
),
),
see result here
You can use ClipRRect with specified BorderRadius property like this:
see image here
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(10.0)),//add border radius here
child: Image.asset('assets/01.jpg'),//add image location here
),
You can use the combination of ClipRRect and Container Widget to achieve the same, use the above code given.
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(15.0),//or 15.0
child: Container(
height: 70.0,
width: 70.0,
color: const Color(0xffFF0E58),
child: const Icon(Icons.volume_up, color: Colors.white, size: 50.0),
),
);
}
}

Flutter - AnimatedBuilder , animation/widget bugs out when loading in for first time

when I load the builder for the first time , all of my cards are the same size for a second before jumping to the correct sizes and proportions ,does anyone know how I can adjust this so that they are in the right sizes from the get go?
Strangely enough when I replace my Padding & Card widget with just a container full of the images, it seems to spawn in the correct sizes , however I need them to be cards for my later layout.
(I also plan on throwing it all into a widget class instead of putting all this code in my main and rather just returning a CustomScroller.)
Please check the following Gifs:
Gif Of Animation Glitch
Gif of Animation working fine when just container is used
Any help fixing the initial loading would be wildly appreciated! :)
import 'package:flutter/material.dart';
import 'package:blink/widget/customScroller.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
PageController pageController;
List<String> images = [
"https://iso.500px.com/wp-content/uploads/2014/07/big-one.jpg",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRXRfe-GzBFRQzv8udHMCshqQGAj2JD5SGsR7CoyWP_HqFapJCYSA&s",
"https://ichef.bbci.co.uk/wwfeatures/live/976_549/images/live/p0/7w/b9/p07wb9xk.jpg",
"https://images.unsplash.com/photo-1501785888041-af3ef285b470?ixlib=rb-1.2.1&w=1000&q=80"
];
#override
void initState() {
// TODO: implement initState
pageController = PageController(initialPage: 1, viewportFraction: 0.77);
}
#override
Widget build(BuildContext context) {
return new Scaffold(
body: PageView.builder(
controller: pageController,
itemCount: images.length,
itemBuilder: (context,position){
return customScroller(position);
}),);
}
customScroller(int index) {
return AnimatedBuilder(
animation: pageController,
builder: (context, widget) {
double val = 1;
if(pageController.position.haveDimensions){
val = pageController.page - index;
val = 1 - (val.abs()*0.3).clamp(0.0,1.0);}
return Center(
child: SizedBox(
height: Curves.easeInOut.transform(val) *300,
width: Curves.easeInOut.transform(val) *400,
child: widget,
),
);
},
// child: Container(
// margin: EdgeInsets.all(10),
// child: Image.network(images[index],fit:BoxFit.cover),
// ),
// When I use the above code as the child instead of the padding with the card in it seems to spawn correctly
child: Padding(
padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
child: Container(
child: Card(
color: Colors.white70,
elevation: 9,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
clipBehavior: Clip.antiAlias,
child: Container(
color: Colors.white,
padding: EdgeInsets.all(5),
child: Row(
children: <Widget>[
Expanded(
flex: 2,
child: Container(
height: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(0.0),
bottomRight: Radius.circular(0.0),
bottomLeft: Radius.circular(20.0)),
child: new Image.network(
images[index],
fit: BoxFit.cover,
),
),
),
),
],
),
),
),
),
)
);
}
}
What you are missing is that the AnimatedBuilder build method runs only if there is some animation. Not at the very beginning. So this is why the size changes if you are scrolling and not before.
Your child widget is not wrapped with a transformer widget (or sized box) so it stays the same for all indexes (at the beginnen - the builder has not run yet). In my example I wrapped the child also with a SizedBox and i gave val some initial value.
Working example:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
PageController pageController;
List<String> images = [
"https://iso.500px.com/wp-content/uploads/2014/07/big-one.jpg",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRXRfe-GzBFRQzv8udHMCshqQGAj2JD5SGsR7CoyWP_HqFapJCYSA&s",
"https://ichef.bbci.co.uk/wwfeatures/live/976_549/images/live/p0/7w/b9/p07wb9xk.jpg",
"https://images.unsplash.com/photo-1501785888041-af3ef285b470?ixlib=rb-1.2.1&w=1000&q=80"
];
#override
void initState() {
// TODO: implement initState
pageController = PageController(initialPage: 1, viewportFraction: 0.77);
}
#override
Widget build(BuildContext context) {
return new Scaffold(
body: PageView.builder(
controller: pageController,
itemCount: images.length,
itemBuilder: (context, position) {
return customScroller(position);
}),
);
}
customScroller(int index) {
Widget child = Padding(
padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
child: Container(
child: Card(
color: Colors.white70,
elevation: 9,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
clipBehavior: Clip.antiAlias,
child: Container(
color: Colors.white,
padding: EdgeInsets.all(5),
child: Row(
children: <Widget>[
Expanded(
flex: 2,
child: Container(
height: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(0.0),
bottomRight: Radius.circular(0.0),
bottomLeft: Radius.circular(20.0)),
child: new Image.network(
images[index],
fit: BoxFit.cover,
),
),
),
),
],
),
),
),
),
);
double val = (index == 1)?1:0.7;
return AnimatedBuilder(
animation: pageController,
builder: (context, widget) {
if (pageController.position.haveDimensions) {
val = pageController.page - index;
val = 1 - (val.abs() * 0.3).clamp(0.0, 1.0);
}
print("val: $val; index: $index");
return _getTransformedSizedBox(val, widget);
},
// child: Container(
// margin: EdgeInsets.all(10),
// child: Image.network(images[index],fit:BoxFit.cover),
// ),
// When I use the above code as the child instead of the padding with the card in it seems to spawn correctly
child: _getTransformedSizedBox(val, child));
}
_getTransformedSizedBox(double val, Widget widget) {
return Center(
child: SizedBox(
height: Curves.easeInOut.transform(val) * 300,
width: Curves.easeInOut.transform(val) * 400,
child: widget,
),
);
}
}

drawing partial round-rect borders

i want to be able to draw partial round-rect borders around a child widget: only the left, top, and right sides; only the left, bottom and right sides; and so on. this function comes tantalizingly close to doing what i want:
Widget roundRectBorderTop(Widget child, Color color, double cornerRadius,
[double borderWidth = 1.0])
{
final side = BorderSide(color:color, width:borderWidth);
final bord = Border(left:side, top:side, right:side);
final radi = BorderRadius.circular(cornerRadius);
final data = BoxDecoration(border:bord, borderRadius:radi);
return DecoratedBox(child:child, decoration:data);
}
alas, when i run this, it dies with the following assertion:
flutter: The following assertion was thrown during paint():
flutter: A borderRadius can only be given for uniform borders.
flutter: 'package:flutter/src/painting/box_border.dart':
flutter: Failed assertion: line 510 pos 12: 'borderRadius == null'
if i remove the borderRadius: parameter from the BoxDecoration object, then the code "works," but it draws right-angle corners, rather than round-rect corners, which is what i want.
does anybody know how to fix this?
You could simplify by having BorderRadius.only for decoration of a Container;
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
)
),
child: ...
)
Full working code for the screenshot below;
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Rounded Corners Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
),
),
padding: EdgeInsets.all(20.0),
child: Text('Rounded Corners'),
),
],
),
),
),
);
}
}