Display list of datas from sharedprefrence to text widget flutter? - flutter

I am saving some list of data to Sharedprefrence and tried to call the data from saved sharedprefrence and it returs all the values i have saved,then i tried to show data from sharedprefrence to a text widget but it shows null,
i need something like,if i have t text widgets how do pass data to those two widget let's say ₹575 and TWA Cap
Retrieving the data from sharedprfrence
List<String> listdata=[];
void initState() {
super.initState();
SharedPrefrence().getCartItem().then((data) async{
listdata = data;
print(listdata);
});
}
this what i am geting from the sharedprefrence
[TWA Cap, ₹575, M, Red]
trying to shows te data to text widget (Whole widget)
Widget CoupensLists() {
return SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
child: Container(
child: ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 1,
itemBuilder: (BuildContext context, int index) {
return Row(
children: <Widget>[
Expanded(
child: Card(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: GestureDetector(
onTap: () {
},
child: Container(
height: 150,
width: 350,
child: Row(
children: <Widget>[
Column(
children: <Widget>[
Padding(
padding:
const EdgeInsets.symmetric(vertical: 5),
child: Container(
height: 50,
width: 80,
child: Image.network("image"),
/* decoration: BoxDecoration(
image: DecorationImage(
image: Image.,
fit: BoxFit.fill,
),
),*/
),
),
SizedBox(
height: 5,
),
Text(
"Prodcut name",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold),
),
Text("Prodcut name",
style: TextStyle(fontSize: 12)),
SizedBox(
height: 10,
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(
vertical: 4),
child: Container(
width: 80,
child: Stack(
children: <Widget>[
SvgPicture.asset(
'assets/images/bg_price_btn_black.svg',
),
Padding(
padding: const EdgeInsets.all(5),
child: Text(
"Price"
style: TextStyle(
color: Colors.white,
fontSize: 12),
),
)
],
),
),
),
Stack(
children: <Widget>[
SvgPicture.asset(
'assets/images/bg_boon_btn_red.svg',
),
Padding(
padding: const EdgeInsets.all(5),
child: Text(
"Book Now",
style: TextStyle(
color: Colors.white,
fontSize: 12),
),
)
],
),
],
),
],
),
],
),
),
),
),
),
],
);
},
),
),
);
}

Try this:
List<String> listdata=[];
void initState() {
super.initState();
SharedPrefrence().getCartItem().then((data) async{
setState(() {
listdata = data;
});
});
}
You're getting this because you didn't upated your screen after changing the listdata variable

use in initail function
setState(() {
listdata = data;
});
then
Widget CoupensLists() {
return SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
child: Container(
child: ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 1,
itemBuilder: (BuildContext context, int index) {
return Row(
children: <Widget>[
Expanded(
child: Card(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: GestureDetector(
onTap: () {
},
child: Container(
height: 150,
width: 350,
child: Row(
children: <Widget>[
Column(
children: <Widget>[
Padding(
padding:
const EdgeInsets.symmetric(vertical: 5),
child: Container(
height: 50,
width: 80,
child: Image.network("image"),
/* decoration: BoxDecoration(
image: DecorationImage(
image: Image.,
fit: BoxFit.fill,
),
),*/
),
),
SizedBox(
height: 5,
),
Text(
listdata.length!=0? listdata[0]:"",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold),
),
Text( listdata.length!=0? listdata[1]:"",
style: TextStyle(fontSize: 12)),
SizedBox(
height: 10,
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(
vertical: 4),
child: Container(
width: 80,
child: Stack(
children: <Widget>[
SvgPicture.asset(
'assets/images/bg_price_btn_black.svg',
),
Padding(
padding: const EdgeInsets.all(5),
child: Text(
listdata.length!=0? listdata[2]:"",
style: TextStyle(
color: Colors.white,
fontSize: 12),
),
)
],
),
),
),
Stack(
children: <Widget>[
SvgPicture.asset(
'assets/images/bg_boon_btn_red.svg',
),
Padding(
padding: const EdgeInsets.all(5),
child: Text(
listdata.length!=0? listdata[3]:"",
style: TextStyle(
color: Colors.white,
fontSize: 12),
),
)
],
),
],
),
],
),
],
),
),
),
),
),
],
);
},
),``
),
);
}

Related

How to set number of elements to show in a CarouselSlider per page, using carousel_slider in flutter

