Getting overflowed error on dynamic chips in flutter - flutter

I am developing this UI in flutter (1st attached picture). Fetching chips from DB and dynamically creating their list. When rendering them on the UI, I am getting overflowed error (2nd attached picture). I want chips to move to the next line if there is no space available. Following is my code:
Container(
padding: EdgeInsets.only(left: 5.0, right: 5.00),
child: Card(
elevation: 4,
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.only(
top: 10.0, bottom: 10.00, left: 10.0, right: 10.00),
child: Column(children: <Widget>[
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(
child: Text('Every Month',
style: TextStyle(
fontSize: 14,
color: Color(0xFF0d1b41),
fontFamily: 'Poppins',
fontWeight: FontWeight.bold)),
),
new Container(
child: Text('Every 28th of month',
style: TextStyle(
fontSize: 12,
color: Color(0xFFb1b1b1),
fontFamily: 'Poppins',
fontWeight: FontWeight.bold)),
),
],
),
Column(
children: <Widget>[
new Container(
padding: EdgeInsets.only(
bottom: 20.0,
),
child: Text('Scheduled',
textAlign: TextAlign.right,
style: TextStyle(
fontSize: 12,
color: Color(0xFFfbb450),
fontFamily: 'Poppins',
fontStyle: FontStyle.italic,
)),
),
],
),
],
),
SizedBox(height: 10),
Divider(
color: Colors.grey[500],
thickness: 1.0,
),
Row(children: <Widget>[
Column(
children: <Widget>[
new Container(
width: 60,
height: 70,
padding: EdgeInsets.only(
top: 10.0,
bottom: 40.00,
left: 10.0,
right: 8.00),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
image: DecorationImage(
image: new NetworkImage(
'https://images.unsplash.com/photo-1519125323398-675f0ddb6308?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=94a1e718d89ca60a6337a6008341ca50&auto=format&fit=crop&w=1950&q=80'),
fit: BoxFit.fill,
),
),
),
],
),
Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
width: 220,
child: new Text(
"Coca Cola Zero",
style: new TextStyle(
fontSize: 18.0,
color: Color(0xff8c8c8c),
fontWeight: FontWeight.bold,
fontFamily: 'Poppins'),
textAlign: TextAlign.left,
),
)
],
),
SizedBox(height: 10.0),
generateTags(tagList),
SizedBox(height: 10.0),
]),
]),
])),
],
),
));
static Widget generateTags(List<String> productTagList) {
tagList = productTagList;
return Wrap(
children: tagWidgets.toList(),
);
}
Kindly guide me to create this UI

not exact... but you can modify ...
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var list1 = ["abc", "sdf", "ags", "qwe","eded","ecc","fef","g4g","wev"];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
children: <Widget>[imgWidget(), content()],
),
],
));
}
imgWidget() {
return Container(
height: 40.0,
width: 40.0,
color: Colors.amber,
);
}
content() {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[_title(), _chips(), _actionBtns()],
);
}
_title() {
return Text("CocaCola Zero");
}
_chips() {
return Container(
width: MediaQuery.of(context).size.width-50,
child: Wrap(
children: list1.map((String e) => Chip(label: Text(e))).toList(),
),
);
}
_actionBtns() {
return Row(
children: <Widget>[
_customBTN(icon: Icons.edit, btnName: "Edit", onTap: () {}),
_customBTN(icon: Icons.delete, btnName: "Delete", onTap: () {})
],
);
}
_customBTN({Function onTap, String btnName, IconData icon}) {
return MaterialButton(
child: Row(
children: <Widget>[
Icon(icon),
Text(btnName),
],
),
onPressed: onTap);
}
}

Please use wrap your chips Wrap widget.
class MultiSelectChip extends StatefulWidget {
final List<String> reportList;
final Function(List<String>) onSelectionChanged; // +added
MultiSelectChip(
this.reportList,
{this.onSelectionChanged} // +added
);
#override
_MultiSelectChipState createState() => _MultiSelectChipState();
}
class _MultiSelectChipState extends State<MultiSelectChip> {
// String selectedChoice = "";
List<String> selectedChoices = List();
_buildChoiceList() {
List<Widget> choices = List();
widget.reportList.forEach((item) {
choices.add(Container(
padding: const EdgeInsets.all(2.0),
child: ChoiceChip(
label: Text(item),
selected: selectedChoices.contains(item),
onSelected: (selected) {
setState(() {
selectedChoices.contains(item)
? selectedChoices.remove(item)
: selectedChoices.add(item);
widget.onSelectionChanged(selectedChoices); // +added
});
},
),
));
});
return choices;
}
#override
Widget build(BuildContext context) {
return Wrap(
children: _buildChoiceList(),
);
}
}

