How do I increase font size in Dart - dart

I'm new in Flutter+Dart and trying to increase font size but hard to understand documentation + implement to my work. Here is the file. How can I solve my problem?
import 'package:flutter/material.dart';
void main() => runApp(NewApp());
class NewApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('App Header'),
),
body: new Column(
children: [
new Container(
margin: new EdgeInsets.all(10.0),
child: new Card(
child: new Column(
children: <Widget>[
new Container(
padding: new EdgeInsets.all(10.0),
child: new Text('Hello Macaw'),
),
],
),
),
)
],
),
),
);
}
}

At the beginning this is hard to understand and implement. But once you understand, you will fall in love with the Flutter framework. Here is the solution:
new Text('Hello Macaw',style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),),
After that, format the code. That's it. Let me know if it works.

You can use style property of Text to change some of the property of the Text.
Example:
Text(
"Your text",
style: TextStyle(
fontSize: 20.0,
color: Colors.red,
fontWeight: FontWeight.w600,
),
)
It's a good practice to use predefined style for Text which gives you standard fontSize and fontWeight for the Text.
You can use them like this
style: Theme.of(context).textTheme.XYZ
XYZ can be body1, body2, title, subhead, headline etc.

Related

Why can't you put a children <Widget>[] in a body: Center widget?

I'm trying to create a listview widget with a floating action button on my Flutter app, but it's not working because Android Studio keeps on telling me that:
"the named parameter children isn't defined"
I basically can't put children in a body: Center widget, but I don't know why
I'm basically a beginner to Flutter and I'm still a bit confused about the basic syntax, and which widgets can hold which widgets, so any help is greatly appreciated! Thank you!
Here's my overall code that won't run due to the first error (in quotation marks above):
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: Home(),
));
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green[300],
title: Text(
'Welcome',
style: TextStyle(
fontSize: 25.0,
fontFamily: 'Raleway',
letterSpacing: 1.0,
),
),
centerTitle: true,
),
body: Center(
children: <Widget> [
ListView(
children: <Widget>[
Container(
height: 50,
color: Colors.green[100],
child: Text(
'Body Text',
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 45.0,
letterSpacing: 1.0,
color: Colors.green[300],
),
),
),
Container(
height: 50,
color: Colors.green[100],
child: Text(
'Text'
),
),
],
),
FloatingActionButton(
onPressed: () {},
child: Text(
'+',
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 35.0,
),
),
backgroundColor: Colors.green[300],
),
]
),
);
}
}
child parameter of Center Widget has a data type of Widget and so it can't take <Widget>[] as an input. It is similar to that an int won't accept String value. They are two different data types.
It seems that you want to have a list of data that is in center of the screen: For that you can use following code.
1.
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // (optional) will center horizontally.
children: <Widget>[
.....
]
)
2.
Center(
child: ListView(
shrinkWrap:true;
children: <Widget>[
.....
]
)
)
It's simple. Because Center is a widget that does not take more than on widget as input.
It can only align one widget provided as a child.
For mulitple children you have to use some widget that takes a list of widgets as input.
Like:
Column
Row
ListView
Wrap
etc.
Center accepts only one widget check laytout page at https://flutter.dev/docs/development/ui/layout
Column(mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget> [
ListView(
children: <Widget>[
Container(
height: 50,
color: Colors.green[100],
child: Text(
'Body Text',
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 45.0,
letterSpacing: 1.0,
color: Colors.green[300],
),
),
),
Container(
height: 50,
color: Colors.green[100],
child: Text(
'Text'
),
),
],
),
FloatingActionButton(
onPressed: () {},
child: Text(
'+',
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 35.0,
),
),
backgroundColor: Colors.green[300],
),
]
),
);
Center is a widget that centres its child within itself.
So you can have only 1 child inside Center Widget.
You can take structure like,
Center(
child: Column(
children: <Widget>[
All Children you have
]
)
There are two types of widgets, one of which accepts a widget as a child and the other one accepts [Widget] as children.
Accepts a Widget as Child: Container widget, Center widget, Padding widget, , etc.
Accepts [Widget] as Children: Row widget, Column widget, Stack widget, Wrap widget, ListView widget, etc.
Center class :
A widget that centers its child within itself.
All layout widgets have either of the following:
A child property if they take a single child—
for example, Center or Container
A children property if they take a list of widgets—
for example, Row, Column, ListView, or Stack.
Add the Text widget to the Center widget:
const Center(
child: Text('Hello World'),
),

How expand text and container according text size?

I'm trying to create a card with a text within a container but I would like to show only a part of the text and when the user click on "show more", show the rest. I saw a Widget to construct text like this here, but I need expand the card container either and I don't know how to do that because I need to know how many lines the text have to expand with the correctly size. Exists a way to calculate the size according the number of lines or characters?
I tried to create the card as follows, where the DescriptionText is the Widget on the link and specify a minHeight in the Container in the hope of expanding the container along with the text but did not work.
Widget _showAnswerCard(Answer answer, User user) {
return Card(
elevation: 3.0,
color: Theme.of(context).backgroundColor,
child: Container(
constraints: BoxConstraints(minHeight: 90),
padding: EdgeInsets.all(10.0),
child: Flex(
direction: Axis.horizontal,
children: <Widget>[
Expanded(flex: 1, child: _showUserAvatar(answer)),
Expanded(flex: 3, child: _showAnswerDetails(answer, user)),
],
),
));
}
Widget _showAnswerDetails(Answer answer, User user) {
return Flex(
direction: Axis.vertical,
children: <Widget>[
Expanded(
flex: 3,
child: DescriptionTextWidget(text: answer.content),
),
Expanded(
flex: 1,
child: _showAnswerOptions(),
)
],
);
}
I'll really appreciate if someone could help me with that.
Just use Wrap widget to wrap your Card widget.
Based on your link for suggested answer. I did change to use Wrap widget.
Jus do copy/paste below code and check.
import 'package:flutter/material.dart';
class ProductDetailPage extends StatelessWidget {
final String description =
"Flutter is Google’s mobile UI framework for crafting high-quality native interfaces on iOS and Android in record time. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source.";
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text("Demo App"),
),
body: new Container(
child: new DescriptionTextWidget(text: description),
),
);
}
}
class DescriptionTextWidget extends StatefulWidget {
final String text;
DescriptionTextWidget({#required this.text});
#override
_DescriptionTextWidgetState createState() =>
new _DescriptionTextWidgetState();
}
class _DescriptionTextWidgetState extends State<DescriptionTextWidget> {
bool flag = true;
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Wrap(
children: <Widget>[
Card(
margin: EdgeInsets.all(8),
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
child: Column(
children: <Widget>[
Container(
child: Text(
widget.text,
overflow: flag ? TextOverflow.ellipsis : null,
style: TextStyle(
fontSize: 15,
),
),
),
InkWell(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(
flag ? "show more" : "show less",
style: new TextStyle(color: Colors.blue),
),
],
),
onTap: () {
setState(() {
flag = !flag;
});
},
),
],
)),
),
],
);
}
}
Result:
The solution I can think of is to use two labels, one for displaying only one line of text and one for displaying all the text. When the button is clicked, the two labels are alternately displayed in an animated manner. There is no computer at the moment, it is not convenient to verify, I hope to give you some help in the implementation of the program.

