how to adjust the container as per the device screen in flutter - flutter

my app look like below
once logged in, in home page it has 3 tabs, and also a bottom navigation bar, and a app bar.
below the tab bar there is a container it contain many cards. I have given a fixed height to container, but when I checked in multiple devices there is a issue , i.e for the container having cards, getting overflow.
I tried to take the entire screen height then took the 70% for the container , but in phones with smaller resolution its showing overflow, if I adjusted as per that screen , In bigger screen , container takes very less space and more than 20% of space is wasted.
I am adding my code below,
here is my dashboard.dart code: ===>
import './generate_report_list.dart';
import './provider_classes.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:bmnav/bmnav.dart' as bmnav;
import './dashboard_view.dart';
class DashBoard extends StatefulWidget {
#override
_DashBoardState createState() => _DashBoardState();
}
class _DashBoardState extends State<DashBoard> with WidgetsBindingObserver {
int _currentIndex = 0;
Widget _view;
double maxHeight;
double maxWidth;
double maxCardWidth;
List<String> headers = ['Dashboard', 'Reports List', 'Profile', 'Settings'];
List<IconData> icons = [
Icons.dashboard,
Icons.insert_chart,
Icons.account_circle,
Icons.settings,
];
List<String> images = [
('assets/dashboard.png'),
('assets/growth.png'),
('assets/user.png'),
('assets/settings.png'),
];
getView(int index) {
List<Widget> _viewList = <Widget>[
GenerateReportList(),
GenerateReportList(),
GenerateReportList(),
GenerateReportList(),
];
setState(() {
_view = _viewList[index];
_currentIndex = index;
Provider.of<ScreenHeader>(context)
.setScreenHeader(headers[_currentIndex]);
});
}
#override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
#override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
#override
Widget build(BuildContext context) {
String appBarHeader = Provider.of<ScreenHeader>(context).getScreenHeader();
maxWidth = MediaQuery.of(context).size.width;
maxCardWidth = maxWidth / 2;
return Scaffold(
appBar: AppBar(
title: Text(
(appBarHeader ?? 'Dashboard'),
),
centerTitle: true,
),
body: _view ?? DashBoardView(),
bottomNavigationBar: bmnav.BottomNav(
onTap: (index) {
getView(index);
},
labelStyle: bmnav.LabelStyle(visible: true),
iconStyle:
bmnav.IconStyle(color: Colors.black, onSelectColor: Colors.red),
elevation: 10,
items: [
bmnav.BottomNavItem(Icons.home, label: 'Dashboard'),
bmnav.BottomNavItem(Icons.trending_up, label: 'Reports'),
bmnav.BottomNavItem(Icons.person, label: 'Profile'),
bmnav.BottomNavItem(Icons.settings, label: 'Settings')
],
),
);
}
}
generate_report_list.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:developer' as developer;
class GenerateReportList extends StatefulWidget {
#override
_GenerateReportListState createState() => _GenerateReportListState();
}
class _GenerateReportListState extends State<GenerateReportList>
with SingleTickerProviderStateMixin {
Future reportList;
List<String> typesOfReports = [];
String currentReportSummaryType;
TabController _tabController;
#override
void initState() {
super.initState();
currentReportSummaryType = 'detailed';
_tabController = TabController(vsync: this, length: 3);
}
void _handleTabSelection(var index) {
developer
.log("index: array:" + typesOfReports[0] + "," + typesOfReports[1]);
setState(() {
print("index is " + index);
if (index == 0) {
currentReportSummaryType = "Group A";
} else if (index == 1) {
currentReportSummaryType = "Group B";
} else if (index == 2) {
currentReportSummaryType = "Group C";
}
});
}
getBody() {
double maxHeight = MediaQuery.of(context).size.height;
developer.log('Max height:' + maxHeight.toString());
return Scaffold(
resizeToAvoidBottomInset: false,
body: Column(
children: [
Container(
child: TabBar(
labelColor: Colors.black,
tabs: <Widget>[
new Tab(text: 'Group A', icon: new Icon(Icons.list)),
new Tab(text: 'Group B', icon: new Icon(Icons.pie_chart)),
new Tab(text: 'Group C', icon: new Icon(Icons.insert_chart)),
],
controller: _tabController,
onTap: _handleTabSelection,
),
),
SingleChildScrollView(
child: Container(
margin: new EdgeInsets.all(0.0),
height: (maxHeight * 0.60),
child: SingleChildScrollView(
child:new Center(
child: Column(
children: <Widget>[
cardGen(),
cardGen(),
cardGen(),
cardGen(),
cardGen(),
cardGen(),
],
)))))
],
));
}
cardGen() {
return Card(
child: Container(
height: (MediaQuery.of(context).size.height * 0.6) * 0.25,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text("sample"),
Text("dummy"),
],
),
],
),
),
);
}
createCard(BuildContext context) {
double maxHeight = MediaQuery.of(context).size.height;
Container(
height: (maxHeight * 0.6) * 0.25,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text("sample"),
Text("dummy"),
],
),
],
),
);
}
#override
Widget build(BuildContext context) {
return getBody();
}
}
provider_classes.dart
import 'package:flutter/material.dart';
class ScreenHeader with ChangeNotifier {
String _screenHeader;
getScreenHeader() => _screenHeader;
setScreenHeader(String newHeader) {
_screenHeader = newHeader;
notifyListeners();
}
}
provider classes is just for displaying the data in appbar.
In dashboard.dart I am creating a appbar,and bottomsheet, and in generate_report_list I am adding 3 new tabs , for each tab once clicked I am displaying the cards.
How to assign the height to container carrying card so that it should fit on every device.,and how can I add scrollable to the tabs , i.e group A, group B, Group C.
Thanks