I'm creating a vertical carousel slider using carousel_slider but there's a huge empty space between elements and I can't display the previous element on the screen. What is the best way to remove the empty space and also show the previous element.
My screen is set as follows
Column(
children: [
Container(
margin: const EdgeInsets.fromLTRB(16, 16, 16, 16),
child: TextField(
controller: controller,
),
),
//Carousel built here
Expanded(
child: CarouselSlider.builder(
itemCount: cars.length,
itemBuilder: (context, index, pageViewIndex) => CarTile(
car: cars[index],
onpress: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
CarDetailsScreen(car: cars[index]))),
),
options: CarouselOptions(
scrollDirection: Axis.vertical,
enlargeCenterPage: true,
),
),
),
],
);
CarTile code is as follows:
class CarTile extends StatelessWidget {
final Car car;
final VoidCallback onpress;
const CarTile({super.key, required this.onpress, required this.car});
#override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return GestureDetector(
onTap: onpress,
child: Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: Column(
children: [
Image.network(
height: 150,
width: size.width,
fit: BoxFit.fitWidth,
car.image,
),
const SizedBox(
height: 5,
),
Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
child: Row(
children: [
Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(5)),
child: Text(
car.features[0],
style: const TextStyle(fontWeight: FontWeight.w500),
),
),
const SizedBox(
width: 5,
),
Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(5)),
child: Text(
car.features[1],
style: const TextStyle(fontWeight: FontWeight.w500),
),
),
],
),
),
Row(
children: [
Text(
'${car.brand} ${car.model}',
style: const TextStyle(fontWeight: FontWeight.w900),
),
const Spacer(),
Text(
'Tsh. ${NumberFormat.decimalPattern().format(car.price)} / hr',
style: const TextStyle(fontWeight: FontWeight.w900),
),
],
),
Align(alignment: Alignment.centerLeft, child: Text('${car.year}'))
],
),
),
);
}
}
And this is my current results

How can i make a horizontal scrolling list View builder with dynamic height?

//ListView Builder
Container(
height: 230,
margin: const EdgeInsets.only(bottom: 30),
child: ListView.separated(
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: controller.homeData!.bestSaleWebinars.length,
itemBuilder: (context, index) => InkWell(
onTap: () {
Get.toNamed(RoutesName.allCoursesPageDetails);
allCoursesController.getAllCourseDetails(
controller.homeData!.bestSaleWebinars[index].slug);
},
child: newCoursesBuilder(
controller.homeData!.bestSaleWebinars[index])),
separatorBuilder: (context, _) => const SizedBox(width: 15),
),
//widget
Widget newCoursesBuilder(var coursesCategories) {
return SizedBox(
height: 350,
width: 200,
child: Padding(
padding: const EdgeInsets.only(left: 4.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 100,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey)),
child: CachedNetworkImage(
imageUrl: "https://livedivine.in${coursesCategories.thumbnail}",
placeholder: (context, url) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
Images.logo,
),
fit: BoxFit.fill),
),
),
errorWidget: (context, url, error) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
Images.logo,
),
fit: BoxFit.fill),
),
),
),
),
Container(
padding: const EdgeInsets.only(left: 11, top: 8),
child: Text(
coursesCategories.title,
style: TextStyle(
color: ColorConst.buttonColor,
fontSize: 15,
fontWeight: FontWeight.w900),
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
const Icon(
Icons.person,
size: 16,
),
Padding(
padding: const EdgeInsets.only(left: 4.0),
child: Text(
coursesCategories.teacher.fullName,
style: const TextStyle(
color: Colors.black,
fontSize: 12,
fontWeight: FontWeight.w500),
),
)
],
),
Row(
children: [
const Icon(
Icons.calendar_month,
size: 16,
),
Padding(
padding: const EdgeInsets.only(left: 4.0),
child: Text(
"${coursesCategories.duration}",
style: const TextStyle(
color: Colors.black,
fontSize: 12,
fontWeight: FontWeight.w500),
),
)
],
)
],
),
),
const SizedBox(
height: 2,
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
coursesCategories.price.toString(),
style: TextStyle(
color: ColorConst.buttonColor,
fontSize: 20,
fontWeight: FontWeight.w800),
),
Text(
"${coursesCategories.status}"
.replaceFirst("Status.", "")
.toLowerCase(),
style: TextStyle(
color: ColorConst.buttonColor,
fontSize: 19,
fontWeight: FontWeight.w800),
)
],
),
)
],
),
),
);
}
You can wrap the List view builder in an IntrinsicHeight widget, so the height of the ListView builder will be dynamic
You can try using SingleChildScrollView
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: List.generate(
44,
(index) => Padding(
padding: EdgeInsets.only(right: 10),
child: Container(
...
),
),
)),
)

How can i expand my container as per my text length?

