body of nestedscrollview overflowed - flutter - flutter

I made a page with NestedScrollView, the body of NestedScrollView is a container that holds data from previous requests. but the body of the NestedScrollView is overflowed. The following code:
NestedScrollView(
controller: _scrollViewController,
headerSliverBuilder:
(BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
title: new Text(
nama,
style: TextStyle(color: putih),
),
iconTheme: IconThemeData(color: putih),
backgroundColor: colorPrimaryDark,
)
];
},
body: Container(
child: Column(
children: <Widget>[
Container(
color: goldtua,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text(
string.harga,
style: TextStyle(color: putih, fontSize: 24.0),
),
Text(
formatingRupiah(harga),
style: TextStyle(color: putih, fontSize: 24.0),
),
],
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
string.fasilitas,
style: TextStyle(fontSize: 18.0),
),
Expanded(
child: Text(
fasilitas, //this is the result of the request, the text is multiline so it takes up less space
style: TextStyle(fontSize: 18.0),
),
)
],
),
......
Please, can someone tell me where do I do it wrong?

If you want or need to keep using the NestedScrollView, you can use ListView instead Column to ListView, just like this:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
class HomePage extends StatefulWidget {
HomePage() : super();
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
CupertinoSliverNavigationBar(
largeTitle: Text('My large title'),
)
];
},
body: ListView( // This is the right way
children: [
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
],
),
),
);
}
}
You will see a screen like this.
Notice, that if you change only the ListView to column, you will get this result:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
class HomePage extends StatefulWidget {
HomePage() : super();
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
CupertinoSliverNavigationBar(
largeTitle: Text('My large title'),
)
];
},
body: Column( // I changed here from ListView and Column and I will get an overflow error
children: [
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
],
),
),
);
}
}

You're getting this error because the size of your content is bigger than the size of the container. That means the Container size is smaller than the size of Column. In order to solve this issue, you should wrap Container with a Scrollable Widget like SingleChildScrollView which will scroll overflowing content and match parents size or available space. You don't need to use Expanded since each item of Column will fill only needed space
body:
SingleChildScrolView(
child: Column(
children: <Widget>[
Container(
color: goldtua,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text(
string.harga,
style: TextStyle(color: putih, fontSize: 24.0),
),
Text(
formatingRupiah(harga),
style: TextStyle(color: putih, fontSize: 24.0),
),
],
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
string.fasilitas,
style: TextStyle(fontSize: 18.0),
),
Expanded(
child: Text(
fasilitas, //this is the result of the request, the text is multiline so it takes up less space
style: TextStyle(fontSize: 18.0),
),
)
],
),
......

Simply just use a SingleChildScrollView at the root of body:
body: SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[.....

Related

Flutter Angela Yu example

I want to print first element in the list "first ques"
but I'm getting different sort of errors tried using final also, then all sort of errors.
class _QuizPageState extends State<QuizPage> {
List<Widget> scoreKeeper = [];
List<String> questions = ["First ques", "seacond ques", "third ques"];
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
"example text"
questions[0], //why its not displaying "first ques"
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
try this
class _QuizPageState extends State<QuizPage> {
List<Widget> scoreKeeper = [];
List<String> questions = ["First ques", "seacond ques", "third ques"];
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
questions[0], //why its not displaying "first ques"
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
the reason you are getting error is that the Text widget takes only one string and you are passing two string to it.
First, you should remove const from the Expanded widget because you're using a variable inside on Text which is questions[0].
And I want to mention that its better to use questions.first for readability proposes and check if the list is empty before it to prevent possible errors.
So Here's the final result:
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 5,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: Text(
questions.first,
//why its not displaying "first ques"
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
],
),

Adding a spacing between card when using Alphabet List Scroll View

I have created a page with shows all the users from my app, in this page I have used a dependenci with uses alphabetic scroll view. Althought when I try to set a spacing between card, it won't work.
This is how it is looking:
This is my screen:
import 'package:alphabet_list_scroll_view/alphabet_list_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:loja_virtual_nnananene/common/custom_drawer/custom_drawer.dart';
import 'package:loja_virtual_nnananene/models/admin_orders_manager.dart';
import 'package:loja_virtual_nnananene/models/admin_users_manager.dart';
import 'package:loja_virtual_nnananene/models/page_manager.dart';
import 'package:provider/provider.dart';
class AdminUsersScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
drawer: CustomDrawer(),
appBar: AppBar(
title: Text(
'Usuários',
style: GoogleFonts.firaCode(),
),
centerTitle: true,
),
body: Consumer<AdminUsersManager>(
builder: (_, adminUsersManager, __) {
return AlphabetListScrollView(
itemBuilder: (_, index) {
return Card(
margin: EdgeInsets.symmetric(horizontal: 16),
elevation: 3,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
adminUsersManager.users[index].name!,
style: GoogleFonts.firaCode(),
),
Text(adminUsersManager.users[index].email!,
style: GoogleFonts.firaCode()),
],
),
));
},
highlightTextStyle: GoogleFonts.firaCode(
textStyle: TextStyle(
color: Colors.black,
fontSize: 20,
)),
indexedHeight: (index) => 60,
strList: adminUsersManager.names,
showPreview: true,
);
},
));
}
}
This is the dependecie:
https://pub.dev/packages/alphabet_list_scroll_view
const itemMargin = EdgeInsets.symmetric(vertical: 10, horizontal: 16);
return AlphabetListScrollView(
itemBuilder: (_, index) {
return Card(
margin: itemMargin,
elevation: 3,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
adminUsersManager.users[index].name!,
style: GoogleFonts.firaCode(),
),
Text(adminUsersManager.users[index].email!,
style: GoogleFonts.firaCode()),
],
),
));
},
highlightTextStyle: GoogleFonts.firaCode(
textStyle: TextStyle(
color: Colors.black,
fontSize: 20,
)),
indexedHeight: (index) => 60 + itemMargin.vertical,
strList: adminUsersManager.names,
showPreview: true,
);
There are multiple ways you can do this.
Try using mediaquery if you think your app is going to be used on multiple devices.
Use some paddings.
Wrap your Card inside a container and set size to it.
I recommend you to use the Container instead of Card, you can replace this code i hope the ui you have designed will be same.
Container(
margin: EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration(boxShadow: [BoxShadow(blurRadius: 4, spreadRadius: 8)],borderRadius: BorderRadius.all(Radius.circular(10))),
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
adminUsersManager.users[index].name!,
style: GoogleFonts.firaCode(),
),
Text(adminUsersManager.users[index].email!, style: GoogleFonts.firaCode()),
],
),
),