Change:
Column(
children: <Widget>[
cardGen(),
...
To:
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
cardGen(),
...

Related

improper use of a GetX has been detected Flutter

the improper use of a GetX has been detected. This error is causing me not to load the widget at that point of time. Tried to figure out but still the problem remain. You should only use GetX or Obx for the specific widget. Unable to figure out get x issue that is causing the problem or OBX where to enclose it with. Please help with this scenario.
Thank you.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'specified_books.dart';
import '../pages/styles.dart';
class BookMagazineTapbar extends StatelessWidget {
final String titleText;
const BookMagazineTapbar({Key? key, required this.titleText})
: super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: whiteColor,
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(titleText.tr),
],
),
),
body: SpecifiedBooks(
titleText: titleText,
),
);
/*TODO: Below is code for magzines and books, comment scafold from above and uncomment below code to use both if need to use magzines */
// DefaultTabController(
// length: 2,
// child: Scaffold(
// appBar: AppBar(
// elevation: 0,
// backgroundColor: whiteColor,
// title: Row(
// mainAxisAlignment: MainAxisAlignment.center,
// mainAxisSize: MainAxisSize.min,
// children: <Widget>[
// Text(titleText.tr),
// ],
// ),
// bottom: TabBar(
// labelColor: mainColor,
// unselectedLabelColor: greyColor,
// tabs: [
// Tab(
// child: Row(
// mainAxisSize: MainAxisSize.min,
// children: [
// const ImageIcon(AssetImage('assets/book.png')),
// const SizedBox(width: 8),
// Text('books'.tr),
// ],
// ),
// ),
// Tab(
// child: Row(
// mainAxisSize: MainAxisSize.min,
// children: [
// const ImageIcon(AssetImage('assets/magazine.png')),
// const SizedBox(width: 8),
// Text('magazines'.tr),
// ],
// ),
// ),
// ],
// ),
// ),
// body: TabBarView(
// children: [
// SpecifiedBooks(
// titleText: titleText,
// ),
// SpecifiedMagazines(
// titleText: titleText,
// ),
// ],
// ),
// ),
// );
// }
}
}
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:matab/controllers/book_controller.dart';
import 'package:matab/ui/pages/search/search_books.dart';
import 'package:matab/ui/pages/styles.dart';
class SpecifiedBooks extends StatefulWidget {
final String titleText;
const SpecifiedBooks({Key? key, required this.titleText}) : super(key: key);
#override
State<SpecifiedBooks> createState() => _SpecifiedBooksState();
}
class _SpecifiedBooksState extends State<SpecifiedBooks> {
#override
Widget build(BuildContext context) {
final BookController bookController = Get.find(tag: 'bookController');
return SingleChildScrollView(
child: RefreshIndicator(
onRefresh: bookController.refreshList,
backgroundColor: mainColor,
color: Colors.white,
child: Obx(
(() {
if (bookController.isLoading.value) {
return const Center(
child: CircularProgressIndicator(),
);
} else {
return Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height - 100,
child: SearchBooks(
titleText: widget.titleText,
)),
],
);
}
}),
)),
);
}
}
instead of Get.find use Get.put bcz sometimes context not available
//final BookController bookController = Get.find(tag: 'bookController');
final BookController bookController = Get.put(BookController());
GetX's solution for this is to provide initialBinding in GetMaterialApp.
You may not be able to view the code sample below.
Future<void> main() async {
runApp(
GetMaterialApp(
title: "test App",
initialRoute: '/',
initialBinding: HomeBinding(),
getPages: AppPages.pages,
),
);
}
We create HomeBinding in a separate file as follows.
class HomeBinding implements Bindings {
#override
void dependencies() {
Get.lazyPut<MyController>(() => MyController());
Get.put<MyOtherController>(MyOtherController());
}
}
If you implement the above code correctly, when the application starts it will first see what controllers you need and initialize them.