Related

how to show multiple comment_tree: ^0.3.0 in flutter , I want to show multiple comment in comment page Pleas help me

'here is comment widget i want to show this multiple comment widget in comment page and i also trying to lot of trying to put this comment widget in listview when i put this widget in listview my screeen is not rendered. but when i put this in gridview it is showing all comment properly plese help me'
import 'package:comment_tree/comment_tree.dart';
import 'package:comment_tree/widgets/comment_tree_widget.dart';
import 'package:ecllipsenew/data/repository/post_repo.dart';
import 'package:ecllipsenew/provider/PostProvider.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:provider/provider.dart';
import '../../../../util/app_constants.dart';
class Comments extends StatefulWidget {
final List<Comment> commentList;
Comments({this.commentList});
#override
State<Comments> createState() => _CommentsState();
}
class _CommentsState extends State<Comments> {
bool reply = false;
final List<Comment> commentLi = [
Comment(
avatar: 'nukkll',
userName: 'njjull',
content: 'I mkk interested',
),
Comment(
avatar: 'null',
userName: 'null',
content: 'I mjjj interested',
),
];
#override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: Container(
child: CommentTreeWidget<Comment, Comment>(
commentLi[0],
[
commentLi[0],
commentLi[1],
],
treeThemeData:
TreeThemeData(lineColor: Colors.blue, lineWidth: 3),
avatarRoot: (context, data) => const PreferredSize(
child: CircleAvatar(
radius: 18,
backgroundColor: Colors.grey,
backgroundImage: NetworkImage(AppConstants.User_Url)),
preferredSize: Size.fromRadius(18),
),
avatarChild: (context, data) => const PreferredSize(
child: CircleAvatar(
radius: 12,
backgroundColor: Colors.grey,
backgroundImage: NetworkImage(AppConstants.User_Url)),
preferredSize: Size.fromRadius(12),
),
contentChild: (context, data) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(12)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${data.userName}',
style: Theme.of(context)
.textTheme
.caption
?.copyWith(
fontWeight: FontWeight.w600,
color: Colors.black),
),
SizedBox(
height: 4,
),
Text(
'${data.content}',
style: Theme.of(context)
.textTheme
.caption
?.copyWith(
fontWeight: FontWeight.w300,
color: Colors.black),
),
],
),
),
DefaultTextStyle(
style: Theme.of(context).textTheme.caption.copyWith(
color: Colors.grey[700], fontWeight: FontWeight.bold),
child: Padding(
padding: EdgeInsets.only(top: 4),
child: Row(
children: [
SizedBox(
width: 8,
),
Text('5d'),
SizedBox(
width: 8,
),
Text('Like'),
SizedBox(
width: 24,
),
Text('Reply'),
],
),
),
)
],
);
},
contentRoot: (context, data) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(12)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Tina Chuhan',
style: Theme.of(context).textTheme.caption.copyWith(
fontWeight: FontWeight.w600,
color: Colors.black),
),
SizedBox(
height: 4,
),
Text(
'${data.content}',
style: Theme.of(context).textTheme.caption.copyWith(
fontWeight: FontWeight.w300,
color: Colors.black),
),
],
),
),
DefaultTextStyle(
style: Theme.of(context).textTheme.caption.copyWith(
color: Colors.grey[700], fontWeight: FontWeight.bold),
child: Padding(
padding: EdgeInsets.only(top: 4),
child: Row(
children: const [
SizedBox(
width: 8,
),
Text('5d'),
SizedBox(
width: 8,
),
Text('Like'),
SizedBox(
width: 24,
),
Text('Reply'),
],
),
),
)
],
);
},
),
padding: EdgeInsets.symmetric(vertical: 12, horizontal: 16),
),
),
],
);
// }
// );
// });
}
}
and here is my comment show page
import 'package:comment_tree/comment_tree.dart';
import 'package:ecllipsenew/screen/home/pages/comment/Comments.dart';
import 'package:ecllipsenew/screen/home/pages/comment/new_message.dart';
import 'package:ecllipsenew/util/app_constants.dart';
import 'package:flutter/material.dart';
class CommentPage extends StatefulWidget {
static const routeName = '/Comment_Page';
const CommentPage({Key key}) : super(key: key);
#override
State<CommentPage> createState() => _CommentPageState();
}
class _CommentPageState extends State<CommentPage> {
// final List<Comments> commentList = [
// Comments(
// commentId: "3",
// commentUserId: "11",
// commentUserName: "Himanshu",
// commentUserProfileImage: AppConstants.User_Url,
// commentType: "text",
// commentFile: "jjjj",
// commentDate: "12/12/12",
// commentTime: "12:30",
// commentReacts: "hhh",
// letestReplys: "jjjj")
// ];
final List<Comment> commentLi = [
Comment(
avatar: 'null',
userName: 'null',
content: 'I m interested',
),
Comment(
avatar: 'null',
userName: 'null',
content: 'I m interested',
),
];
#override
Widget build(BuildContext context) {
return
// Scaffold(
// appBar: AppBar(
// title: Text("Comment"),
// ),
// body:
SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[
//when I add this line my screen is not rendered
for (int i = 0; i < 3; i++) Comments(commentList: commentLi),
Expanded(
child: Comments(commentList: commentLi),
),
NewMessage()
],
),
),
);
// );
}
}
The main cause of render error is using Expanded widget inside Column, without having a fixed height for that Column.
If you want to use Expanded widget inside Column, you should wrap the column with a sizedbox and give it a fixed height as shown below:
SizedBox(
height: MediaQuery.of(context).size.height * 0.8,
child: Column(
children: [
Text('Header'),
Expanded(
child: Container(
child: Center(
child: Text('I take the remaining space'),
),
),
)
],
)
I could render the same code by removing the Expanded widgets wherever you have used them. They seem unnecessary too.
Screenshot of commentPage by removing Expanded widget everywhere
Please find full code as you have asked for in previous answer. Still, I recommend you to know the basic of flutter-layout to understand working with Expanded widget before using this code.
import 'package:flutter/material.dart';
import 'package:test/view.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const CommentPage());
}
}
Comments page code
import 'package:comment_tree/comment_tree.dart';
import 'package:flutter/material.dart';
import 'package:test/test.dart';
class CommentPage extends StatefulWidget {
static const routeName = '/Comment_Page';
const CommentPage({Key? key}) : super(key: key);
#override
State<CommentPage> createState() => _CommentPageState();
}
class _CommentPageState extends State<CommentPage> {
// final List<Comments> commentList = [
// Comments(
// commentId: "3",
// commentUserId: "11",
// commentUserName: "Himanshu",
// commentUserProfileImage: AppConstants.User_Url,
// commentType: "text",
// commentFile: "jjjj",
// commentDate: "12/12/12",
// commentTime: "12:30",
// commentReacts: "hhh",
// letestReplys: "jjjj")
// ];
final List<Comment> commentLi = [
Comment(
avatar: 'null',
userName: 'null',
content: 'I m interested',
),
Comment(
avatar: 'null',
userName: 'null',
content: 'I m interested',
),
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Comment"),
),
body: SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[
for (int i = 0; i < 3; i++) Comments(commentList: commentLi),
// Comments(commentList: commentLi),
// NewMessage()
],
),
),
),
);
// );
}
}
Comments tree page
import 'package:comment_tree/comment_tree.dart';
import 'package:flutter/material.dart';
class Comments extends StatefulWidget {
final List<Comment> commentList;
const Comments({Key? key, required this.commentList}) : super(key: key);
#override
State<Comments> createState() => _CommentsState();
}
class _CommentsState extends State<Comments> {
bool reply = false;
final List<Comment> commentLi = [
Comment(
avatar: 'nukkll',
userName: 'njjull',
content: 'I mkk interested',
),
Comment(
avatar: 'null',
userName: 'null',
content: 'I mjjj interested',
),
];
#override
Widget build(BuildContext context) {
return Column(
children: [
Container(
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
child: CommentTreeWidget<Comment, Comment>(
commentLi[0],
[
commentLi[0],
commentLi[1],
],
treeThemeData:
const TreeThemeData(lineColor: Colors.blue, lineWidth: 3),
avatarRoot: (context, data) => const PreferredSize(
preferredSize: Size.fromRadius(18),
child: CircleAvatar(
radius: 18,
backgroundColor: Colors.grey,
backgroundImage: NetworkImage(
"https://i.pinimg.com/736x/6f/de/85/6fde85b86c86526af5e99ce85f57432e.jpg")),
),
avatarChild: (context, data) => const PreferredSize(
preferredSize: Size.fromRadius(12),
child: CircleAvatar(
radius: 12,
backgroundColor: Colors.grey,
backgroundImage: NetworkImage(
"https://i.pinimg.com/736x/6f/de/85/6fde85b86c86526af5e99ce85f57432e.jpg")),
),
contentChild: (context, data) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding:
const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(12)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${data.userName}',
style: Theme.of(context).textTheme.caption?.copyWith(
fontWeight: FontWeight.w600, color: Colors.black),
),
const SizedBox(
height: 4,
),
Text(
'${data.content}',
style: Theme.of(context).textTheme.caption?.copyWith(
fontWeight: FontWeight.w300, color: Colors.black),
),
],
),
),
DefaultTextStyle(
style: Theme.of(context).textTheme.caption!.copyWith(
color: Colors.grey[700], fontWeight: FontWeight.bold),
child: Padding(
padding: const EdgeInsets.only(top: 4),
child: Row(
children: const [
SizedBox(
width: 8,
),
Text('5d'),
SizedBox(
width: 8,
),
Text('Like'),
SizedBox(
width: 24,
),
Text('Reply'),
],
),
),
)
],
);
},
contentRoot: (context, data) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding:
const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(12)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Tina Chuhan',
style: Theme.of(context).textTheme.caption!.copyWith(
fontWeight: FontWeight.w600, color: Colors.black),
),
const SizedBox(
height: 4,
),
Text(
'${data.content}',
style: Theme.of(context).textTheme.caption!.copyWith(
fontWeight: FontWeight.w300, color: Colors.black),
),
],
),
),
DefaultTextStyle(
style: Theme.of(context).textTheme.caption!.copyWith(
color: Colors.grey[700], fontWeight: FontWeight.bold),
child: Padding(
padding: const EdgeInsets.only(top: 4),
child: Row(
children: const [
SizedBox(
width: 8,
),
Text('5d'),
SizedBox(
width: 8,
),
Text('Like'),
SizedBox(
width: 24,
),
Text('Reply'),
],
),
),
)
],
);
},
),
),
],
);
}
}

