How can I float text around an image in Flutter? - dart

How could I achieve this layout in Flutter?

I published package drop_cap_text to achive DropCapText, you can also put an image as custom widget inside DropCap.

its Pretty basic just simple Row with the data inside it
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Image.asset('Your image link here'),
Text('you paragraph here')
],)

You can use Stack(): https://flutter.io/docs/development/ui/layout#stack
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
var stack = Stack(
alignment: const Alignment(0.6, 0.6),
children: [
CircleAvatar(
backgroundImage: AssetImage('images/pic.jpg'),
radius: 100.0,
),
Container(
decoration: BoxDecoration(
color: Colors.black45,
),
child: Text(
'Mia B',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
],
);
// ...
}
}
You can use a Container() widget with foregroundDecoration:
Container(
child: Text('Hello World'),
foregroundDecoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage('https://www.example.com/images/frame.png'),
centerSlice: Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
),
),

Related

Decide which type to construct during runtime

I am trying to get more into Flutter.
I have a button class that either builds a FlatButton or an OutlineButton depending on a parameter
import 'package:flutter/material.dart';
class Button extends StatelessWidget {
final String text;
final VoidCallback onPressed;
final Color backgroundColor;
final Color textColor;
final bool isOutline;
Button(
{#required this.text,
#required this.onPressed,
this.backgroundColor = Colors.deepOrange,
this.textColor = Colors.white,
this.isOutline = false});
#override
Widget build(BuildContext context) {
return this.isOutline
? _buildOutlineButton(context)
: _buildFlatButton(context);
}
FlatButton _buildFlatButton(BuildContext context) {
return FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
color: this.backgroundColor,
onPressed: this.onPressed,
child: Container(
padding: const EdgeInsets.symmetric(
vertical: 20.0,
horizontal: 20.0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Text(
this.text,
textAlign: TextAlign.center,
style:
TextStyle(color: this.textColor, fontWeight: FontWeight.bold),
),
),
],
),
),
);
}
OutlineButton _buildOutlineButton(BuildContext context) {
return OutlineButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
color: this.backgroundColor,
onPressed: this.onPressed,
child: Container(
padding: const EdgeInsets.symmetric(
vertical: 20.0,
horizontal: 20.0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Text(
this.text,
textAlign: TextAlign.center,
style:
TextStyle(color: this.textColor, fontWeight: FontWeight.bold),
),
),
],
),
),
);
}
}
As you can see, both _build*Button functions look similar. Is there a way to simply the code? Something like that (pseudo-code ahead):
final type = this.isOutline ? OutlineButton : FlatButton;
return type(shape: ..., color: ..., ...);
You can share much of the common code like this:
MaterialButton _buildButton(bool flat, BuildContext context) {
Container container = Container(
padding: const EdgeInsets.symmetric(
vertical: 20.0,
horizontal: 20.0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Text(
text,
textAlign: TextAlign.center,
style: TextStyle(color: textColor, fontWeight: FontWeight.bold),
),
),
],
),
);
RoundedRectangleBorder border = RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
);
return flat
? FlatButton(
shape: border,
color: backgroundColor,
onPressed: onPressed,
child: container,
)
: RaisedButton(
shape: border,
color: backgroundColor,
onPressed: onPressed,
child: container,
);
}

How to horizontally center a Text in flutter?