Flutter : I want to change an image when you tap the image, and others are not affected by the tap

I am creating a simple app in Flutter. There are 7 images on 1 screen. I need a function that you can change an image when you tap one of the images. However, now when I tap an image, the other 6 images are also changed. I made a variable "isReal" to put into buildButton() and "isReal" would be switched true and false in the For statement which switch "isReal" in buildButton(). But, that did not work. Could you give me some advice on this problem? Thank you.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
class Screen extends StatefulWidget {
#override
_ScreenState createState() => _ScreenState();
}
class _ScreenState extends State<Screen> {
bool isReal = true;
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.teal[100],
// appBar: AppBar(
// title: Text('AnimalSounds'), backgroundColor: Colors.teal),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
buildButton('cat.mp3', Colors.red, 'images/cat.png',
'images/cat_real.jpg'),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
buildButton('dog.mp3', Colors.yellow, 'images/dog.png',
'images/cow.png'),
buildButton('cow.mp3', Colors.orange, 'images/cow.png',
'images/dog.png'),
])),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
buildButton('pig.mp3', Colors.green, 'images/pig.png',
'images/elephant.png'),
buildButton('elephant.mp3', Colors.teal,
'images/elephant.png', 'images/rooster.png'),
buildButton('rooster.mp3', Colors.blue,
'images/rooster.png', 'images/pig.png'),
])),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
buildButton('goat.mp3', Colors.purple, 'images/goat.jpg',
'images/pig.png'),
],
)),
],
),
)));
}
Expanded buildButton(sound, color, simpleImage, realImage) {
return Expanded(
child: FlatButton(
onPressed: () {
setState(() {
isReal = !isReal;
});
final player = AudioCache();
player.play(sound);
},
color: color,
child: isReal ? Image.asset(simpleImage) : Image.asset(realImage),
));
}
}
Ok, you have variable isReal that is the same for entire class (i.e. each button use the same variable). So when you change it's value by tapping on one button it affects all other buttons as well.
To solve this issue I would recommend to move button implementation into a separate Statefull widget. This way you can keep your Screen class as Stateless.
UPD:
Obviously you should watch some tutorials on how to make this on your own. But just for this time this is how it should look like after you separate widgets.
What I did here is:
Create new widget class FlipButton
Move code from your method into build function of new widget
Add parameters to constructor
This way when each FlipButton will have it's own isReal variable.
NOTE: I didn't try to compile it so there might be some errors.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
class Screen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.teal[100],
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
//replace all occurances on `buildButton` method with new widget
FlipButton(sound: 'cat.mp3', color: Colors.red, simpleImage: 'images/cat.png', realImage: 'images/cat_real.jpg'),
Expanded(
child: Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[
FlipButton(sound: 'dog.mp3', color: Colors.yellow, simpleImage: 'images/dog.png', realImage: 'images/cow.png'),
FlipButton(sound: 'cow.mp3', color: Colors.orange, simpleImage: 'images/cow.png', realImage: 'images/dog.png'),
])),
Expanded(
child: Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[
FlipButton(sound: 'pig.mp3', color: Colors.green, simpleImage: 'images/pig.png', realImage: 'images/elephant.png'),
FlipButton(sound: 'elephant.mp3', color: Colors.teal, simpleImage: 'images/elephant.png', realImage: 'images/rooster.png'),
FlipButton(sound: 'rooster.mp3', color: Colors.blue, simpleImage: 'images/rooster.png', realImage: 'images/pig.png'),
])),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
FlipButton(sound: 'goat.mp3', color: Colors.purple, simpleImage: 'images/goat.jpg', realImage: 'images/pig.png'),
],
)),
],
),
),
),
);
}
}
/// You can copy this widget into separate file for better formatting
///
class FlipButton extends StatefulWidget {
//declare final variables
final String sound;
final Color color;
final String simpleImage;
final String realImage;
//constructor for this class
const FlipButton({
Key? key,
required this.sound,
required this.color,
required this.simpleImage,
required this.realImage,
}) : super(key: key);
#override
_FlipButtonState createState() => _FlipButtonState();
}
class _FlipButtonState extends State<FlipButton> {
//inside the state declare variable that is about to change
bool isReal = false;
#override
Widget build(BuildContext context) {
return Expanded(
child: FlatButton(
onPressed: () {
setState(() {
isReal = !isReal;
});
final player = AudioCache();
player.play(sound);
},
color: widget.color,
child: isReal ? Image.asset(widget.simpleImage) : Image.asset(widget.realImage),
));
}
}
You can use Random class from dart:math to generate the next random image.
Exemple :
int imageNumber = 1;
void updateImage() {
setState(() {
//Random.nextInt(n) returns random integer from 0 to n-1
imageNumber = Random().nextInt(7) + 1;
});
}
#override
Widget build(BuildContext context) {
return Center(
child: Expanded(
child: Padding(
padding: const EdgeInsets.all(50.0),
child: FlatButton(
child: Image.asset('images/dice$imageNumber.png'),
onPressed: () {
updateImage();
},
),
),
),
);
}