Flutter: is there any possibility to send the button value to the text field?

I am Writing a small quiz game, in which I am pressing the button and these buttons are going to the empty text fields, I don't know how to send the text of the button to the text fields.
here is my code :
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: NinjaCard()));
class NinjaCard extends StatefulWidget {
#override
_NinjaCardState createState() => _NinjaCardState();
}
class _NinjaCardState extends State<NinjaCard> {
String result = "";
String shaka = "";
var text;
String str;
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(title: Text('Animals'), backgroundColor: Colors.green),
body: Padding(
padding: EdgeInsets.all(15),
child: Column(
children: <Widget>[
Center(
child: Image.asset('lib/photo-1495594059084-33752639b9c3.jpg'),
),
SizedBox(width: 10, height: 10),
Row(children: <Widget>[
Container(
color: Colors.grey,
width: 40.0,
child: Text('$result', style: TextStyle(fontSize: 10.0, height: 2.0, color: Colors.black)),
),
SizedBox(width: 10),
Container(
color: Colors.grey,
width: 40.0,
child: Text('$shaka', style: TextStyle(fontSize: 10.0, height: 2.0, color: Colors.black)),
),
SizedBox(width: 15),
Row(
children: <Widget>[
SizedBox(
width: 50,
child: RaisedButton(
onPressed: () {},
color: Colors.green,
splashColor: Colors.red,
child: Text('S', style: TextStyle(backgroundColor: Colors.green, fontSize: 20, color: Colors.white)),
),
),
SizedBox(width: 15),
SizedBox(
width: 50,
child: RaisedButton(
onPressed: () {},
color: Colors.green,
splashColor: Colors.red,
child: Text('T', style: TextStyle(backgroundColor: Colors.green, fontSize: 20, color: Colors.white)),
),
),
SizedBox(width: 15),
],
),
]),
],
),
),
);
}
}
In a simple case, I would go with a stateful widget and array of letters. Of course, it could be created and sized dynamically, below I only explain the basic idea with some simplifications (no duplicate checks, no shuffling):
import 'package:flutter/material.dart';
void main() => runApp(App());
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App',
home: GuessTheWordWidget(),
);
}
}
class GuessTheWordWidget extends StatefulWidget {
#override
_GuessTheWordWidgetState createState() => _GuessTheWordWidgetState();
}
class _GuessTheWordWidgetState extends State<GuessTheWordWidget> {
String _word = 'Goldfish';
List<String> _input = List.generate(8, (_) => '');
int _position = 0;
void _press(int rune) {
setState(() {
if (_position < _input.length) {
print('Position ${_position}, rune: ${String.fromCharCode(rune)}');
_input[_position++] = String.fromCharCode(rune);
}
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('App'),
),
body: Center(
child: Column(children: <Widget>[
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: _input
.map((letter) => Container(
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(3.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent)),
child: Text(letter),
))
.toList())),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: _word.runes
.map((rune) => RaisedButton(
onPressed: () => _press(rune),
child: Text(String.fromCharCode(rune),
style: TextStyle(fontSize: 20)),
))
.toList())),
])),
);
}
}
Go play with this code at DartPad: https://dartpad.dev/69bae58772305c74f1688193076ecaef!

