How can I resolve child duplication error? - flutter

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.

Related

Flutter: Prevent some children from getting stretched

Do you have an idea on how to prevent child from getting stretched.
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(child: ...),
Row( // idon't want this child to be stretched
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
FlatButton(
child: Icon(Icons.add),
onPressed: () {},
color: Color(0xFFE2EDE4),
),
Text(
'$quantity',
style: Theme.of(context).textTheme.headline5.copyWith(
fontWeight: FontWeight.bold,
),
),
FlatButton(
child: Icon(Icons.remove),
onPressed: () {},
color: Color(0xFFE2EDE4),
),
],
),
],
);
I tried to wrap it in ConstrainedBox but still it is being stretched.
In this case, the crossAxisAlignment: CrossAxisAlignment.stretch property of Column don't affect to Row child. Instead of using mainAxisAlignment: MainAxisAlignment.spaceBetween
You can add SizedBox(width: ...)
If you want stretch Row widget, how about remove 'mainAxisAlignment' of Row.
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: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(height: 200, color: Colors.blue, child: Text('123'),),
Column(
children: <Widget> [
Row( // idon't want this child to be stretched
children: [
FlatButton(
child: Icon(Icons.add),
onPressed: () {},
color: Color(0xFFE2EDE4),
),
Text(
'qweqe',
style: Theme.of(context).textTheme.headline5.copyWith(
fontWeight: FontWeight.bold,
),
),
FlatButton(
child: Icon(Icons.remove),
onPressed: () {},
color: Color(0xFFE2EDE4),
),
],
),
]),
],
);
}
}

Updating part of a stateful widget

i'm very new to Flutter.
I have a stateful widget. Every time i click a counter, the whole build function runs and the results are updated.
But is there a way to make it a stateless widget and update only the relevant part somehow so i could avoid rebuilding the screen?
I have a stateful widget. Every time i click a counter, the whole build function runs and the results are updated.
But is there a way to make it a stateless widget and update only the relevant part somehow so i could avoid rebuilding the screen?
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home:Home(),
));
}
class Home extends StatefulWidget {
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State {
int long = 0;
void increaseCounter(){
setState(() {
long++;
});
}
void decreaseCounter(){
if(long > 0) {
setState(() {
long--;
});
}
}
Text numberOfDeliveries(){
return Text(
'$long deliveries today',
style: TextStyle(
color:Colors.white,
fontSize: 24.0,
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text('Deliveries'),
backgroundColor: Colors.green,
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Container(
color: Colors.blue,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child:Padding(
padding: const EdgeInsets.symmetric(vertical: 32.0, horizontal: 0.0),
child: Text(
'Long',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 32.0,
color: Colors.white,
),
),
),
),
Expanded(
child:
// is it possible to update this without running the whole build again?
numberOfDeliveries(),
),
Expanded(
child: Row(
children: [
Expanded(
child: Container(
padding: EdgeInsets.symmetric(vertical: 32.0, horizontal: 0.0),
color:Colors.blue[100],
child: FlatButton(
onPressed: (){
decreaseCounter();
},
child: Text('-'),
),
),
),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(vertical: 32.0, horizontal: 0.0),
color:Colors.blue[500],
),
),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(vertical: 32.0, horizontal: 0.0),
color:Colors.blue[900],
child: FlatButton(
onPressed: (){
increaseCounter();
},
child: Text('+'),
),
),
),
],
),
),
],
),
),
),
Expanded(
child: Container(
color: Colors.blue[800],
child: Row(
children: [
Text('Bottom'),
],
),
),
),
],
),
);
}
}

textAlign: Textalign.center not coming in center?why

class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: CircleAvatar(
radius: 40.0,
child: Image.asset('images/pic.jpg'),
),
),
Text(
"michael dub",
),
Text("FLUTTER DEVELOPER"),
Container(
width: 350.0,
height: 70.0,
child: Card(
child: Row(
children: <Widget>[
Icon(
Icons.phone,
),
Text(
"+12-345-678-910",
textAlign: TextAlign.center,
),
],
)))
],
),
),
);
}
}
in your column, try crossAxisAlignment: CrossAxisAlignment.stretch
EDIT:
if you're trying to do what I think you're trying to do, try adding
mainAxisAlignment: MainAxisAlignment.center
to your row
you need to wrap your text widget with Expanded widget. Default Row Widget will not take full width and it is creating issue.
Check out following code.
Expanded(
child: Text(
"+12-345-678-910",
textAlign: TextAlign.center,
),
),