Can I random to show SizeBox widget?

I want to show SizeBox widget by random. when I open this class it should be random to show Size Box 1 first or Size Box 2 first.
class Page extends StatelessWidget {
var random = new Random();
int randomNumber;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
randomNumber = random.nextInt(1);
if(randomNumber == 1){
/////////////////////////////////////// Size box 1 ////////////////////////////////////
SizedBox(
child: TextButton(
onPressed: () {
AlertDialog(
title: Text('a'),
content: Text('a'));
},
child: Image.asset('images/test.jpg'),
)),
}else{
/////////////////////////////////////// Size box 2 ////////////////////////////////////
SizedBox(
child: TextButton(
onPressed: () {
AlertDialog(
title: Text('b'),
content: Text('b'));
},
child: Image.asset('images/test2.jpg'),
)),
}
I try to use randomNumber = random.nextInt(1) in children: <Widget>[] but it show
The element type 'int' can't be assigned to the list type 'Widget'.dart(list_element_type_not_assignable)
It seems I cannot write dart code in widget. Can I random to show SizeBox widget ?
The error occurs because you called randomNumber = random.nextInt(1); inside the Row children. Since randomNumber is of type int and not of type Widget you can't do this. One way to fix this is to create a StatefulWidget and set the randomNumber in initState method.
By the way using if/else inside children of Row/Column you don't put the curly brackets {}.
import 'package:flutter/material.dart';
import 'dart:math';
class Page extends StatefulWidget {
#override
_Page createState() => _Page();
}
class _Page extends State<Page> {
var random = new Random();
int randomNumber;
#override
void initState(){
randomNumber = random.nextInt(1);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
if (randomNumber == 1)
/////////////////////////////////////// Size box 1 ////////////////////////////////////
SizedBox(
child: TextButton(
onPressed: () {
AlertDialog(title: Text('a'), content: Text('a'));
},
child: Image.asset('images/test.jpg'),
),
)
else
/////////////////////////////////////// Size box 2 ////////////////////////////////////
SizedBox(
child: TextButton(
onPressed: () {
AlertDialog(title: Text('b'), content: Text('b'));
},
child: Image.asset('images/test2.jpg'),
),
),
],
),
),
],
),
),
);
}
}

Screen not updating in a statefulWidget in Flutter