How to place an image below a listview in flutter?

Using this simple design, how can I display the second image under the listview? In reality the list will be fetched from firebase where each item is an ExpansionTile, so the height of the listview can in no way be fixed.
The column should be scrollable so you can see the full image if you scroll down below the list.
import 'package:flutter/material.dart';
List<Widget> list = <Widget>[
ListTile(
title: Text('CineArts at the Empire',
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
subtitle: Text('85 W Portal Ave'),
leading: Icon(
Icons.theaters,
color: Colors.blue[500],
),
),
ListTile(
title: Text('The Castro Theater',
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
subtitle: Text('429 Castro St'),
leading: Icon(
Icons.theaters,
color: Colors.blue[500],
),
),
];
class CartWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Column(children: <Widget>[
Image.network("https://via.placeholder.com/350x150"),
Expanded(
child: ListView(
children: list,
),
),
Image.network("https://via.placeholder.com/350x500"), // error: hides above widget
]));
}
}
The way I understood your problem is that you want the bottom image to appear inside the list view instead of under it, as if it was just another item. Solution: Make it just another item!
More concrete, this is how your implementation for a helper function that enriches the list with the image may look like:
List<Widget> _buildListWithFooterImage(List<Widget> items) {
return items.followedBy([
Image.network("https://via.placeholder.com/350x500")
]);
}
Then, you could use that function during your build:
Widget build(BuildContext context) {
return SafeArea(
child: Column(
children: <Widget>[
Image.network("https://via.placeholder.com/350x150"),
Expanded(
child: ListView(
children: _buildListWithFooterImage(list)
)
),
]
)
);
}
Also, I believe your question is similar to this one.

Handling the app bar separately