Align text in centre of screen

I am trying to use Expand widget in Column so user can tap in any position of screen to increase counter, but there is an issue in alignment of text in crossAxisAlignment and mainAxisAlignment of the column it didn't apply on Expand widget as the following
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black12,
body: SafeArea(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
i++;
print(i);
});
},
child: Text(
i.toString(),
style: TextStyle(fontSize: 50.0, color: Colors.blueAccent),
textAlign: TextAlign.center,
),
),
),
],
),
),
),
),
);
You went with the wrong approach because you can't Center an Expanded because Expanded takes the entire available space inside Row or Column in your case Column with single Widget inside children the GestureDetector. Here is one of a solution for what you want to achieve
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black12,
body: SafeArea(
child: Center(
child: Stack(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: GestureDetector(
onTap: () {
setState(() => i++;);
},
),
),
],
),
Align(
alignment: Alignment.center,
child: Text(
'$i',
style: TextStyle(fontSize: 50.0, color: Colors.blueAccent),
),
),
],
),
),
),
);
}
another solution using FlatButton inside SizedBox.expand()
so you don't need to fight with a single child column.
class _MyAppState extends State<MyApp> {
int i = 0;
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black12,
body: SafeArea(
child: SizedBox.expand(
child: FlatButton(
onPressed: () {
setState(() {
i++;
print(i);
});
},
child: Text(
i.toString(),
style: TextStyle(fontSize: 50.0, color: Colors.blueAccent),
),
),
),
),
),
);
}
}

How to set state via a method from another class

Using Flutter and trying to move out a Widget to another class to keep it modular.
Problem is one of the inner Widget has a setState in it. But when I move it out to another class, it is not valid as it is not stateful. Initially thought of passing in a Function and replace that with the setState function itself but that doesn't seem to work. How can I come around this?
Current method that works fine. I plan to move out everything from line 6 (child: Center) to another class as this is going to be repeated via a PageView. Going to be swiping this page to next page, each looking the same, only the text changes.
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.tealAccent,
body: SafeArea(
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"Did you hear about the claustrophobic astronaut?",
style: TextStyle(
fontSize: 20,
),
),
Text(
"He just needed a little space.",
style: TextStyle(
fontSize: 20,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
GestureDetector(
child: Icon(heartStatus),
onTap: () {
setState(() {
if (heartStatus == FontAwesomeIcons.heart) {
heartStatus = FontAwesomeIcons.solidHeart;
} else {
heartStatus = FontAwesomeIcons.heart;
}
});
},
),
GestureDetector(
child: Icon(FontAwesomeIcons.shareAlt),
onTap: (){
print('shared');
},
),
],
),
],
),
),
),
),
);
}
This is after moving it to anther class which won't work as set state is not valid here. Passing in function here throws error -> Unnecessary statement
Widget getPageData(Function function){
IconData heartStatus = FontAwesomeIcons.heart;
return Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"Did you hear about the claustrophobic astronaut?",
style: TextStyle(
fontSize: 20,
),
),
Text(
"He just needed a little space.",
style: TextStyle(
fontSize: 20,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
GestureDetector(
child: Icon(heartStatus),
onTap: () {
setState(() {
//tried passing in function here
if (heartStatus == FontAwesomeIcons.heart) {
heartStatus = FontAwesomeIcons.solidHeart;
} else {
heartStatus = FontAwesomeIcons.heart;
}
});
},
),
GestureDetector(
child: Icon(FontAwesomeIcons.shareAlt),
onTap: (){
print('shared');
},
),
],
),
],
),
),
);
}
You can turn your function:
Widget getPageData(Function function){...}
To a Customised Widget
class CustomizeWidget extend StatefulWidget{
}
class _CustomizeWidgetState extend State<StatefulWidget>{
/// in the build function, you can call setState
}