Text widget always stays on top and doesn't disappear as I scroll

My text widget "More" always stays on top even though I scroll down, I'm not finding the root of this issue,
I've tried wrapping it in a SingleChildScrollView widget but still nothing has changed, are there any arguments for the Text widget that allows it to scroll as the user scrolls that I don't know of? Or is it something else entirely?
Code:
class MorePage extends StatefulWidget {
#override
_MorePageState createState() => _MorePageState();
}
class _MorePageState extends State<MorePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: white,
body: Stack(
// YESSSS
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center, children: [
SizedBox(height: 45.0),
Padding(
padding: const EdgeInsets.symmetric(horizontal: kDefaultPaddin),
child: SizedBox(
height: 25,
child: Text(
"MORE",
style: TextStyle(
color: black,
fontFamily: "Raleway",
fontWeight: FontWeight.w700,
fontSize: 25),
textAlign: TextAlign.center,
),
),),
]),
Container(
// YESSSS
child: getBody(),
),
],
),
);
}
You are using Stack(), Stack will overlay children on top of others.
while using Stack wrap with fixed-Sized Widget, It can be just Container(), also while using Stack, use Aligning widgets as child => Center, Align, Positioned, else use stack fit property,
to know more about Stack Visit https://api.flutter.dev/flutter/widgets/Stack-class.html
import 'package:flutter/material.dart';
class MorePage extends StatefulWidget {
#override
_MorePageState createState() => _MorePageState();
}
class _MorePageState extends State<MorePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Container(
child: Stack(
// YESSSS
children: <Widget>[
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
SizedBox(height: 45.0),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 22),
child: SizedBox(
height: 25,
child: Text(
"MORE",
style: TextStyle(
color: Colors.black,
fontFamily: "Raleway",
fontWeight: FontWeight.w700,
fontSize: 25),
textAlign: TextAlign.center,
),
),
),
]),
Center(
child: Container(
color: Colors.amber,
// YESSSS
height: 1333,
child: Text("Body")
// getBody(),
),
),
],
),
),
),
);
}
}

How can I resolve child duplication error?

How to resolve child duplication error:
Error: Duplicated named argument 'child'.
child: CenteredView(
This is my code:
Container(
child: RaisedButton(
onPressed: (){},
child: Text(
'Alcoholic Beverages',
style: TextStyle(
decoration:TextDecoration.underline,
fontSize: 25,
),
),
),
child: CenteredView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
NavigationBar()
]
)
)
),
]
),
);
}
}
In your Container you have two child, when Container widget can take only a single child. So you need to use some widget that can have more than one child. Column and Row allows you to do that.
Check this link to read more about multi-child widget, unlike Container, which is single-child widget, Column and Row are Multi-child widget.
Multi-Child Widget
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
class selClass extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: sel(),
);
}
}
class sel extends StatefulWidget {
#override
_selState createState() => _selState();
}
class _selState extends State<sel> {
String _date = "Not set";
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(child:SingleChildScrollView(
child: Container( child: Column (
children: [
RaisedButton(
onPressed: (){},
child: Text(
'Alcoholic Beverages',
style: TextStyle(
decoration:TextDecoration.underline,
fontSize: 25,
),
),
),
CenteredView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
NavigationBar()
]
),
),
]
),
);
}
}
Container can have only One Child . That's why you're getting this error. You need to Column if your view is vertical or Row if your view is horizontal. Use this to solve your problem:
Column :
Container(
child: Column(
children: [
RaisedButton(
onPressed: () {},
child: Text(
'Alcoholic Beverages',
style: TextStyle(
decoration: TextDecoration.underline,
fontSize: 25,
),
),
),
CenteredView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[NavigationBar()])),
],
),
),
);
}
}
OR
Row :
Container(
child: Row(
children: [
RaisedButton(
onPressed: () {},
child: Text(
'Alcoholic Beverages',
style: TextStyle(
decoration: TextDecoration.underline,
fontSize: 25,
),
),
),
CenteredView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[NavigationBar()])),
],
),
),
);
}
}
Container widget can only have one child.
To lay out multiple children, let this widget's child be a widget such as Row, Column, or Stack.