I'm new to flutter and dart. I am trying to learn both by developing an app. I have taken the udacity course but it only gave me the basics. What I want to know is if it is possible to handle the appBar code separately.
Currently, this is what I have:
class HomePage extends StatelessWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
leading: new IconButton(
icon: new Icon(Icons.menu),
tooltip: 'Menu',
onPressed: () {
print('Pressed Menu');
},
),
title: new Text(title),
titleSpacing: 0.0,
actions: <Widget>[
new Row(
children: <Widget>[
new Column(
children: <Widget>[
new Text(
'Firstname Lastname',
textAlign: TextAlign.right,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12.0,
),
),
new Text("username#email.com",
textAlign: TextAlign.right,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12.0,
)),
],
mainAxisAlignment: MainAxisAlignment.center,
),
new Padding(
padding: new EdgeInsets.all(8.0),
child: new Image.network(
'https://s5.postimg.cc/bycm2rrpz/71f3519243d136361d81df71724c60a0.png',
width: 42.0,
height: 42.0,
),
),
],
),
],
),
body: new Center(
child: Text('Hello World!'),
),
);
}
}
However, I would like to handle the appbar code separately as I believe it can swell a bit more. I have tried something like this:
class HomePage extends StatelessWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: MyAppBar(),
body: new Center(
child: Text('Hello World!'),
),
);
}
}
class MyAppBar extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new AppBar(
leading: new IconButton(
icon: new Icon(Icons.menu),
tooltip: 'Menu',
onPressed: () {
print('Pressed Menu');
},
),
title: new Text(title),
titleSpacing: 0.0,
actions: <Widget>[
new Row(
children: <Widget>[
new Column(
children: <Widget>[
new Text(
'Firstname Lastname',
textAlign: TextAlign.right,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12.0,
),
),
new Text("username#email.com",
textAlign: TextAlign.right,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12.0,
)),
],
mainAxisAlignment: MainAxisAlignment.center,
),
new Padding(
padding: new EdgeInsets.all(8.0),
child: new Image.network(
'https://s5.postimg.cc/bycm2rrpz/71f3519243d136361d81df71724c60a0.png',
width: 42.0,
height: 42.0,
),
),
],
),
],
);
}
}
But then I'm getting this message:
The argument type 'MyAppBar' can't be assigned to the parameter type 'PreferredSizeWidget'
I have an intuition that this might not be possible. As I said, I'm new to flutter and dart and I have tried looking in the documentation and in other posts to no avail. Sorry if this seems stupid. I would just really like for someone to point me to the documentation, if there is any, on how to achieve this kind of things or any resource that can help me better understand how this works.
For your kind and valuable help, many thanks in advance!
the appBar widget must implement the PreferredSizeWidget class so you have to :
class MyAppBar extends StatelessWidget implements PreferredSizeWidget
and then you have to implemt this method also
Size get preferredSize => new Size.fromHeight(kToolbarHeight);
Full Example :
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
#override
Widget build(BuildContext context) {
return new AppBar(
leading: new IconButton(
icon: new Icon(Icons.menu),
tooltip: 'Menu',
onPressed: () {
print('Pressed Menu');
},
),
title: new Text(title),
titleSpacing: 0.0,
actions: <Widget>[
new Row(
children: <Widget>[
new Column(
children: <Widget>[
new Text(
'Firstname Lastname',
textAlign: TextAlign.right,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12.0,
),
),
new Text("username#email.com",
textAlign: TextAlign.right,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12.0,
)),
],
mainAxisAlignment: MainAxisAlignment.center,
),
new Padding(
padding: new EdgeInsets.all(8.0),
child: new Image.network(
'https://s5.postimg.cc/bycm2rrpz/71f3519243d136361d81df71724c60a0.png',
width: 42.0,
height: 42.0,
),
),
],
),
],
);
}
#override
Size get preferredSize => new Size.fromHeight(kToolbarHeight);
}
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
#override
Widget build(BuildContext context) {
return AppBar(
backgroundColor: Colors.blueGrey,
title: Text('News App'),
centerTitle: true,
leading: Icon(Icons.menu ),
);
}
#override
// TODO: implement preferredSize
Size get preferredSize => new Size.fromHeight(48);
}

Creating a Sticky Site Footer