Acessing List items on another class

Hi I would like to know how can I access the items of a list on a different class where the list was created.Particularly i want to access items of the restaurantList on the Restaurant Screen class.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:projectfooddelivery/Src/models/restaurant.dart';
List<Restaurant> restaurantList=[
Restaurant(image:"kfcloja.jpg",distance: "2 miles away",location:"Shoprite do magoanine",restaurantName: "KFC"),
Restaurant(image:"dominos.jpg",distance: "0.9 miles away",location:"Downtown",restaurantName: "Dominos"),
Restaurant(image:"kfclogo.png",distance: "2 miles away",location:"Shoprite do magoanine",restaurantName: "KFC"),
];//I would like to access the items on the list
class RestaurantWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 350,
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: restaurantList.length,
itemBuilder: (_, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(
width: 1.0,
color: Colors.grey[100]
),
borderRadius: BorderRadius.circular(20)
),
child: Row(
children: <Widget>[
Container(
width: 150,
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.asset("images/${restaurantList[index].image}",fit:BoxFit.cover ,)
)
),
Padding(
padding: const EdgeInsets.all(3.0),
child: Container(
padding: EdgeInsets.all(2),
height: 70,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("${restaurantList[index].restaurantName}",style: TextStyle(fontWeight: FontWeight.bold),),
Row(
children: <Widget>[
Icon(Icons.star,size: 15,color:Colors.yellowAccent,),
Icon(Icons.star,size: 15,color:Colors.yellowAccent),
Icon(Icons.star,size: 15,color:Colors.yellowAccent),
Icon(Icons.star,size: 15,color:Colors.yellowAccent),
Icon(Icons.star_half,size: 15,color:Colors.yellowAccent),
],
),
Text("${restaurantList[index].location}"),
Text("${restaurantList[index].distance}"),
],
),
),
),
],
),
),
);
},
),
);
}
}
In this class
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:projectfooddelivery/Src/widgets/menu_widget.dart';
import 'package:smooth_star_rating/smooth_star_rating.dart';
class RestaurantScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: 400,
child: Column(
children: <Widget>[
Stack(
children: <Widget>[
Image.asset(
"images/kfcloja.jpg",
fit: BoxFit.cover,
),
Positioned(
child: IconButton(
icon: Icon(Icons.keyboard_arrow_left,size: 40,color: Colors.white,),
onPressed: (){},
),
top: 15,
)
],
),
Padding(
padding: const EdgeInsets.only(top: 14, left: 14, right: 14),
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Restaurant KFC",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
Text(
"0.2 miles away",
style: TextStyle(
fontSize: 16,
),
),
],
),
Align(
alignment: Alignment.topLeft,
child: SmoothStarRating(
allowHalfRating: false,
starCount: 5,
size: 20.0,
filledIconData: Icons.blur_off,
halfFilledIconData: Icons.blur_on,
color: Colors.yellowAccent,
borderColor: Colors.yellowAccent,
spacing: 0.0),
),
Align(
alignment: Alignment.topLeft,
child: Text("200 Main St, New york"),
),
Padding(
padding: const EdgeInsets.only(
left: 30, right: 30, top: 20, bottom: 5),
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlatButton.icon(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(10.0),
),
color: Theme.of(context).primaryColor,
icon: Icon(
Icons.rate_review,
color: Colors.white,
),
label: Text(
'Reviews',
style: TextStyle(color: Colors.white),
),
onPressed: () {},
),
FlatButton.icon(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(10.0),
),
color: Theme.of(context).primaryColor,
icon: Icon(
Icons.contact_phone,
color: Colors.white,
),
label: Text(
'Contact',
style: TextStyle(color: Colors.white),
),
onPressed: () {},
)
],
),
),
),
Text(
"Menu",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20),
),
MenuWidget(),
],
)
),
)
],
),
),
);
}
}
You can send and receive the data from one screen to another as follow , you need to use the Navigator for it , In below example i am sending the data from class A to class B as follow
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => B(bean: restaurantList[index])), //// HERE B IS THE CLASS ON WHICH YOU NEED TO CARRY DATA FROM CLASS A
);
And inside the B class you need to create the contrsutor to receive the data from A like below
class B extends StatefulWidget {
Restaurant bean;
B ({Key key, #required this.bean}) : super(key: key); ////YOU WILL GET THE DATA HERE FROM THE CONSTRUCTOR , AND USE IT INSIDE THE CLASS LIKE "widget.bean"
#override
State<StatefulWidget> createState() {
// TODO: implement createState
return _B();
}
}
And please check the example of it to pass data from one class to another
A class which send data to B class
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'B.dart';
import 'Fields.dart';
class A extends StatefulWidget {
#override
State<StatefulWidget> createState() {
// TODO: implement createState
return _A();
}
}
class _A extends State<A> {
Widget build(BuildContext context) {
return MaterialApp(
title: 'Screen A',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.red,
accentColor: Color(0xFFFEF9EB),
),
home: Scaffold(
appBar: new AppBar(),
body: Container(
margin: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: Text("Screen A"),
),
Expanded(
child: ListView.builder(
itemCount: fields.length,
itemBuilder: (BuildContext ctxt, int index) {
return ListTile(
title: new Text("Rating #${fields[index].rating}"),
subtitle: new Text(fields[index].title),
onTap: (){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => B(bean: fields [index])), //// HERE B IS THE CLASS ON WHICH YOU NEED TO CARRY DATA FROM CLASS A
);
},
);
}),
)
],
),
)));
}
}
List<Fields> fields = [
new Fields(
'One',
1,
),
new Fields(
'Two',
2,
),
new Fields(
'Three',
3,
),
new Fields(
'Four',
4,
),
new Fields(
'Five',
5,
),
];
Now check B class which receive the data from A class
import 'package:flutter/material.dart';
import 'Fields.dart';
class B extends StatefulWidget{
Fields bean;
B ({Key key, #required this.bean}) : super(key: key); ////YOU WILL GET THE DATA HERE FROM THE CONSTRUCTOR , AND USE IT INSIDE THE CLASS LIKE "widget.bean"
#override
State<StatefulWidget> createState() {
// TODO: implement createState
return _B ();
}
}
class _B extends State<B> {
#override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
title: 'Screen A',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.red,
accentColor: Color(0xFFFEF9EB),
),
home: Scaffold(
appBar: new AppBar(),
body: Container(
margin: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text("Screen B" ,style: TextStyle(fontSize: 20.0),),
)
),
Text("Rating=>>> ${widget.bean.rating} and Title ${widget.bean.title} ")
],
),
)));
}
}
And I have used the Pojo class for the list ,please check it once
Fields.dart
class Fields {
final String title;
final int rating;
Fields(this.title, this.rating);
}
And the output of the above program as follow.