How to adjust Font Size of Flutter App according to Font Size of Device

I'm learning Flutter.
I am not able to find the solution of a problem in Flutter.
The Problem is:
I have created an App and have tested it in my phone and everything seems fine but when I run the same App in another phone then the fonts of the App in that phone is large than the previous one and it's Overflowing by some pixels.
It's seems like my App is not able to adjust it's font size according to the device font size.
Please anyone help me to solve this problem
Screenshot of First Mobile
Redme 5A
Screenshot of Second Mobile
Realme 6
Code Snippet
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home:Chicken(),
));
}
class Chicken extends StatefulWidget {
Chicken({Key key}) : super(key: key);
#override
_ChickenState createState() => _ChickenState();
}
class Item {
Item({this.isExpanded = false, this.headerValue, this.expandedValue});
bool isExpanded;
Widget expandedValue;
String headerValue;
}
Text customText({text}) {
return Text(
text,
style: TextStyle(fontSize: 25.0, fontWeight: FontWeight.normal),
);
}
class _ChickenState extends State<Chicken> {
List<Item> _items = <Item>[
Item(
headerValue: 'Shop 1',
expandedValue: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
customText(text: 'Time: '),
customText(text: 'Rate: '),
customText(text: 'Location: '),
],
),
SizedBox(
height: 15,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
customText(text: '9:00 AM to 8:00 Pm'),
customText(text: 'Rs. 100'),
customText(text: 'ABC'),
],
),
],
)),
Item(
headerValue: 'Shop 2',
expandedValue: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
customText(text: 'Time: '),
customText(text: 'Rate: '),
customText(text: 'Location: '),
],
),
SizedBox(
height: 15,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
customText(text: '9:00 AM to 8:00 Pm'),
customText(text: 'Rs. 100'),
customText(text: 'ABC'),
],
),
],
)),
Item(
headerValue: 'Shop 3',
expandedValue: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
customText(text: 'Time: '),
customText(text: 'Rate: '),
customText(text: 'Location: '),
],
),
SizedBox(
height: 15,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
customText(text: '9:00 AM to 8:00 Pm'),
customText(text: 'Rs. 100'),
customText(text: 'ABC'),
],
),
],
)),
];
#override
Widget build(BuildContext context) {
Color headerColor;
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
'Chicken',
style: TextStyle(
fontSize: 30.0,
fontWeight: FontWeight.bold,
),
),
),
body: ListView(
children: <Widget>[
ExpansionPanelList(
expansionCallback: (int index, bool isExpanded) {
setState(() {
_items[index].isExpanded = !_items[index].isExpanded;
});
},
children: _items.map<ExpansionPanel>((Item item) {
return ExpansionPanel(
canTapOnHeader: true,
headerBuilder: (BuildContext context, bool isExpanded) {
headerColor = isExpanded ? Colors.blue : Colors.black;
return Container(
padding: EdgeInsets.only(top: 8.0),
child: Text(
item.headerValue,
style: TextStyle(
fontSize: 30.0,
color: headerColor,
),
textAlign: TextAlign.center,
),
);
},
isExpanded: item.isExpanded,
body: Container(
child: item.expandedValue,
padding: EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 10.0),
));
}).toList())
],
),
);
}
}
Try using media queries and define the font size dynamically.
double screenWidth = MediaQuery.of(context).size.width
double screenHeight = MediaQuery.of(context).size.height
So, your font size should be something like this:
fontSize: screenWidth * 0.01. (change this constant according to your need)
Example:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
return Scaffold(
body: Center(
child: Container(
child: Text(
"Hey there",
style: TextStyle(
fontSize: screenWidth * 0.1
),
),
),
),
);
}
}
I have also answer, with the easy technique. its work for me,
for text define,
Text('Setting',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: sizeTextHeaderSet(context),
),
)
and here is my sizeTextHeaderSet method(i use the method because may places I need to use this)
double sizeTextHeaderSet(context){
double unitHeightValue = MediaQuery.of(context).size.height * 0.01;
double customSize = 2.5;
return customSize*unitHeightValue;
}