I have created one widget called slidercarasol in that i had done following :
Widget slidercarasol = FutureBuilder(
future: GetAlerts(device_id),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return SpinKitChasingDots(
color: Colors.white,
size: 50.0,
);
} else if (snapshot.data.length == 0) {
return Container(
height: 100,
child: Center(
child: Text(
'NO ALERTS AVAILABLE NOW',
style: TextStyle(color: Colors.white, fontSize: 18),
)),
);
} else {
return new CarouselSlider(
aspectRatio: 16 / 5,
viewportFraction: 1.0,
autoPlayInterval: Duration(seconds: 20),
onPageChanged: (index) {
setState(() {
_current = index;
});
},
autoPlay: true,
pauseAutoPlayOnTouch: Duration(seconds: 10),
items: <Widget>[
for (var ind = 0; ind < snapshot.data.length; ind++)
GestureDetector(
child: Container(
child: Text(snapshot.data[ind].que,
softWrap: true,
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
),
),
onTap: () {},
),
],
);
}
});
and i had called that widget in scaffold
body: Container(
color: Colors.black,
height: MediaQuery.of(context).size.height,
child: Container(
height: double.infinity,
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.vertical,
children: <Widget>[
Card(
color: Colors.black,
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
child: Stack(
alignment: Alignment.topLeft,
children: <Widget>[
Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/slideralert.png",),
fit: BoxFit.fill,
),
borderRadius: new BorderRadius.all(const Radius.circular(10.0)),
),
// color: Colors.black.withOpacity(0.5),
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.notifications,
color: Colors.white,
),
SizedBox(
height: 10,
),
slidercarasol
],
),
)
],
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
elevation: 5,
margin: EdgeInsets.only(right: 24.0, left: 24.0, top: 10.0),
),
otherwidget,
],
),
),
),
and i get the output like this,
i need the full text to be shown and the height of the container will be increased as per text is large in length.
i want to display full text in container if the text is short than no issue but when i get long text i am unable to display full text..
thanks in advance!
Try wrapping your slidercarasol with Flexible.
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.notifications,
color: Colors.white,
),
SizedBox(
height: 10,
),
Flexible(
fit: FlexFit.loose,
child: slidercarasol,
),
],
),

flutter sliver list tabbar bottom overflowed error