Flutter Widget: set width to leftover width without using absolute number

I have the following Flutter Widget. On the right side to "+", we still see a lot of free gray space. How can I set the width of the white box to use all the leftover space, without using the absolute number?
import 'package:flutter/material.dart';
class QtyWidget extends StatefulWidget {
String title;
QtyWidget({this.title});
#override
_QtyWidgetState createState() => new _QtyWidgetState();
}
class _QtyWidgetState extends State<QtyWidget> {
int _itemCount = 1;
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
color: Colors.grey[200],
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
width: 100,
padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
child: Text(
'Qty',
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16,
color: Colors.grey[600]),
),
),
Container(
padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.amber[800],
),
borderRadius: BorderRadius.all(Radius.circular(10))
),
child: Row(
children: <Widget>[
IconButton(icon: new Icon(Icons.remove),
onPressed: () {
setState(() {
_itemCount = _itemCount - 1 ;
if (_itemCount < 0) {
_itemCount = 1;
}
});
}),
Text(_itemCount.toString()),
IconButton(icon: new Icon(Icons.add),
onPressed: ()=>setState(()=>_itemCount++))
],
),
),
]),
);
}
}
You can copy paste run full code below
You can use wrap Container with Expanded and Row use MainAxisAlignment.center and CrossAxisAlignment.center
code snippet
Expanded(
child: Container(
padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.amber[800],
),
borderRadius: BorderRadius.all(Radius.circular(10))),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
working demo
full code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class QtyWidget extends StatefulWidget {
String title;
QtyWidget({this.title});
#override
_QtyWidgetState createState() => new _QtyWidgetState();
}
class _QtyWidgetState extends State<QtyWidget> {
int _itemCount = 1;
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
color: Colors.grey[200],
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
width: 100,
padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
child: Text(
'Qty',
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16,
color: Colors.grey[600]),
),
),
Expanded(
child: Container(
padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.amber[800],
),
borderRadius: BorderRadius.all(Radius.circular(10))),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
IconButton(
icon: new Icon(Icons.remove),
onPressed: () {
setState(() {
_itemCount = _itemCount - 1;
if (_itemCount < 0) {
_itemCount = 1;
}
});
}),
Text(_itemCount.toString()),
IconButton(
icon: new Icon(Icons.add),
onPressed: () => setState(() => _itemCount++))
],
),
),
),
]),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
QtyWidget(),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