I have not been able to locate any documentation for creating footer nav bars with Flutter/Dart. I know that "crossAxisAlignment: CrossAxisAlignment.end" can be used to pull content to the bottom of a column. However, I'm not sure how to render a site footer that sticks to the bottom of the screen. There are various solutions in flex and css grid, but not clear on what implementation would look like in this platform.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
Widget siteLogo = new Container(
padding: const EdgeInsets.only(top: 100.0),
child: new Image.asset(
'images/logo.png',
width: 180.0,
height: 180.0,
),
);
Widget titleTextSection = new Container(
padding: const EdgeInsets.only(left: 80.0, right: 80.0, top: 30.0, bottom: 20.0),
child: new Text(
''' Welcome to The Site''',
textAlign: TextAlign.center,
style: new TextStyle(
fontSize: 35.0,
fontWeight: FontWeight.w500,
),
),
);
Widget subtitleTextSection = new Container(
padding: const EdgeInsets.only(left: 40.0, right: 40.0, bottom: 40.0),
child: new Text(
'''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec imperdiet Donec imperdiet.''',
textAlign: TextAlign.center,
style: new TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.w400
),
),
);
// footer
Column signInLink(String label) {
return new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Container(
child: new Text(
label,
style: new TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w400,
color: const Color(0xFFf735e9)
),
),
),
],
);
}
Column existingAccountLink(String label) {
return new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Container(
child: new Text(
label,
style: new TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w400,
color: const Color(0xFFeceff1)
),
),
),
],
);
}
Widget footer = new Container(
height: 50.0,
decoration: new BoxDecoration(color: Colors.black),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
existingAccountLink('Already have an account?'),
signInLink('SIGN IN'),
],
),
);
return new MaterialApp(
title: 'Flutter Demo',
home: new Scaffold(
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage('images/backgroundimg.jpg'),
fit: BoxFit.cover,
),
),
child: new ListView(
children: [
siteLogo,
titleTextSection,
subtitleTextSection,
footer,
],
),
)
),
);
}
}
As you can see, I am currently using ListView to layout the widgets. The only thing I can think of right now is to put the whole ListView in a single column, crossAxisAlign to End so everything gets pulled down, and then style each widget relative to the footer. There must be a better way though?
Since you are using Scaffold, you can use bottomNavigationBar to have a 'sticky' bottom widget. (And potentially use BottomNavigationBar if you want to)
new Scaffold(
appBar: new AppBar(title: new Text("Title")),
body: new ListView.builder(
itemBuilder: (context, index) {
return new ListTile(
title: new Text("title $index"),
);
},
),
bottomNavigationBar: new Container(
height: 40.0,
color: Colors.red,
),
);
Alternatively your
The only thing I can think of right now is to put the whole ListView in a single column
is not a bad idea at all. That's how things works in flutter.
Although MainAxisAlignement.end is not the right way to do it.
You could achieve the same layout as with bottomNavigationBar without a Scaffold, using a Column this way :
new Column(
children: <Widget>[
new Expanded(
child: new ListView.builder(
itemBuilder: (context, index) {
return new ListTile(
title: new Text("title $index"),
);
},
),
),
new Container(
height: 40.0,
color: Colors.red,
),
],
),
The use of a listview is an excellent choice when you have a list of items that need a listview. Sometimes the other items in the layout might be fixed and do not need a listview. e.g using a column.
Example on how to achieve fixed bottom footer using stack.
#override
Widget build(BuildContext context) {
return new Material(
child: Scaffold(
body: Stack(
children : <Widget> [
Text("Top positioned text"),
Column(
children : <Widget> [
Text("Column:Top positioned text"),
Text("Column:Top positioned text")
]
),
Positioned(
bottom : 0,
child: Text("Bottom positioned text")
)
]
)
}
you can use Footer from flutter_layouts
sticky footer can be hard to implement. since you have to know the height of footer, it is not possible to perform this in build() function.
try out below flutter package.
https://github.com/softmarshmallow/flutter-layouts/tree/master/lib/src/footer
https://github.com/softmarshmallow/flutter-layouts/
by this, you can use footer with scaffold bottom nav. example
import 'package:flutter_layouts/flutter_layouts.dart';
class _FooterScreenState extends State<FooterScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("footer demo"),
),
body: buildBody(),
bottomNavigationBar: BottomNavigationBar(items: [
BottomNavigationBarItem(icon: Icon(Icons.add), title: Text("first")),
BottomNavigationBarItem(icon: Icon(Icons.remove), title: Text("second"))
]),
);
}
Widget buildBody() {
return Footer(
body: buildContent(),
footer: buildFooter(),
);
}
Widget buildContent() {
return ListView.builder(
itemBuilder: (c, i) {
return Card(
margin: EdgeInsets.all(16),
child: Container(
padding: EdgeInsets.all(24),
child: Text("contents"),
),
);
},
itemCount: 20,
);
}
Widget buildFooter() {
return Container(
padding: EdgeInsets.all(24),
decoration: BoxDecoration(color: Theme.of(context).primaryColor),
child: FlatButton(
onPressed: () {},
child: Text("Lean more", style: Theme.of(context).textTheme.button.copyWith(
color: Theme.of(context).colorScheme.onBackground
),),
),
);
}
}

Resources