I am a newbie of flutter.
I have just started to rebuild my android app built by Kotlin to flutter.
However, I am stuck in building the user page.
I intended to build the page like this screenshot
However, I continuously get bottom overflowed error.
How can I solve this problem?
I am looking forward to your help.
I will attach the whole code of the page.
import 'package:botanic_flutter/login_page.dart';
import 'package:botanic_flutter/main.dart';
import 'package:botanic_flutter/root_page.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:botanic_flutter/custom_color_scheme.dart';
import 'package:google_sign_in/google_sign_in.dart';
class AccountPage extends StatefulWidget {
final String userUid;
AccountPage(this.userUid);
#override
_AccountPageState createState() => _AccountPageState();
}
class _AccountPageState extends State<AccountPage> with SingleTickerProviderStateMixin {
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _Auth = FirebaseAuth.instance;
FirebaseUser _currentUser;
TabController _controller;
#override
void initState() {
super.initState();
_controller = new TabController(length: 2, vsync: this);
}
#override
void dispose() {
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
length: 2,
child: NestedScrollView(
headerSliverBuilder: (context, isScrolled) {
return <Widget> [
SliverList(
// itemExtent: 300,
delegate: SliverChildListDelegate([
Container(
child: Column(
children: <Widget>[
_detailedBody()
],
),
)
]),
),
];
},
body: Column(
children: <Widget>[
TabBar(
unselectedLabelColor: Theme.of(context).colorScheme.greyColor,
labelColor: Theme.of(context).colorScheme.mainColor,
controller: _controller,
indicatorColor: Theme.of(context).colorScheme.mainColor,
tabs: [
Container(
height: 90,
padding: EdgeInsets.only(top: 20),
child: Tab(
icon: const Icon(Icons.home,
),
text: 'PLANTS',
),
),
Container(
height: 90,
padding: EdgeInsets.only(top: 20),
child: Tab(
icon: const Icon(Icons.bookmark_border,
),
text: 'BOOKMARK',
),
)
],
),
Expanded(
flex: 1,
child: TabBarView(
controller: _controller,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
child: Column(
children: <Widget>[
Container(
child: Row(
children: <Widget>[
Padding(
padding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
child: Icon(
Icons.clear_all,
color: Colors.grey,
),
),
Container(
width: MediaQuery.of(context).size.width/3*1.4,
child: DropdownButton<String>(
isExpanded: true,
items: <String>['Foo', 'Bar'].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (_) {},
),
),
Padding(
padding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 5.0),
child: Icon(
Icons.mode_edit,
color: Colors.grey,
),
),
Padding(
padding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 1.0),
child: Icon(
Icons.delete,
color: Colors.grey,
),
),
Padding(
padding: EdgeInsets.all(4.0),
),
Container(
height: 30,
width: MediaQuery.of(context).size.width/4.5,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.mainColor,
borderRadius: BorderRadius.all(Radius.circular(20)),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey.shade200,
offset: Offset(2, 4),
blurRadius: 5,
spreadRadius: 2)
],
),
child: FlatButton(
child: Text(
"+식물등록",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 13,
color: Colors.white,
),
),
onPressed: () => {
},
),
),
],
),
),
Expanded(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1.0,
mainAxisSpacing: 1.0,
crossAxisSpacing: 1.0),
itemCount: notes.length,
itemBuilder: (context, index) => Card(
child: Image.network(notes[index],
fit: BoxFit.cover,
),
),
),
),
],
),
),
Container(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1.0,
mainAxisSpacing: 1.0,
crossAxisSpacing: 1.0),
itemCount: notes.length,
itemBuilder: (context, index) => Card(
child: Image.network(notes[index],
fit: BoxFit.cover,
),
),
),
)
],
),
),
],
),
),
),
);
}
Widget _detailedBody() {
return SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width * 3 / 4,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
'http://www.korea.kr/newsWeb/resources/attaches/2017.08/03/3737_cp.jpg'),
),
),
),
Container(
transform: Matrix4.translationValues(0.0, -41.0, 0.0),
child: Column(
children: <Widget>[
Stack(
alignment: Alignment.center,
children: <Widget>[
SizedBox(
width: 90.0,
height: 90.0,
child: CircleAvatar(
backgroundColor: Colors.white,
),
),
SizedBox(
width: 82.0,
height: 82.0,
child: CircleAvatar(
backgroundImage: NetworkImage('https://steemitimages.com/DQmS1gGYmG3vL6PKh46A2r6MHxieVETW7kQ9QLo7tdV5FV2/IMG_1426.JPG')
),
),
Container(
width: 90.0,
height: 90.0,
alignment: Alignment.bottomRight,
child: Stack(
alignment: Alignment.center,
children: <Widget>[
SizedBox(
width: 28.0,
height: 28.0,
child: FloatingActionButton(
onPressed: null,
backgroundColor: Colors.white,
//child: Icon(Icons.add),
),
),
SizedBox(
width: 25.0,
height: 25.0,
child: FloatingActionButton(
onPressed: null,
backgroundColor: Theme.of(context).colorScheme.mainColor,
child: Icon(Icons.add),
),
),
],
)
)
],
),
Padding(padding: EdgeInsets.all(5.0)),
Text(
'nickname',
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 24.0),
),
Padding(padding: EdgeInsets.all(5.0)),
Text(
'introduce',
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0),
),
Padding(padding: EdgeInsets.all(9.0)),
FlatButton(
onPressed: () {
signOutWithGoogle().then((_) {
Navigator.popUntil(context, ModalRoute.withName('/'));
});
},
color: Theme.of(context).colorScheme.mainColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0)),
child: Text('로그아웃'),
),
Padding(padding: EdgeInsets.all(9.0)),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Column(
children: <Widget>[
Text(
'0',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16.0),
),
Text(
'식물수',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20.0),
),
],
),
Column(
children: <Widget>[
Text(
'0',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16.0),
),
Text(
'팔로워',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20.0),
),
],
),
Column(
children: <Widget>[
Text(
'0',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16.0),
),
Text(
'팔로잉',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20.0),
),
],
)
],
)
],
),
),
],
),
);
}
List<String> notes = [
'https://steemitimages.com/DQmS1gGYmG3vL6PKh46A2r6MHxieVETW7kQ9QLo7tdV5FV2/IMG_1426.JPG',
'https://lh3.googleusercontent.com/proxy/BKvyuWq6b5apNOqvSw3VxB-QhezYHAoX1AptJdWPl-Ktq-Efm2gotbeXFtFlkr_ZPZmpEHc2BsKTC9oFQgzBimKsf5oRtTqOGdlO3MTfwiOT54E5m-lCtt6ANOMzmhNsYMGRp9Pg1NzjwMRUWNoWX0oJEFcnFvjOj2Rr4LtZpkXyiQFO',
'https://img1.daumcdn.net/thumb/R720x0.q80/?scode=mtistory2&fname=http%3A%2F%2Fcfile28.uf.tistory.com%2Fimage%2F2343174F58DBC14C2ECB8B',
];
Future<void> signOutWithGoogle() async {
// Sign out with firebase
await _Auth.signOut();
// Sign out with google
await _googleSignIn.signOut();
}
}

Fit Container Widget Height to Parent Row Height