I have a statefulWidget in Flutter like this:
class GameScreen extends StatefulWidget {
#override
GameScreenState createState() => GameScreenState();
}
class GameScreenState extends State<GameScreen> {
List<String> selectedWord = ['h', 'e', 'l', 'l', 'o'];
Widget _letterInput() {
return Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
for (var letter in selectedWord) LetterInput(letter: letter)
],
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
_letterInput(),
],
)),
);
}
}
class LetterInput extends StatelessWidget {
LetterInput({this.letter});
final String letter;
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
decoration: BoxDecoration(
border: BorderDirectional(
bottom: BorderSide(width: 6.0, color: Colors.green))),
child: Text(letter,
textAlign: TextAlign.center,
style:
GoogleFonts.acme(fontSize: 28.0, fontWeight: FontWeight.w400)));
}
}
The problem is that when I first launch the app with this widget, I can see hello on the screen, but if I go on and change hello to hellos in selectedWord, that does not update the screen and it still shows me hello even though the hot reload is turned on. I have to go and restart the app so it shows me hellos. How could I fix this?
In my experience, hot reload keeps states. Try hot restart instead?
Referring to your comment, if you want to keep using hot reload, I suggest you pull out the variable to your widget itself (if that is an option for you), like this:
class GameScreen extends StatefulWidget {
final List<String> selectedWord = ['h', 'e', 'l', 'l', 'o'];
#override
GameScreenState createState() => GameScreenState();
}
class GameScreenState extends State<GameScreen> {
Widget _letterInput() {
return Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
for (var letter in widget.selectedWord) LetterInput(letter: letter)
],
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
_letterInput(),
],
)),
);
}
}

Flutter column widget: childs with different heights

I am trying to create a small test app with a large central area onto which i will render an image and a smaller bottom area that will contains an horizontal scrollable list of widgets. Here is the code:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:isolate';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:photo_app_ui_test/fxmanager/fx_manager.dart';
////
void main() {
runApp(SampleApp());
}
class SampleApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sample App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SampleAppPage(),
);
}
}
class SampleAppPage extends StatefulWidget {
SampleAppPage({Key key}) : super(key: key);
#override
_SampleAppPageState createState() => _SampleAppPageState();
}
class _SampleAppPageState extends State<SampleAppPage> {
FxManager fxManager;
bool showLoadingDialog = true;
#override
void initState() {
super.initState();
//loadData();
fxManager = FxManager();
fxManager.init().then( (dynamic) => initInterface() );
}
void initInterface(){
setState(() {
showLoadingDialog = false;
});
}
getBody() {
if (showLoadingDialog) {
return getProgressDialog();
} else {
return getEffectsWidget();//getListView();
}
}
getProgressDialog() {
return Center(child: CircularProgressIndicator());
}
getEffectsWidget() {
return
Column(
children: <Widget>[
Expanded(child: Container(color: Color.fromARGB(255, 255, 0, 0),
child: Center(child: Text("Image")))),
Flexible( child: Container(
color: Color.fromARGB(255, 0, 255, 0),
child: ListView(
scrollDirection: Axis.horizontal,
children: _getListData()
)))
]);
}
_getListData() {
List<Widget> widgets = [];
for (int i = 0; i < 100; i++) {
widgets.add(Padding(
padding: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
/*
Expanded(
child: Container(),
),*/
FlatButton(
onPressed: () => {},
color: Colors.orange,
padding: EdgeInsets.all(10.0),
child: Column(
// Replace with a Row for horizontal icon + text
children: <Widget>[Icon(Icons.add), Text("Add")],
)),
],
)));
}
return widgets;
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sample App"),
),
body: getBody());
}
}
produce this result:
I would like to make the green box's height as small as possible:
If the ListView widget is a direct children of the Column widget, the usual Horizontal viewport was given unbounded height exception is thrown. So i have to embed it inside a Flexible or Expanded.
Desired Output:
getEffectsWidget() {
return Column(children: <Widget>[
Expanded(
child: Container(
color: Color.fromARGB(255, 255, 0, 0),
child: Center(child: Text("Image")))),
Container(
color: Color.fromARGB(255, 0, 255, 0),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: _getListData(),
),
),
)
]);
}
static _getListData() {
List<Widget> widgets = [];
for (int i = 0; i < 100; i++) {
widgets.add(Padding(
padding: EdgeInsets.only(right: 10.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
/*
Expanded(
child: Container(),
),*/
FlatButton(
onPressed: () => {},
color: Colors.orange,
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisSize: MainAxisSize.min,
// Replace with a Row for horizontal icon + text
children: <Widget>[Icon(Icons.add), Text("Add")],
)),
],
)));
}
return widgets;
}