I’m trying to horizontally center a text. Please check the below code.
import 'package:flutter/material.dart';
class LoginPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
// TODO: implement build
return Container(
color: Colors.black,
child: Row(
children: <Widget>[
Column(
children: <Widget>[_buildTitle()],
),
],
));
}
Widget _buildTitle() {
return
Center(child: Container(
margin: EdgeInsets.only(top: 100),
child: Column(
children: <Widget>[
Text(
"something.xyz",
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 25,),
textAlign: TextAlign.center,
),
],
),
),);
}
}
This did not center horizontally, instead gave the following output. The margins etc is fine.
How can I fix this?
try this
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: LoginPage()));
class LoginPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: Container(
color: Colors.black,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[_buildTitle()],
),
],
)),
);
}
Widget _buildTitle() {
return Center(
child: Container(
margin: EdgeInsets.only(top: 100),
child: Column(
children: <Widget>[
Text(
"something.xyz",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 25,
),
// textAlign: TextAlign.center,
),
],
),
),
);
}
}
You can also solve it with a Container and TextAlign:
Container(
width: double.infinity,
child: Text(
'something.xyz',
textAlign: TextAlign.center,
),
)
In this case, the container takes up the entire width with double.infinity. The text adapts to the container and can be moved to the middle of the screen with TextAlign.center.
more simple way:
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Sunday', style: TextStyle(fontSize: 20),),
],
),
), //Center
set
child: Center(
child: Text(
"Hello World",
textAlign: TextAlign.center,
),
),
in program
I added my own widget for this use case which is much shorter than the row solution:
import 'package:flutter/material.dart';
class CenterHorizontal extends StatelessWidget {
CenterHorizontal(this.child);
final Widget child;
#override
Widget build(BuildContext context) =>
Row(mainAxisAlignment: MainAxisAlignment.center,children: [child]);
}
the result is this:
CenterHorizontal(Text('this is horizontal centered'))
Widget textSection = Container(
child: Text(
'This can be several lines centered in the child of a container Widget.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15.0,
color: Colors.white,
),
),
);
If I understand, you just want to center horizontally the title, not the other elements that may come after I suppose.
Take a look at the code below:
import 'package:flutter/material.dart';
class LoginPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blueAccent,
title: Text("DEMO"),
),
body: Container(
color: Colors.black,
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[_buildTitle()],
),
Row(
children: <Widget>[
// add other elements that you don't to center horizontally
Text(
"other elements here",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 25,
),
),
],
),
],
)));
}
Widget _buildTitle() {
return Container(
margin: EdgeInsets.only(top: 100),
child: Text(
"something.xyz",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 25,
),
),
);
}
}
The result that gives: here
Container(
width: double.infinity,
child: const Text(
"Hello World!",
textAlign: TextAlign.center,
),
);
Flutter now recommends SizedBox for white space
SizedBox(
width: double.infinity,
child: Text('Hello World',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.White,
fontSize: 16.0,
fontWeight: FontWeight.normal))),

Flutter - How to control profile image size in the drawer