I have a web application with the following layout:
I am trying to create a flutter application with a similar layout and this is what I have so far:
For reproduction purposes, my layout logic is the following (all my code is in the main.dart file for this example):
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Assemblers Tasks',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.deepOrange,
),
home: MyHomePage(title: 'Assembly Tasks'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownValue;
List<Map<String, dynamic>> buildItems = [];
getBuilds() {
List<Map<String, dynamic>> items = [];
items.add({"id": 10, "vehicleModel": "A10", "vehicleNumber": "TEST-00010"});
setState(() {
buildItems = items;
});
}
List<Map<String, dynamic>> buildSections = [];
getSections() {
List<Map<String, dynamic>> items = [];
items.add({
"id": 5,
"section": "Front",
});
items.add({
"id": 15,
"section": "Rear",
});
setState(() {
buildSections = items;
});
}
Future<List<Map<String, dynamic>>> getSystems(String buildSectionId) async {
List<Map<String, dynamic>> items = [];
if (int.parse(buildSectionId) == 5) {
items.add({
"id": 4,
"system": "Hydraulics",
});
items.add({
"id": 20,
"system": "High Voltage",
});
}
return items;
}
#override
void initState() {
getBuilds();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Container(
width: 275.0,
child: DropdownButton(
value: dropdownValue,
hint: Text("Choose Build"),
isExpanded: true,
items: buildItems
.map<Map<String, String>>((Map<String, dynamic> item) {
String id = item["id"].toString();
String name = item["vehicleModel"] + " " + item["vehicleNumber"];
return {"id": id, "name": name};
})
.toList()
.map<DropdownMenuItem<String>>((Map<String, String> item) {
return DropdownMenuItem<String>(
value: item["id"],
child: Text(item["name"]),
);
})
.toList(),
onChanged: (String newValue) {
getSections();
setState(() {
dropdownValue = newValue;
});
}),
),
],
),
Row(
children: <Widget>[
Container(
width: 150.0,
height: 60.0,
color: Colors.black,
child: Align(
alignment: Alignment.center,
child: Text(
"SECTION",
style: TextStyle(
color: Colors.white,
fontSize: 17.3,
letterSpacing: 1.35,
),
),
),
),
Container(
width: 150.0,
height: 60.0,
color: Color(0xff444444),
child: Align(
alignment: Alignment.center,
child: Text(
"SYSTEM",
style: TextStyle(
color: Colors.white,
fontSize: 17.3,
letterSpacing: 1.35,
),
),
),
),
Expanded(
child: Container(
height: 60.0,
padding: EdgeInsets.only(left: 36.0),
margin: EdgeInsets.only(right: 72.0),
color: Color(0xff666666),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"TASK",
style: TextStyle(
color: Colors.white,
fontSize: 17.3,
letterSpacing: 1.35,
),
),
),
),
)
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black12, width: 1.0, style: BorderStyle.solid)),
height: MediaQuery.of(context).size.height - 225,
child: ListView.builder(
shrinkWrap: true,
itemCount: buildSections.length,
itemBuilder: (BuildContext context, int index) {
String section = buildSections[index]["section"] != null ? buildSections[index]["section"] : "";
String buildSectionId = buildSections[index]["id"].toString();
return Row(
children: <Widget>[
Container(
width: 150.0,
decoration: BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.black12,
width: 1.0,
),
bottom: BorderSide(
color: Colors.black12,
width: 1.0,
),
),
),
padding: EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Align(
alignment: Alignment.center,
child: Center(
child: RotatedBox(
quarterTurns: -1,
child: Text(
section.toUpperCase(),
style: TextStyle(
fontSize: 15.0,
letterSpacing: 1.2,
),
),
),
),
),
Padding(
padding: EdgeInsets.all(12.0),
),
Align(
alignment: Alignment.center,
child: FloatingActionButton(
child: Icon(Icons.image),
),
),
],
),
),
FutureBuilder(
future: getSystems(buildSectionId),
builder: (BuildContext context, AsyncSnapshot systemsSnap) {
if (systemsSnap.connectionState == ConnectionState.waiting) {
return Container(
height: 100.0,
width: 200.0,
child: Text("Please wait..."),
);
} else if (systemsSnap.hasError) {
return Container(
height: 100.0,
width: 200.0,
child: Text("Oops! There was an error!"),
);
}
return Row(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width - 256.0,
child: ListView.builder(
shrinkWrap: true,
itemCount: systemsSnap.data.length,
itemBuilder: (context, index) {
Map<String, dynamic> system = systemsSnap.data[index];
String systemName = system["system"];
return Row(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
width: 150.0,
decoration: BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.black12,
width: 1.0,
),
bottom: BorderSide(
color: Colors.black12,
width: 1.0,
),
),
),
child: Column(
children: <Widget>[
Align(
alignment: Alignment.center,
child: RotatedBox(
quarterTurns: -1,
child: Text(
systemName.toUpperCase(),
style: TextStyle(
fontSize: 15.0,
letterSpacing: 1.2,
),
),
),
),
Padding(
padding: EdgeInsets.all(12.0),
),
Align(
alignment: Alignment.center,
child: FloatingActionButton(
child: Icon(Icons.image),
),
),
],
),
),
],
);
},
),
),
],
);
},
),
],
);
},
),
),
),
Container(
padding: EdgeInsets.fromLTRB(16.0, 16.0, 0.0, 0.0),
child: Column(
children: <Widget>[
Container(
child: FloatingActionButton(
child: Icon(Icons.photo_library),
onPressed: () {
//TODO action
},
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: FloatingActionButton(
child: Icon(Icons.library_books),
onPressed: () {
//TODO action
},
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: FloatingActionButton(
child: Icon(Icons.list),
onPressed: () {
//TODO action
},
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: FloatingActionButton(
child: Icon(Icons.history),
onPressed: () {
//TODO action
},
),
),
],
),
),
],
),
],
),
),
);
}
}
The above code is ready to be pasted into the main.dart file and preview on a the virtual device (preferably tablet).
So far I have tried the solutions I found on these posts to no avail:
- Make container widget fill parent vertically
- Flutter Container height same as parent height
- Flutter expand Container to fill remaining space of Row
- The equivalent of wrap_content and match_parent in flutter?
Since the Row that contains the section Container also has a ListView being generated with the FutureBuilder, the height of the Row automatically expands to fit the ListView. I also want the section Container to expand to the same height as the how the Row widget is expanding; i.e., The bottom border of the section Container that says FRONT, should be aligned with the bottom border of the Hight Voltage system and the right border of the FRONT section Container, should go all the way to the top.
I already spent 3 days without a resolution.
Edit
I have tried the suggestion on the answer provided by #MaadhavSharma but I get the following exception:
════════ Exception Caught By rendering library ════════════════════════════
The following assertion was thrown during performLayout():
BoxConstraints forces an infinite height.
I changed a little the structure to make it work, here is the entire build() method:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Container(
width: 275.0,
child: DropdownButton(
value: dropdownValue,
hint: Text("Choose Build"),
isExpanded: true,
items: buildItems
.map<Map<String, String>>((Map<String, dynamic> item) {
String id = item["id"].toString();
String name = item["vehicleModel"] + " " + item["vehicleNumber"];
return {"id": id, "name": name};
})
.toList()
.map<DropdownMenuItem<String>>((Map<String, String> item) {
return DropdownMenuItem<String>(
value: item["id"],
child: Text(item["name"]),
);
})
.toList(),
onChanged: (String newValue) {
getSections();
setState(() {
dropdownValue = newValue;
});
}),
),
],
),
Row(
children: <Widget>[
Container(
width: 150.0,
height: 60.0,
color: Colors.black,
child: Align(
alignment: Alignment.center,
child: Text(
"SECTION",
style: TextStyle(
color: Colors.white,
fontSize: 17.3,
letterSpacing: 1.35,
),
),
),
),
Container(
width: 150.0,
height: 60.0,
color: Color(0xff444444),
child: Align(
alignment: Alignment.center,
child: Text(
"SYSTEM",
style: TextStyle(
color: Colors.white,
fontSize: 17.3,
letterSpacing: 1.35,
),
),
),
),
Expanded(
child: Container(
height: 60.0,
padding: EdgeInsets.only(left: 36.0),
margin: EdgeInsets.only(right: 72.0),
color: Color(0xff666666),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"TASK",
style: TextStyle(
color: Colors.white,
fontSize: 17.3,
letterSpacing: 1.35,
),
),
),
),
)
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black12, width: 1.0, style: BorderStyle.solid)),
height: MediaQuery.of(context).size.height - 225,
child: ListView.builder(
shrinkWrap: true,
itemCount: buildSections.length,
itemBuilder: (BuildContext context, int index) {
String section = buildSections[index]["section"] != null ? buildSections[index]["section"] : "";
String buildSectionId = buildSections[index]["id"].toString();
return IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
width: 150.0,
decoration: BoxDecoration(
color: Colors.green,
border: Border(
right: BorderSide(
color: Colors.black12,
width: 1.0,
),
bottom: BorderSide(
color: Colors.black12,
width: 1.0,
),
),
),
padding: EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Align(
alignment: Alignment.center,
child: Center(
child: RotatedBox(
quarterTurns: -1,
child: Text(
section.toUpperCase(),
style: TextStyle(
fontSize: 15.0,
letterSpacing: 1.2,
),
),
),
),
),
Padding(
padding: EdgeInsets.all(12.0),
),
Align(
alignment: Alignment.center,
child: FloatingActionButton(
child: Icon(Icons.image),
),
),
],
),
),
FutureBuilder(
future: getSystems(buildSectionId),
builder: (BuildContext context, AsyncSnapshot systemsSnap) {
if (systemsSnap.connectionState == ConnectionState.waiting) {
return Container(
height: 100.0,
width: 200.0,
child: Text("Please wait..."),
);
} else if (systemsSnap.hasError) {
return Container(
height: 100.0,
width: 200.0,
child: Text("Oops! There was an error!"),
);
}
return
Container(
width: MediaQuery.of(context).size.width - 256.0,
child: Column(
children: systemsSnap.data.map<Widget>((e) => Container(
padding: EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
width: 150.0,
decoration: BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.black12,
width: 1.0,
),
bottom: BorderSide(
color: Colors.black12,
width: 1.0,
),
),
),
child: Column(
children: <Widget>[
Align(
alignment: Alignment.center,
child: RotatedBox(
quarterTurns: -1,
child: Text(
e["system"].toUpperCase(),
style: TextStyle(
fontSize: 15.0,
letterSpacing: 1.2,
),
),
),
),
Padding(
padding: EdgeInsets.all(12.0),
),
Align(
alignment: Alignment.center,
child: FloatingActionButton(
child: Icon(Icons.image),
),
),
],
),
)).toList(),
),
);
// Row(
// children: <Widget>[
// Container(
// width: MediaQuery.of(context).size.width - 256.0,
// child: ListView.builder(
// shrinkWrap: true,
// itemCount: systemsSnap.data.length,
// itemBuilder: (context, index) {
// Map<String, dynamic> system = systemsSnap.data[index];
// String systemName = system["system"];
// return Row(
// children: <Widget>[
//
// ],
// );
// },
// ),
// ),
// ],
// );
},
),
],
),
);
},
),
),
),
Container(
padding: EdgeInsets.fromLTRB(16.0, 16.0, 0.0, 0.0),
child: Column(
children: <Widget>[
Container(
child: FloatingActionButton(
child: Icon(Icons.photo_library),
onPressed: () {
//TODO action
},
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: FloatingActionButton(
child: Icon(Icons.library_books),
onPressed: () {
//TODO action
},
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: FloatingActionButton(
child: Icon(Icons.list),
onPressed: () {
//TODO action
},
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: FloatingActionButton(
child: Icon(Icons.history),
onPressed: () {
//TODO action
},
),
),
],
),
),
],
),
],
),
),
);
}
Basically I changed the ListView of the second element of the Row for a Column, since it already was inside a ListView and that was making it have double scroll and the heights weren't expanding the height of the row properly, and also I wrapped the Row inside an IntrinsicHeight and put crossAxisAlignment: CrossAxisAlignment.stretch to the Row so the content will fit the height of the Row.
The use of IntrinsicHeight is expensive, but I don't see another solution for the way you structured the widgets. I recommend you to try to optimize all the structure so you could find a better and optimal solution.
If you want to stretch the Container to match its parent, use double.infinity for the height and width properties
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Container as a layout')),
body: Container(
height: double.infinity,
width: double.infinity,
color: Colors.yellowAccent,
child: Text("Hi"),
),
);
}
The trick is to use constraints: BoxConstraints.expand() in some of your containers that you want to expand to fill the row height.
Try something like this:
Scaffold(
appBar: AppBar(
title: Text("Title"),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Expanded(
flex: 2,
child: Container(
padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
color: Colors.yellow,
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Container(
constraints: BoxConstraints.expand(),
color: Colors.blue,
child: Text("box 1")),
),
Expanded(
flex: 2,
child: Container(
constraints: BoxConstraints.expand(),
color: Colors.red,
child: Column(
children: <Widget>[
Expanded(
flex: 2,
child: Container(
padding:
EdgeInsets.fromLTRB(0, 0, 0, 0),
color: Colors.yellow,
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Container(
constraints:
BoxConstraints.expand(),
color: Colors.lightGreen,
child: Text("box 2")),
),
Expanded(
flex: 2,
child: Container(
constraints:
BoxConstraints.expand(),
color: Colors.lightBlue,
child: Text("box 3")),
),
],
),
),
),
Expanded(
flex: 1,
child: Container(
padding:
EdgeInsets.fromLTRB(0, 0, 0, 0),
color: Colors.yellow,
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Container(
constraints:
BoxConstraints.expand(),
color: Colors.purple,
child: Text("box 4")),
),
Expanded(
flex: 2,
child: Container(
constraints:
BoxConstraints.expand(),
color: Colors.orange,
child: Text("")),
),
],
),
),
),
],
)),
),
],
),
),
),
Expanded(
flex: 1,
child: Container(
padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
color: Colors.yellow,
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Container(
constraints: BoxConstraints.expand(),
color: Colors.yellow,
child: Text("box 5")),
),
Expanded(
flex: 2,
child: Container(
constraints: BoxConstraints.expand(),
color: Colors.green,
child: Text("box 6")),
),
],
),
),
),
],
)),
);
I think you will need to use some fixed heights since you are using ListView and FutureBuiulders. If you can get rid of FutureBuilders, you can probably achieve dynamic heights. Notice the hright of 320 on the parent row and height of 160 on child rows.
But have a look at this:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Assemblers Tasks',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.deepOrange,
),
home: MyHomePage(title: 'Assembly Tasks'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownValue;
List<Map<String, dynamic>> buildItems = [];
getBuilds() {
List<Map<String, dynamic>> items = [];
items.add({"id": 10, "vehicleModel": "A10", "vehicleNumber": "TEST-00010"});
setState(() {
buildItems = items;
});
}
List<Map<String, dynamic>> buildSections = [];
getSections() {
List<Map<String, dynamic>> items = [];
items.add({
"id": 5,
"section": "Front",
});
items.add({
"id": 15,
"section": "Rear",
});
setState(() {
buildSections = items;
});
}
Future<List<Map<String, dynamic>>> getSystems(String buildSectionId) async {
List<Map<String, dynamic>> items = [];
if (int.parse(buildSectionId) == 5) {
items.add({
"id": 4,
"system": "Hydraulics",
});
items.add({
"id": 20,
"system": "High Voltage",
});
}
return items;
}
#override
void initState() {
getBuilds();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Container(
width: 275.0,
child: DropdownButton(
value: dropdownValue,
hint: Text("Choose Build"),
isExpanded: true,
items: buildItems
.map<Map<String, String>>(
(Map<String, dynamic> item) {
String id = item["id"].toString();
String name = item["vehicleModel"] +
" " +
item["vehicleNumber"];
return {"id": id, "name": name};
})
.toList()
.map<DropdownMenuItem<String>>(
(Map<String, String> item) {
return DropdownMenuItem<String>(
value: item["id"],
child: Text(item["name"]),
);
})
.toList(),
onChanged: (String newValue) {
getSections();
setState(() {
dropdownValue = newValue;
});
}),
),
],
),
Row(
children: <Widget>[
Container(
width: 150.0,
height: 60.0,
color: Colors.black,
child: Align(
alignment: Alignment.center,
child: Text(
"SECTION",
style: TextStyle(
color: Colors.white,
fontSize: 17.3,
letterSpacing: 1.35,
),
),
),
),
Container(
width: 150.0,
height: 60.0,
color: Color(0xff444444),
child: Align(
alignment: Alignment.center,
child: Text(
"SYSTEM",
style: TextStyle(
color: Colors.white,
fontSize: 17.3,
letterSpacing: 1.35,
),
),
),
),
Expanded(
child: Container(
height: 60.0,
padding: EdgeInsets.only(left: 36.0),
margin: EdgeInsets.only(right: 72.0),
color: Color(0xff666666),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"TASK",
style: TextStyle(
color: Colors.white,
fontSize: 17.3,
letterSpacing: 1.35,
),
),
),
),
)
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black12,
width: 1.0,
style: BorderStyle.solid)),
height: MediaQuery.of(context).size.height - 225,
child: ListView.builder(
shrinkWrap: true,
itemCount: buildSections.length,
itemBuilder: (BuildContext context, int index) {
String section = buildSections[index]["section"] != null
? buildSections[index]["section"]
: "";
String buildSectionId =
buildSections[index]["id"].toString();
return Container(
height: 320,
child: Row(
children: <Widget>[
Container(
width: 120.0,
decoration: BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.yellow,
width: 1.0,
),
bottom: BorderSide(
color: Colors.black12,
width: 1.0,
),
),
),
padding:
EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Align(
alignment: Alignment.center,
child: Center(
child: RotatedBox(
quarterTurns: -1,
child: Text(
section.toUpperCase(),
style: TextStyle(
fontSize: 15.0,
letterSpacing: 1.2,
),
),
),
),
),
Padding(
padding: EdgeInsets.all(12.0),
),
Align(
alignment: Alignment.center,
child: FloatingActionButton(
child: Icon(Icons.image),
),
),
],
),
),
Expanded(
child: Container(
color: Colors.orange,
child: false ? Text(" ") : FutureBuilder(
future: getSystems(buildSectionId),
builder: (BuildContext context,
AsyncSnapshot systemsSnap) {
if (systemsSnap.connectionState ==
ConnectionState.waiting) {
return Container(
height: 100.0,
width: 200.0,
child: Text("Please wait..."),
);
} else if (systemsSnap.hasError) {
return Container(
height: 100.0,
width: 200.0,
child: Text("Oops! There was an error!"),
);
}
return Row(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width -
256.0,
child: ListView.builder(
shrinkWrap: true,
itemCount: systemsSnap.data.length,
itemBuilder: (context, index) {
Map<String, dynamic> system =
systemsSnap.data[index];
String systemName = system["system"];
return Row(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(
0.0, 16.0, 0.0, 16.0),
width: 50.0,
height: 160,
decoration: BoxDecoration(
color: Colors.yellow,
border: Border(
right: BorderSide(
color: Colors.red,
width: 1.0,
),
bottom: BorderSide(
color: Colors.black12,
width: 1.0,
),
),
),
child: Column(
children: <Widget>[
Align(
alignment:
Alignment.center,
child: RotatedBox(
quarterTurns: -1,
child: Text(
systemName
.toUpperCase(),
style: TextStyle(
fontSize: 15.0,
letterSpacing: 1.2,
),
),
),
),
],
),
),
],
);
},
),
),
],
);
},
),
),
),
],
),
);
},
),
),
),
Container(
padding: EdgeInsets.fromLTRB(16.0, 16.0, 0.0, 0.0),
child: Column(
children: <Widget>[
Container(
child: FloatingActionButton(
child: Icon(Icons.photo_library),
onPressed: () {
//TODO action
},
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: FloatingActionButton(
child: Icon(Icons.library_books),
onPressed: () {
//TODO action
},
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: FloatingActionButton(
child: Icon(Icons.list),
onPressed: () {
//TODO action
},
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: FloatingActionButton(
child: Icon(Icons.history),
onPressed: () {
//TODO action
},
),
),
],
),
),
],
),
],
),
),
);
}
}