'constraints.hasBoundedWidth': is not true

Following is my code
main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "Student Info",
debugShowCheckedModeBanner: false,
home: SafeArea(
child: HomePage(),
),
);
}
}
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: ListView(
children: <Widget>[StudentScreenAppBar(), StudentGraduateList()],
),
);
}
}
class StudentScreenAppBar extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
AppBar(
leading: IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.black,
size: 20.0,
),
onPressed: () {}),
centerTitle: true,
title: Text(
"Student Info",
style: TextStyle(color: Colors.black),
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.settings,
size: 20.0,
color: Colors.black,
),
onPressed: () {}),
// SizedBox(width: 10.0,),
IconButton(
icon: Icon(
Icons.settings_ethernet,
color: Colors.black,
size: 20.0,
),
onPressed: () {})
],
elevation: 0.0,
backgroundColor: Colors.white,
),
Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black)),
padding: const EdgeInsets.fromLTRB(8.0, 2.0, 8.0, 2.0),
child: Image.asset(
"images/isdi_school.png",
width: 40.0,
height: 15.0,
fit: BoxFit.contain,
))
],
);
}
}
class StudentGraduateList extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
yearListForUgPg("Under Graduate"),
yearListForUgPg("Post Graduate")
],
),
);
}
Widget yearListForUgPg(String graduationName) {
return Column(
children: <Widget>[
Container(
padding: const EdgeInsets.fromLTRB(25.0, 4.0, 25.0, 4.0),
child: Text(
graduationName,
style:
TextStyle(color: Colors.white, fontFamily: "suisseintlMedium"),
),
decoration: BoxDecoration(color: Colors.black),
),
ListView.builder(
shrinkWrap: true,
itemBuilder: (context, index) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"2018",
style: TextStyle(
fontFamily: "okomitoBold", color: Colors.black),
),
Icon(Icons.arrow_forward)
],
);
},
itemCount: 10,
)
],
);
}
}
The ListView Widget does not get displayed. If I comment the ListView then the code works fine and I am able to see the UI. I tried wrapping the ListView in Expanded and Flexible Widget but it does not work.
If I uncomment the ListView then I get error saying 'constraints.hasBoundedWidth': is not true. I am trying to achieve something like this in my UI:
![Image][1]
Simply wrap your -Column in Expanded Widget: that way your Column will try to occupy available space in the parent Row.
Updated code:
class StudentGraduateList extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(child: yearListForUgPg("Under Graduate")),
Expanded(child: yearListForUgPg("Post Graduate"))
],
),
);
}