Add Padding to Default Flutter App - dart

I'm learning Flutter, and I've made some VERY simple modifications to the starter app, so that the floating button changes the text item to a random string from an array.
The problem is that the text is too close to the edge of the screen--needs padding. But whenever I try to add a padding: element in any number of places, VSCode doesn't seem to like it. I've seen padding used in other example code, but can't seem to get it to work in this case.
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'Welcome to DadBot!',
),
new Text(
_txt,
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _changeText,
tooltip: 'Increment',
child: new Icon(Icons.add),
),
);
}

The easiest way to get padding is to wrap your widget in a Padding widget and specify the offset you want.

Fixed the issue by wrapping the Text widget in question within a Padding class, like so:
new Padding(
padding: new EdgeInsets.all(10.0),
child: new Text(
_txt,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.display1,
),
),
I'm still getting used to the way Flutter does things, but I like it!

Related

Flutter - Bottom Overflowed By 119 Pixels [duplicate]

This question already has answers here:
Bottom overloaded by 213 pixels in flutter
(18 answers)
Closed 12 months ago.
I had an error "Bottom Overflowed by 199 pixel" when creating an Image inside the ListView, and after i google it, all of them suggest me to add:
resizeToAvoidBottomPadding: false
But, it doesnt work! The error is still there.
SafeArea widget is also doesnt solve the problem. Here is the short code version of my layout:
body: ListView(
children:<Widget> [
new Container(
child: new Stack(
children:<Widget> [
//THE WIDGET
new Container(), //THE BACKGROND IMAGE
new Positioned(
child: Column(
children:<Widget>[
new Transform(),
new FadeTransition(),
new FadeTransition(),
Divider(),
new Row(),
//THE IMAGE THAT I WANT TO ADD
new Container(
height: 360.0
decoration: BoxDecoration(
image: DecorationImage(
image: Assetimage('lake.jpg)
Use Scaffold property "resizeToAvoidBottomPadding: false" and "SingleChildScrollView" as parent of Scaffold body:
home: Scaffold(
resizeToAvoidBottomInset : false,
appBar: AppBar(
title: Text("Registration Page"),
),
body: SingleChildScrollView(
child: RegisterUserPage(),
)),
put your contents in a SingleChildScrollView and add ConstrainedBox like this:
body :SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(),
child: ListView(
children:<Widget> [
new Container(
child: new Stack(
children:<Widget> [
//THE WIDGET
new Container(), //THE BACKGROND IMAGE
new Positioned(
child: Column(
children:<Widget>[
new Transform(),
new FadeTransition(),
new FadeTransition(),
Divider(),
new Row(),
//THE IMAGE THAT I WANT TO ADD
new Container(
height: 360.0
decoration: BoxDecoration(
image: DecorationImage(
image: Assetimage('lake.jpg)
This is may make your screen scrollable and adding constraint will make it finite scroll.
Nothing, Just include your widget inside Expanded like this
Expanded(
child: sectionList(),
)
//this solved my issue
Using resizeToAvoidBottomInset: true, in Scaffold and wrapping the first child in the body with SingleChildScrollView solved my problem.
Just Use
SingleChildScrollView()
like as
body: SingleChildScrollView(
child: Column(
children: [
widgetClassSectionButton(),
listAttandance.isNotEmpty ? headLineContainer() : msgNothingToShow(),
listAttandance.isNotEmpty ? widgetStudentList():widgetMsgEmpty(),
CustomButton("Submit Data",context)
],
),
)
The parameter in scaffold works for me, envolve your widget for this error.
singlechildscrollview
This works for me for long form:
return Scaffold(
resizeToAvoidBottomInset: true,
body: SingleChildScrollView(
child: IntrinsicHeight(
child: Form(
key: _formKey,
child: Column(...
This is How I solved it, adding a resizeToAvoidBottomInset: false, inside Scaffold() and using SingleChildScrollView() inside the body.
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.grey,
appBar: AppBar(
title: Text("Quotes"),
backgroundColor: Colors.green,
),
body: SingleChildScrollView(
child: Column(
children: quotes.map((quote) => quotesTemplete(quote)).toList(),
),
)
);
This aligns the item from bottom to top:
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: SingleChildScrollView(
reverse: true,

How do I increase font size in 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.

how can I make BottomNavigationBar stick on top of keyboard flutter

I am trying to make a simple chat app, so I created a scaffold and my body, will be the messages and my bottomNavigationBar would be my typing field and sending icon.
I added a text field but when typing the navigation bar is hidden by the keyboard.
this is the code of my BottomNavigationBar :
bottomNavigationBar: new Container(
height: ScreenSize.height/12,
/*color: Colors.red,*/
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new Container(
child: new Icon(Icons.send),
width:ScreenSize.width/6,
),
],
),
new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Material(
child: new Container(
child: new TextField(
autofocus: false,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(9.0),
border: InputBorder.none,
hintText: 'Please enter a search term',
),
),
width:ScreenSize.width*4/6,
),
elevation: 4.0,
/*borderRadius: new BorderRadius.all(new Radius.circular(45.0)),*/
clipBehavior: Clip.antiAlias,
type: MaterialType.card,
)
],
),
new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new Container(
child: Text('HELLO C1'),
color: Colors.green,
width:ScreenSize.width/6,
),
],
)
],
),
),
here is how it looks when focused :
if you use a Stack on your Scaffold's body, instead of bottomNavigationBar, your nav will push up above the keyboard. even if you fix to the bottom with a Positioned:
Positioned(
bottom: 0.0,
left: 0.0,
right: 0.0,
child: MyNav(),
),
simply wrap your bottom navigation bar with Padding and set it to MediaQuery.of(context).viewInsets,
bottomNavigationBar: Padding(
padding: MediaQuery.of(context).viewInsets,
child: ChatInputField(),
),
Literally just worked through the same issue. Given the code i was refactoring, this worked like a charm. Peep the github link, review his change and apply. Couldn't be much more straight forward: https://github.com/GitJournal/GitJournal/commit/f946fe487a18b2cb8cb1d488026af5c64a8f2f78..
Content of the link above in case the link goes down:
(-)BottomAppBar buildEditorBottonBar(
(+)Widget buildEditorBottonBar(
BuildContext context,
Editor editor,
EditorState editorState,
BottomAppBar buildEditorBottonBar(
folderName = "Root Folder";
}
*REPLACE* return BottomAppBar(
elevation: 0.0,
color: Theme.of(context).scaffoldBackgroundColor,
child: Row(
children: <Widget>[
FlatButton.icon(
icon: Icon(Icons.folder),
label: Text(folderName),
onPressed: () {
var note = editorState.getNote();
editor.moveNoteToFolderSelected(note);
},
)
],
mainAxisAlignment: MainAxisAlignment.center,
*WITH THE WRAPPER* return StickyBottomAppBar(
child: BottomAppBar(
elevation: 0.0,
color: Theme.of(context).scaffoldBackgroundColor,
child: Row(
children: <Widget>[
FlatButton.icon(
icon: Icon(Icons.folder),
label: Text(folderName),
onPressed: () {
var note = editorState.getNote();
editor.moveNoteToFolderSelected(note);
},
)
],
mainAxisAlignment: MainAxisAlignment.center,
),
),
);
}
class StickyBottomAppBar extends StatelessWidget {
final BottomAppBar child;
StickyBottomAppBar({#required this.child});
#override
Widget build(BuildContext context) {
return Transform.translate(
offset: Offset(0.0, -1 * MediaQuery.of(context).viewInsets.bottom),
child: child,
);
}
}
I achieved this by a mix of two things I found separated in the web:
1 - Inside the Scaffold, I put other with only a bottomNavigationBar with a empty Container. For some reason, this trick push all my real bottomNavigationBar up to the top of the keyboard.
Scaffold(
bottomNavigationBar: Container(
height: 0,
),
body: Scaffold(
body: MyWidget(
But, I did not want all the content up, so I got that Package:
2 - I added flutter_keyboard_visibility: ^5.1.0 from
https://pub.dev/packages/flutter_keyboard_visibility
With this Package, you can do anything you want in response to keyboard visibility - is up to you. In my case, I made all content of my real bottomNavigationBar disappear except the textfield, which stay on the top of the keyboard:
[TextFormField] // dont go away,
//The others:
KeyboardVisibilityBuilder(builder: (context, visible) {
return Column(
children: [
visible
? SizedBox(
height: 0,
)
: OtherWidgets(
If you need some kind of button; you can do:
Scaffold(
bottomNavigationBar: bottomNavigationBar,
floatingActionButton: ExampleButton(
text: 'Hello',
),
body: body,
),
You can apply further customizations on the Floating Action Button using parameters in the Scaffold.
There is a simple way to do this if you want to really need to use the bottom navigation bar of the scaffold to put your widgets in rather than put it on a stack. Just wrap your scaffold with another scaffold and it should solve the problem.
return Scaffold(
body: Scaffold(
bottomNavigationBar: yourBottomNavigationBarWidget(),
body: yourBody(),
This works best especially when the height of your widget changes dynamically (because the text user types may introduce multiple lines) and you want the body to resize accordingly. A body in the stack, as suggested by many, will require a bottom padding to be visible over the text field and need to change dynamically as user types which is difficult to handle when you have multiple widgets sitting in and around the text field.

Second page in flutter naviagtion flow is not full size

I am trying to make a basic "new activity" in flutter
I have followed the flutters docs and made a button that navigates to the next page with:
Navigator.push(context, new MaterialPageRoute(builder: (context)=> new HomePage(title: title,)));
This is the build method of the 2nd page/activity (HomePage.dart)
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Home"),
textTheme: orbitTextTheme),
body: new Center(
child: new Container(
color: Color.orange,
height: 150.0,
width: 150.0,
child:new Column(
children: <Widget>[
new TextField(
controller: _textController,
decoration: new InputDecoration(
hintText: "Try to type?"
),
)
],
),
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _newReg,
tooltip: 'New reg', //externalize
backgroundColor: Color.orange,
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
BUT, the second page is not full size: (Notice the missing floating action button)
Can anyone tell me what am doing wrong?
UPDATE
Was calling the Navigator.push( in a callback from googleSignIn (which presents a modal). So putting in a small delay fixed the bug. Thanks!
This graphical bug happens whenever you pop the last visible route before pushing a new one.
Consider using pushReplacement or pushReplacementNamed instead.

Flutter RaisedButton covering TextField

I have a TextField in my app that's immediately followed by a RaisedButton.
When I type in that TextField, the button is moved up to be just above my keyboard. If I scroll all the way down in the TextField, I can see all of the text. However, when I'm typing and go to a new line, it doesn't scroll all the way down, and the button covers part of the last line of text.
How can I make sure that the full line of text is always visible when I'm typing?
Ideally I'd like the button not to be above the keyboard when I'm typing but rather hidden by it.
If it's easier, though, I'd also be fine with a solution that ensures the TextField is scrolled all of the way down.
Here's the (simplified) relevant piece of my app:
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text('Title')),
body: new Column(children: <Widget>[
new Expanded(child: new Container(
child: new TextField(
maxLines: null,
controller: textController,
),
padding: new EdgeInsets.all(8.0),
)),
new RaisedButton(
onPressed: () => _myFunction(context),
child: new Center(child: new Text('Save'))
)
]),
);
}
By Adding
margin: new EdgeInsets.only(bottom: 50.0),
in Expanded Widget
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text('Title')),
body: new Column(children: <Widget>[
new Expanded(child: new Container(
margin: new EdgeInsets.only(bottom: 50.0),
child: new TextField(
maxLines: null,
controller: textController,
),
padding: new EdgeInsets.all(8.0),
)),
new RaisedButton(
onPressed: () => _myFunction(context),
child: new Center(child: new Text('Save'))
)
]),
);
}

Resources