I would like to control the profile image size, and get it rounded instead of oval as shown below.
Changing the height and/or the width values doesn't affect neither the size nor the ratio, also the weird thing is when I change the margin parameter it changes the oval shape radius.
new UserAccountsDrawerHeader(
decoration: BoxDecoration(color: Colors.white),
currentAccountPicture: new Container(
margin: const EdgeInsets.only(bottom: 40.0),
width: 10.0,
height: 10.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: new NetworkImage(
"https://example.com/assets/images/john-doe.jpg",
),
),
),
),
accountName: new Container(
...
),
accountEmail: new Container(
...
),
onDetailsPressed: () {
...
},
),
What am I doing wrong ?
Update: The above way of doing is a workaround to the CircleAvatar that didn't give any control on the image size. If you tried CircleAvatar in some different way that gives control on image size, please share it to help.
Use This code for network image:
new CircleAvatar(
radius: 60.0,
backgroundColor: const Color(0xFF778899),
backgroundImage: NetworkImage("Your Photo Url"), // for Network image
),
Use this for asset Image:
new CircleAvatar(
radius: 60.0,
backgroundColor: const Color(0xFF778899),
child: new Image.asset(
'images/profile.png',
), //For Image Asset
),
If you use backgroundImage as the image provider for CircleAvatar then changing the radius property indeed has no effect. From the source circle_avatar.dart it can be observed the image is being rendered as BoxFit.cover DecorationImage(image: backgroundImage, fit: BoxFit.cover) - and in user_accounts_drawer_header.dart the currentAccountPicture is hardcoded to be a 72.0 pixel SizedBox so the image will always be 72.0px in dimensions.
https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/user_accounts_drawer_header.dart#L57
https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/circle_avatar.dart#L203
Hopefully the Flutter team adds some level of control to this in the future.
Not an answer, just more info that hopefully helps someone.
Try This:
import 'package:flutter/material.dart';
class AppDrawer extends StatefulWidget {
#override
_AppDrawerState createState() => new _AppDrawerState();
}
class _AppDrawerState extends State<AppDrawer> {
#override
Widget build(BuildContext context) {
return new Drawer(
child: Center(
child: Column(
children: <Widget>[
new UserAccountsDrawerHeader(
decoration: BoxDecoration(color: Colors.white),
currentAccountPicture: new CircleAvatar(
radius: 50.0,
backgroundColor: const Color(0xFF778899),
backgroundImage:
NetworkImage("http://tineye.com/images/widgets/mona.jpg"),
),
),
],
),
),
);
}
}
This is the screenshot of output:
Wrap your image in a CircleAvatar widget. It’s made for such purposes.
You put the margin inside the Container of the image while you have to use the margin parameter of the UserAccountDrawerHeader, this is why your image became an oval:
UserAccountsDrawerHeader(
decoration: BoxDecoration(color: Colors.white),
margin: EdgeInsets.only(bottom: 40.0),
currentAccountPicture: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image:
NetworkImage("https://via.placeholder.com/150"))),
),
accountName: new Container(
child: Text(
'Name',
style: TextStyle(color: Colors.black),
)),
accountEmail: new Container(
child: Text(
'Email',
style: TextStyle(color: Colors.black),
)),
),
You can create your own header:
DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue
),
child: ListView(
children: [
return ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.asset('images/myImage.jpg'),
),
//These can go here or below the header with the same background color
Text("user name"),//customize this text
Text("useremail#example.com"),
//...additional header items here
],
)),
I found a solution!!! at least thats what worked for me
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
// Side menu
drawer: new Drawer(
child: GestureDetector(
onTap: () {},
child: ListView(
children: <Widget>[
new UserAccountsDrawerHeader(
accountName: new Text('Hymn +'),
accountEmail: new Text('johndoe#gmail.com'),
currentAccountPicture: new CircleAvatar(
backgroundImage: new NetworkImage(
'https://miro.medium.com/max/1400/1*uC0kYhn8zRx8Cfd0v0cYQg.jpeg'),
),
),
],
),
),
),
);
}
}
This works for me:
DrawerHeader(
decoration: BoxDecoration(color: Colors.blue),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
/*CircleAvatar(
radius: 50.0,
backgroundColor: const Color(0xFF778899),
child: new Image.asset('assets/images/profile_image.png'), //For Image Asset
),*/
Container(
height: 110,
child: ClipRRect(
borderRadius: BorderRadius.circular(100.0),
child: Image.asset('assets/images/profile_image.png'),
),
),
//These can go here or below the header with the same background color
//Divider(height: 1.0, thickness: 1.0, color: Colors.black),
SizedBox(height: 9.0),
Text("user name"),
//...additional header items here
],
),
),

Inkwell not showing ripple when used with Container decoration

I want to add a ripple on an item, it is working fine until I add a gradient on the item using BoxDecoration.
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Material(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
elevation: 6.0,
shadowColor: Colors.grey[50],
child: InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: AlignmentDirectional.bottomStart,
end: AlignmentDirectional.topEnd,
colors: [
Colors.yellow[800],
Colors.yellow[700],
],
),
),
padding: EdgeInsets.all(16.0),
child: Text(
widget.title,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
),
);
}
Update in 2019:
You should use Ink widget inside Material, instead of Container.
It takes decoration parameter as well:
Material(
child: Ink(
decoration: BoxDecoration(
// ...
),
child: InkWell(
onTap: () {},
child: child, // other widget
),
),
);
I found the solution:
I need one Material for Inkwell, and one Material for elevation and rounded borders.
The inner Material has a type of MaterialType.transparency so that it doesn't draw anything over the box decoration of its parent and still preserve the ink effect. The shadow and borders are controlled by outer Material.
Container(
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Material( // <----------------------------- Outer Material
shadowColor: Colors.grey[50],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
elevation: 6.0,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: AlignmentDirectional.bottomStart,
end: AlignmentDirectional.topEnd,
colors: [
AppColors.pinkDark,
AppColors.pink,
],
),
),
child: Material( // <------------------------- Inner Material
type: MaterialType.transparency,
elevation: 6.0,
color: Colors.transparent,
shadowColor: Colors.grey[50],
child: InkWell( //<------------------------- InkWell
splashColor: Colors.white30,
onTap: () {},
child: Container(
padding: EdgeInsets.all(16.0),
child: Row(
children: <Widget>[
Icon(
Icons.work,
size: 40.0,
color: Colors.white,
),
SizedBox(
width: 20.0,
),
Column(
children: <Widget>[
Text(
widget.title,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
],
)
],
),
),
),
),
),
),
);
Simple splash effect widget I created that works perfect.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class SplashEffect extends StatelessWidget {
final Widget child;
final Function onTap;
const SplashEffect({Key key, this.child, this.onTap}) : super(key: key);
#override
Widget build(BuildContext context) {
return Material(
type: MaterialType.transparency,
child: InkWell(
borderRadius: BorderRadius.all(Radius.circular(16)),
child: child,
onTap: onTap,
),
);
}
}
Splash color is overlap by container BoxDecoration
Try this
Widget build(BuildContext context) {
return new Container(
decoration: BoxDecoration(
borderRadius: new BorderRadius.all(new Radius.circular(4.0)),
gradient: LinearGradient(
begin: AlignmentDirectional.bottomStart,
end: AlignmentDirectional.topEnd,
tileMode: TileMode.repeated,
colors: [
Colors.yellow[800],
Colors.yellow[700],
],
),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Colors.grey[50],
//blurRadius: 0.3,
blurRadius: 6.0,
offset: new Offset(0.0, 4.0)
)
]
),
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Material(
color: Colors.transparent,
//shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
//elevation: 6.0,
//shadowColor: Colors.grey[50],
child: InkWell(
splashColor: const Color(0x8034b0fc),
onTap: () {},
child: Container(
//decoration: ,
padding: EdgeInsets.all(16.0),
child: Text(
'Click',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
),
);
}
If anyone came here looking to do use an inkwell with a circle decoration (like I did), I used the accepted answer to come up with this.
Material(
child: Ink(
width: 150,
height: 150,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey[350],
border: Border.all(
color: Colors.red,
width: 4.0,
),
),
child: InkWell(
customBorder: const CircleBorder(),
onTap: onTap,
child: const Icon(Icons.add, size: 48, color: Colors.white),
),
));

how to spacely give the image or text background in complex screen

Hi friends I need to develop the screen like this one
But I am getting below view I tried the expanded but varied differently please help friends I am new to flutter I am not getting any idea how to achieve the required layout
Below is my code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'ColorsPage.dart';
void main() => runApp(new Login());
class Login extends StatefulWidget{
#override
State<StatefulWidget> createState() {
Login_State login_state() => Login_State();
return login_state();
}
}
class Login_State extends State<Login>{
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
body: new Builder(builder: (BuildContext context){
return new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Container(
child: new Image.asset('assets/rural_post_logo.png'),
decoration: new BoxDecoration(
color: Colors.white
),
alignment: Alignment(0.0, -1.0),
),
new Container(
child: new Text('SIGN IN',style: new TextStyle(
color: Colors.white
),),
decoration: new BoxDecoration(
color: secondarycolor
),
alignment: Alignment(0.0, -1.0),
),
new Container(
child: new Text('SIGN UP',style: new TextStyle(
color: Colors.white
),),
decoration: new BoxDecoration(
color: primarycolor
),
alignment: Alignment(0.0, -1.0),
)
],
);
}),
),
);
}
}
The Column widget over the Container overwrites its normal behavior to be as big as possible and now it just wraps it's content as you noticed. You could give the Container a height property or wrap it in a Padding:
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(70.0),
child:
new Container(
child: new Image.asset('assets/rural_post_logo.png'),
decoration: new BoxDecoration(
color: Colors.white
),
alignment: Alignment(0.0, -1.0),
),
),
new Column(
children: <Widget>[
new Container(
child: Padding(
padding: const EdgeInsets.all(80.0),
child: new Text(
'SIGN IN',
style: new TextStyle(color: Colors.white),
),
),
decoration: new BoxDecoration(color: secondarycolor),
alignment: Alignment(0.0, -1.0),
),
new Container(
child: Padding(
padding: const EdgeInsets.all(80.0),
child: new Text(
'SIGN UP',
style: new TextStyle(color: Colors.white),
),
),
decoration: new BoxDecoration(color: primarycolor),
alignment: Alignment(0.0, -1.0),
)
],
),
],
),

Resources