Why Flutter SfDataGrid sorting just renders sorted rows and no changes in datasource? - flutter

Im trying to use build-in sorting in my SfDataGrid. It works fine and flutter rerenders and show me sorted table, but when im trying to get cell value in table its bring me past(not sorted) value of cell.
So I want to not only render the sorted list but also sort the data source as well.
return FutureBuilder(
future: _portfolioDataSource.loadAndBuildPortfolioData(),
builder: (ctx, snapshot) {
asyncSnapshotErrorHandler(snapshot);
if (snapshot.connectionState == ConnectionState.done) {
return SfDataGridTheme(
data: SfDataGridThemeData(
headerColor: Color.fromRGBO(243, 243, 250, 1)),
child: SfDataGrid(
onSelectionChanged: _portfolioDataSource.isEntitiesEmpty
? null
: _onSelectionChanged,
gridLinesVisibility: GridLinesVisibility.horizontal,
headerGridLinesVisibility: GridLinesVisibility.horizontal,
highlightRowOnHover: true,
source: _portfolioDataSource,
frozenColumnsCount: _portfolioDataSource.isEntitiesEmpty
? 0
: widget.frozenColumnsCount,
columnWidthMode: ColumnWidthMode.none,
allowPullToRefresh: true,
allowSorting: false,
allowMultiColumnSorting: false,
showSortNumbers: true,
loadMoreViewBuilder:
(BuildContext context, LoadMoreRows loadMoreRows) {
bool showIndicator = false;
return StatefulBuilder(builder:
(BuildContext context, StateSetter setState) {
if (showIndicator) {
return Container(
height: 60.0,
width: double.infinity,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.white,
border: BorderDirectional(
top: BorderSide(
width: 1.0,
color:
Color.fromRGBO(0, 0, 0, 0.26)))),
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(
Color.fromRGBO(18, 183, 106, 1),
)));
} else {
return Container(
height: 60.0,
width: double.infinity,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.white,
border: BorderDirectional(
top: BorderSide(
width: 1.0,
color:
Color.fromRGBO(0, 0, 0, 0.26)))),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin:
EdgeInsets.symmetric(horizontal: 20),
height: 36.0,
width: 142.0,
child: TextButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(
Color.fromRGBO(18, 183, 106, 1),
)),
child: Text('LOAD MORE',
style: TextStyle(
color: Colors.white)),
onPressed: () async {
_onSelectionChanged([], []);
if (context is StatefulElement &&
context.state.mounted) {
setState(() {
print(
widget.selectedInstanceIds);
showIndicator = true;
});
}
await loadMoreRows();
if (context is StatefulElement &&
context.state.mounted) {
setState(() {
showIndicator = false;
});
}
})),
Container(
margin:
EdgeInsets.symmetric(horizontal: 20),
height: 36.0,
width: 142.0,
child: TextButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(
Color.fromRGBO(18, 183, 106, 1),
)),
child: Text('LOAD ALL',
style: TextStyle(
color: Colors.white)),
onPressed: () async {
if (context is StatefulElement &&
context.state.mounted) {
setState(() {
showIndicator = true;
});
}
await _portfolioDataSource
.loadAllRows();
if (context is StatefulElement &&
context.state.mounted) {
setState(() {
showIndicator = false;
});
}
})),
],
));
}
});
},
And DataSource
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:zwe_spmis_mobile/models/controls/portfolio-control/portfolio_configs.dart';
import 'package:zwe_spmis_mobile/services/portfolio_service.dart';
import '../../../services/api/entity_search_service.dart';
import '../../../services/provider/service_provider.dart';
import '../../../utils/entity_helpers/filter/filter_builder.dart';
import '../../../zwe_icons_icons.dart';
import '../../entity.dart';
import '../../portfolio_items_state.dart';
class PortfolioDataSource extends DataGridSource {
final List<dynamic> _entities = [];
List<int> downloadedItemIds = [];
List<DataGridRow> _dataGridRows = [];
PortfolioService _portfolioService = new PortfolioService();
PortfolioItemsState _portfolioItemsState;
final FilterBuilder _filter;
final String _categorySystemName;
final Function buildDataGridRows;
final highlitedFieldSystemName;
String? bulkAction;
int pageNumber = 0;
int countPerPage = 20;
String sortFields = "DateUpdated";
bool isEntitiesEmpty = false;
EntitySearchService _offlineEntitySearchService =
ServiceProvider.getOfflineContext().getEntitySearchService();
PortfolioDataSource(
this._portfolioItemsState,
this._filter,
this._categorySystemName,
this.buildDataGridRows,
this.highlitedFieldSystemName,
this.countPerPage,
this.pageNumber,
{this.bulkAction: null});
Future<void> handleLoadMoreRows() async {
this.pageNumber++;
await loadMoreAndBuildPortfolioData();
}
Future<void> loadAllRows() async {
this.pageNumber = 0;
this.countPerPage = 500;
await loadAndBuildPortfolioData();
this.pageNumber = 25;
this.countPerPage = 20;
}
#override
List<DataGridRow> get rows => _dataGridRows;
#override
DataGridRowAdapter? buildRow(DataGridRow row) {
TextStyle? getTextStyle(DataGridCell<dynamic> dataGridCell) {
return TextStyle(
color: dataGridCell.columnName == ''
? Color.fromRGBO(19, 180, 105, 1)
: null);
}
return DataGridRowAdapter(
cells: row.getCells().map<Widget>((dataGridCell) {
return Container(
alignment: (dataGridCell.columnName == 'id')
? Alignment.centerRight
: Alignment.centerLeft,
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: dataGridCell.columnName == 'actions'
? RichText(
text: TextSpan(
children: [
WidgetSpan(
child: (dataGridCell.value.toString() == 'Download')
? Icon(
ZweIcons.download,
size: 20,
color: Color.fromRGBO(19, 180, 105, 1),
)
: (dataGridCell.value.toString() == 'Submit')
? RichText(
text: TextSpan(
style: TextStyle(
color:
Color.fromRGBO(19, 180, 105, 1),
fontWeight: FontWeight.w900),
children: [
WidgetSpan(
child: Icon(
Icons.upload,
size: 20,
color: Color.fromRGBO(
19, 180, 105, 1),
),
),
],
),
)
: Text('')),
TextSpan(
style: dataGridCell.value.toString() == 'Downloaded' ||
dataGridCell.value.toString() == 'Delete'
? TextStyle(
color: Color.fromRGBO(19, 180, 105, 1),
fontWeight: FontWeight.w900)
: TextStyle(color: Color.fromRGBO(19, 180, 105, 1)),
text: dataGridCell.value.toString(),
),
],
),
)
: dataGridCell.columnName == 'sync'
? RichText(
text: TextSpan(
children: [
WidgetSpan(
child: Icon(
Icons.upload,
size: 20,
color: Color.fromRGBO(19, 180, 105, 1),
),
),
TextSpan(
style: TextStyle(
color: Color.fromRGBO(19, 180, 105, 1)),
text: "Submit",
),
],
),
)
: Text(
dataGridCell.value.toString(),
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: dataGridCell.columnName ==
this.highlitedFieldSystemName
? Color.fromRGBO(44, 121, 231, 1)
: dataGridCell.columnName == 'actions'
? Color.fromRGBO(19, 180, 105, 1)
: null),
));
}).toList());
}
addItem(Entity ent, String? bulkAction) {
_entities.add(ent);
_dataGridRows = buildDataGridRows(_entities, bulkAction);
notifyListeners();
}
Future<List<int>> loadDownloadedItemIds() {
return _offlineEntitySearchService.loadAllEntityIds(_categorySystemName);
}
removeItem(int entityInstanceId) {
this.downloadedItemIds.remove(entityInstanceId);
this
._entities
.removeWhere((element) => element['instanceId'] == entityInstanceId);
_dataGridRows.removeWhere((element) =>
element.getCells()[element.getCells().length - 1].value ==
entityInstanceId);
}
Future<List<Map<String, dynamic>>> loadCreatedItems() {
return _offlineEntitySearchService.loadEntitiesByInstanceIds(
this._categorySystemName,
downloadedItemIds.where((element) => element < 0).toList());
}
Future<void> loadAndBuildPortfolioData() async {
_entities.length = 0;
_entities.addAll(await loadCreatedItems());
this.downloadedItemIds = await loadDownloadedItemIds();
if (this.bulkAction == 'Download') {
_portfolioItemsState = PortfolioItemsState.NOT_DOWNLOADED;
}
await _portfolioService
.loadEntitiesByFilter(
_categorySystemName,
_portfolioItemsState,
sortFields,
countPerPage,
pageNumber,
_filter,
serializationFields[_categorySystemName])
.then((List<dynamic> value) {
_entities.addAll(value);
_dataGridRows =
buildDataGridRows(_entities, downloadedItemIds, bulkAction);
isEntitiesEmpty = (_entities.length == 0);
notifyListeners();
});
}
}

You can get the sorted row collection from the DataGridSource.effectiveRows property. It holds the sorted collection if the sorting is applied in DataGrid.
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
List<Employee> _employees = <Employee>[];
late EmployeeDataSource _employeeDataSource;
#override
void initState() {
super.initState();
_employees = _getEmployeeData();
_employeeDataSource = EmployeeDataSource(employeeData: _employees);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter DataGrid')),
body: Column(
children: [
Container(
margin: const EdgeInsets.all(30),
child: ElevatedButton(
onPressed: (() {
// You can get a sorted collection from effectiveRows property.
for (var details in _employeeDataSource.effectiveRows) {
print(details.getCells()[0].value);
}
}),
child: const Text('Get Sorted Collection')),
),
Expanded(
child: SfDataGrid(
source: _employeeDataSource,
allowSorting: true,
columnWidthMode: ColumnWidthMode.fill,
columns: <GridColumn>[
GridColumn(
columnName: 'ID',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text('ID'))),
GridColumn(
columnName: 'Name',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text('Name'))),
GridColumn(
columnName: 'Designation',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text('Designation'))),
GridColumn(
columnName: 'Salary',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text('Salary'))),
],
),
),
],
),
);
}
}
class EmployeeDataSource extends DataGridSource {
EmployeeDataSource({required List<Employee> employeeData}) {
_employeeData = employeeData
.map<DataGridRow>((Employee e) => DataGridRow(cells: <DataGridCell>[
DataGridCell<int>(columnName: 'ID', value: e.id),
DataGridCell<String>(columnName: 'Name', value: e.name),
DataGridCell<String>(
columnName: 'Designation', value: e.designation),
DataGridCell<int>(columnName: 'Salary', value: e.salary),
]))
.toList();
}
List<DataGridRow> _employeeData = <DataGridRow>[];
#override
List<DataGridRow> get rows => _employeeData;
#override
DataGridRowAdapter buildRow(DataGridRow row) {
return DataGridRowAdapter(
cells: row.getCells().map<Widget>((DataGridCell cell) {
return Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(8.0),
child: Text(cell.value.toString()),
);
}).toList());
}
}
class Employee {
Employee(this.id, this.name, this.designation, this.salary);
final int id;
final String name;
final String designation;
final int salary;
}
List<Employee> _getEmployeeData() {
return <Employee>[
Employee(10001, 'James', 'Manager', 90000),
Employee(10002, 'Kathryn', 'Project Lead', 75000),
Employee(10003, 'Lara', 'Developer', 45000),
Employee(10004, 'Michael', 'Designer', 37000),
Employee(10005, 'Martin', 'Developer', 43000),
];
}

Related

Cannot remove strings from list in flutter

I have a page to select images from the gallery and add those urls in a list that will be passed to another page after popping. Everything works fine the first time the user accesses the page, however, when coming back to the Image View with an already populated list (passed as argument), whenever I try to edit/removing elements from it, the list remains the same. But the images I want to delete get removed correctly from the firebase storage.
I will attach the code I am trying to use. I am new to flutter so I will really appreciate your help.
If you have any questions about the code feel free to ask!
import 'dart:convert';
import 'dart:io';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:moneybo/utilities/dialogs/delete_dialog.dart';
import 'package:moneybo/utilities/dialogs/error_dialog.dart';
import 'package:moneybo/utilities/generics/get_arguments.dart';
class ImageView extends StatefulWidget {
const ImageView({super.key});
#override
State<ImageView> createState() => _ImageViewState();
}
class _ImageViewState extends State<ImageView> {
late final ImagePicker _picker;
late final ImageCache _cache;
List<String> imageUrls = [];
List<bool> isImageSelected = [];
List<String> imagesToDelete = [];
String imgs = "";
bool _isSelected = false;
#override
void initState() {
_picker = ImagePicker();
_cache = ImageCache();
super.initState();
}
#override
Widget build(BuildContext context) {
List<String>? newImageList;
final imageList = context.getArgument<String>();
if (imageList != null && imageList.isNotEmpty) {
newImageList = (jsonDecode(imageList) as List<dynamic>).cast<String>();
for (String url in newImageList) {
if (!imageUrls.contains(url)) {
imageUrls.add(url);
}
}
imgs = jsonEncode(imageUrls);
for (String element in imageUrls) {
isImageSelected.add(false);
}
}
return GestureDetector(
onTap: () {
List<bool> newIsImageSelected = [];
for (String element in imageUrls) {
newIsImageSelected.add(false);
}
setState(() {
_isSelected = false;
isImageSelected = newIsImageSelected;
imagesToDelete = [];
});
print(imageUrls);
// print(imgs);
},
child: Scaffold(
appBar: AppBar(
centerTitle: true,
toolbarHeight: 73,
leading: Padding(
padding: const EdgeInsets.only(top: 25, left: 5),
child: TextButton(
child: const Text(
"OK",
style: TextStyle(fontSize: 18, color: Colors.white),
),
onPressed: () => Navigator.pop(context, imgs),
),
),
actions: [
Padding(
padding: const EdgeInsets.only(right: 20, top: 20),
child: isImageSelected.contains(true)
? IconButton(
onPressed: () async {
final shouldDelete = await showDeleteDialog(
context, "Delete this image(s)?");
if (shouldDelete) {
for (String deleteImage in imagesToDelete) {
imageUrls.removeWhere((image) =>
image.hashCode == deleteImage.hashCode);
await FirebaseStorage.instance
.refFromURL(deleteImage)
.delete();
}
imgs = jsonEncode(imageUrls);
List<bool> newIsImageSelected = [];
for (String element in imageUrls) {
newIsImageSelected.add(false);
}
_cache.clear();
_cache.clearLiveImages();
setState(() {
isImageSelected = newIsImageSelected;
imagesToDelete = [];
});
}
},
icon: SizedBox(
height: 25,
child: Image.asset(
"lib/icons/bin.png",
color: Colors.white,
)))
: IconButton(
icon: const Icon(Icons.add_a_photo_rounded),
onPressed: () async {
List<XFile>? images = await _picker.pickMultiImage();
String uniqueFileName =
DateTime.now().microsecondsSinceEpoch.toString();
Reference referenceRoot =
FirebaseStorage.instance.ref();
Reference referenceDirImages =
referenceRoot.child("images");
try {
for (XFile image in images) {
uniqueFileName =
(int.parse(uniqueFileName) + 1).toString();
Reference referenceImageToUpload =
referenceDirImages.child(uniqueFileName);
await referenceImageToUpload
.putFile(File(image.path));
String imageUrl =
await referenceImageToUpload.getDownloadURL();
imageUrls.add(imageUrl);
isImageSelected.add(false);
}
} catch (error) {
if (mounted) {
showErrorDialog(context, "$error");
}
}
imgs = jsonEncode(imageUrls);
setState(() {});
},
),
),
],
title: const Padding(
padding: EdgeInsets.only(top: 30.0),
child: Text(
"Add images",
style: TextStyle(fontSize: 16),
),
),
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: <Color>[
Color.fromRGBO(24, 92, 92, 1),
Color.fromRGBO(33, 108, 108, 1),
Color.fromRGBO(40, 121, 121, 1),
Color.fromRGBO(48, 136, 136, 1),
Color.fromRGBO(50, 139, 139, 1),
Color.fromRGBO(54, 143, 143, 1),
Color.fromRGBO(57, 145, 145, 1),
]),
),
),
),
body: imageUrls.isNotEmpty
? Padding(
padding: const EdgeInsets.only(top: 20, left: 8, right: 8),
child: GridView.builder(
itemCount: imageUrls.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: imageUrls.length > 1 ? 2 : 1,
mainAxisSpacing: imageUrls.isNotEmpty ? 8 : 0,
crossAxisSpacing: imageUrls.isNotEmpty ? 8 : 0,
),
itemBuilder: (context, index) {
return GestureDetector(
onLongPress: () {
setState(() {
_isSelected = true;
});
},
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: _isSelected
? Colors.green
: const Color.fromRGBO(11, 68, 68, 1),
width: _isSelected ? 4 : 2),
borderRadius:
const BorderRadius.all(Radius.circular(20))),
child: Stack(
children: [
Center(
child: Image.network(
imageUrls[index],
fit: BoxFit.contain,
),
),
_isSelected
? Align(
alignment: Alignment.topRight,
child: Transform.scale(
scale: 1.3,
child: Checkbox(
side: const BorderSide(
width: 2,
color: Color.fromRGBO(
11, 68, 68, 1)),
shape: const CircleBorder(),
value: isImageSelected[index],
onChanged: (bool? value) {
setState(() {
isImageSelected[index] = value!;
});
if (isImageSelected[index]) {
if (imagesToDelete.isNotEmpty) {
imagesToDelete.insert(
index, imageUrls[index]);
} else {
imagesToDelete
.add(imageUrls[index]);
}
} else if (!isImageSelected[
index] &&
imagesToDelete.contains(
imageUrls[index])) {
imagesToDelete
.remove(imageUrls[index]);
} else {
return;
}
print(imagesToDelete);
}),
),
)
: const SizedBox(),
],
),
),
);
},
),
)
: Padding(
padding: const EdgeInsets.only(bottom: 150),
child: Center(
child: SizedBox(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
Icons.image_not_supported_outlined,
size: 150,
color: Color.fromRGBO(168, 168, 168, 1),
),
SizedBox(height: 25),
Text(
"No images yet",
style: TextStyle(
color: Color.fromRGBO(168, 168, 168, 1),
fontSize: 25,
),
)
]),
)),
),
),
);
}
}

No Error message but it still isn't displaying the products lodaded from firebase firestore

I'm trying to write a shopping app. But unfortunately I stuck with loading and displaying the products from firebase with the help of the package provider.
In the app you can get from the searchPage to the ResultPage. In the ResultPage a GridView.builder should display the products whose category matches your entered searchtext from the SearchPage.
From the ResultPage you can also get to a FilterScreen where you can set filters. After you have left this screen you should only see the products on the result screen which matches your filters.
But this isn't working at all. Instead of seeing the filtered products there are no products displayed. But I get no error message. So I really stuck at this problem.
I don't even know where it's coming from.
I would be very thankful if anyone could help me!
This is my result page code:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shs/providers/filters_provider.dart';
import 'package:shs/providers/products_provider.dart';
import 'package:shs/screens/filters_screen.dart';
import 'package:shs/widgets/bottom_navigation_bar.dart';
import 'package:shs/widgets/filter_overlay.dart';
import 'package:shs/widgets/my_scaffold.dart';
import 'package:shs/widgets/product_grid.dart';
import '../widgets/size_grid.dart';
import '../widgets/size_grid_buchstaben.dart';
class ResultPage extends StatefulWidget {
String searchtext;
String? gender;
ResultPage(
this.searchtext,
this.gender,
);
#override
State<ResultPage> createState() => _ResultPageState();
}
class _ResultPageState extends State<ResultPage> {
final searchController = TextEditingController();
#override
void initState() {
searchController.addListener(() => setState(() {}));
super.initState();
}
#override
Widget build(BuildContext context) {
return MyScaffold(
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: 30,
),
Container(
height: 47,
margin: EdgeInsets.only(top: 30, left: 30, right: 30),
child: TextField(
onSubmitted: ((value) {
setState(() {
widget.searchtext = value;
});
}),
controller: searchController,
textInputAction: TextInputAction.search,
style: Theme.of(context).textTheme.bodyMedium,
decoration: InputDecoration(
fillColor: Colors.white,
suffixIcon: searchController.text.isEmpty
? Container(
width: 0,
)
: IconButton(
onPressed: () {
searchController.clear();
},
icon: Icon(
Icons.close_rounded,
color: Colors.black,
)),
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(15)),
prefixIcon: Container(
padding: const EdgeInsets.only(top: 5, left: 8),
child: Image.asset(
'lib/asset/icons/Search.png',
),
),
hintText: 'Search',
hintStyle: Theme.of(context).textTheme.bodyMedium,
),
),
),
SizedBox(
height: 30,
),
Padding(
padding: const EdgeInsets.only(right: 230),
child: Container(
width: 109,
height: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Theme.of(context).colorScheme.primary,
),
child: GestureDetector(
onTap: () {
Navigator.of(context).pushNamed(FiltersScreen.routeName);
},
child: Row(
children: [
SizedBox(
width: 10,
),
Icon(Icons.filter_list_alt),
SizedBox(
width: 10,
),
Text(
'Filter',
style: Theme.of(context)
.textTheme
.bodyMedium!
.copyWith(fontSize: 15),
)
],
),
)),
),
SizedBox(
height: 30,
),
Consumer<FilterProvider>(
builder: (context, filters, _) => ProductGrid(
filterColor: filters.filterMap['color']!,
filterZustand: filters.filterMap['state']!,
filtersize: filters.filterMap['size']!,
filterProdCat: widget.searchtext,
filterprice: filters.filteredPrice,
)), //Marke noch überall hnzufügen, wenn Filter eingefügt + searchtext zur filters.filterList hinzufügen und wie die anderen darauf referenzieeren, Suchfeld für alle Attribute verwendbar machen
SizedBox(
height: 30,
),
MyBottomNavigationBar(),
],
)),
);
}
}
This is my FilterProviderCode
import 'package:flutter/material.dart';
class FilterProvider with ChangeNotifier {
Map<String, List<String>> _filterMap = {'size': [], 'state': [], 'color': []};
double? filteredPrice;
var _expandedSize = false;
var _expandedPrice = false;
var _expandedState = false;
var _expandedColor = false;
var _expandedBrand = false;
bool _selected = false;
bool get selected {
return _selected;
}
bool _selectedSize = false;
bool _selectedState1 = false;
bool _selectedState2 = false;
bool _selectedState3 = false;
bool _selectedState4 = false;
bool _selectedState5 = false;
bool _selectedColor1 = false;
bool _selectedColor2 = false;
bool _selectedColor3 = false;
bool _selectedColor4 = false;
bool _selectedColor5 = false;
bool _selectedColor6 = false;
bool _selectedColor7 = false;
bool _selectedColor8 = false;
bool get selectedSize {
return _selectedSize;
}
Map<String, List<String>> get filterMap {
return _filterMap;
}
void setSelectedSize(bool variable) {
_selectedSize = variable;
}
void setSelectedState1(bool variable) {
_selectedState1 = variable;
}
void setSelectedState2(bool variable) {
_selectedState2 = variable;
}
void setSelectedState3(bool variable) {
_selectedState3 = variable;
}
void setSelectedState4(bool variable) {
_selectedState4 = variable;
}
void setSelectedState5(bool variable) {
_selectedState5 = variable;
}
void setSelectedColor1(bool variable) {
_selectedColor1 = variable;
}
void setSelectedColor2(bool variable) {
_selectedColor2 = variable;
}
void setSelectedColor3(bool variable) {
_selectedColor3 = variable;
}
void setSelectedColor4(bool variable) {
_selectedColor4 = variable;
}
void setSelectedColor5(bool variable) {
_selectedColor5 = variable;
}
void setSelectedColor6(bool variable) {
_selectedColor6 = variable;
}
void setSelectedColor7(bool variable) {
_selectedColor7 = variable;
}
void setSelectedColor8(bool variable) {
_selectedColor8 = variable;
}
bool get selectedState1 {
return _selectedState1;
}
bool get selectedState2 {
return _selectedState2;
}
bool get selectedState3 {
return _selectedState3;
}
bool get selectedState4 {
return _selectedState4;
}
bool get selectedState5 {
return _selectedState5;
}
bool get selectedColor1 {
return _selectedColor1;
}
bool get selectedColor2 {
return _selectedColor2;
}
bool get selectedColor3 {
return _selectedColor3;
}
bool get selectedColor4 {
return _selectedColor4;
}
bool get selectedColor5 {
return _selectedColor5;
}
bool get selectedColor6 {
return _selectedColor6;
}
bool get selectedColor7 {
return _selectedColor7;
}
bool get selectedColor8 {
return _selectedColor8;
}
bool get expandedSize {
return _expandedSize;
}
bool get expandedPrice {
return _expandedPrice;
}
bool get expandedState {
return _expandedState;
}
bool get expandedColor {
return _expandedColor;
}
bool get expandedBrand {
return _expandedBrand;
}
void expandSize() {
_expandedSize = !_expandedSize;
notifyListeners();
}
void expandPrice() {
_expandedPrice = !_expandedPrice;
notifyListeners();
}
void expandState() {
_expandedState = !_expandedState;
notifyListeners();
}
void expandColor() {
_expandedColor = !_expandedColor;
notifyListeners();
}
void expandBrand() {
_expandedBrand = !_expandedBrand;
notifyListeners();
}
void applyFilter(String key, String entry) {
_selected = !_selected;
if (_selected) {
_filterMap[key]!.add(entry);
}
if (_selected == false) {
_filterMap[key]!.removeWhere((element) => element == entry);
}
print(_filterMap);
notifyListeners();
}
void applyFilterUpdate(double entry) {
filteredPrice = entry;
notifyListeners();
print(filteredPrice);
}
}
This is my ProductProvider Code
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:shs/models/product.dart';
class ProductProvider with ChangeNotifier {
final db = FirebaseFirestore.instance;
List<Product> _items = [];
List<Product> get items {
return [..._items];
}
/*Stream<QuerySnapshot> get stream {
return db
.collection('products')
.withConverter(
fromFirestore: Product.fromFirestore,
toFirestore: (Product product, _) => product.toFirestore(),
)
.snapshots();
}*/
Future<void> fetchProducts(
{required bool showAll,
List<dynamic>?
filtersize, //List ist halt manchmal leer wie bei allen anderen
double? filterprice,
List<dynamic>? filterZustand,
//required List<String>? filterMarke,
List<dynamic>? filterColor,
String? filterProdCat}) async {
List<Product> filteredProducts = [];
List<Product> allProducts = [];
List<String> allIDs = [];
final ref = db.collection('products').withConverter(
fromFirestore: Product.fromFirestore,
toFirestore: (Product product, _) => product.toFirestore(),
);
ref.snapshots().listen((event) {
List<String> products = [];
for (var doc in event.docs) {
products.add(doc.id);
allIDs = products;
}
});
allIDs.forEach((id) async {
final docSnap = await ref.doc(id).get();
final product = docSnap.data();
if (product != null) {
allProducts.add(product);
} else {
print('No such document.');
}
});
filteredProducts = allProducts.where((product) {
return filtersize!.contains(product
.size) || //Liste ist halt manchmal leer, also nur wahr wenn auch wirklich Filter gesetzt sind
product.price == filterprice ||
filterZustand!.contains(product.zustand) ||
//filterMarke.contains(product.marke) ||
filterColor!.contains(product.color) ||
product.prodCat == filterProdCat;
}).toList(); // nur wenn filtern erwünscht alle Filter Lists angeben, auch wenn sie leer sind sonst null
if (showAll) {
_items = allProducts;
} else {
_items = filteredProducts;
}
//notifyListeners();
}
}
This is my FiltersScreen Code
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shs/providers/filters_provider.dart';
import 'package:shs/widgets/filter_color.dart';
import 'package:shs/widgets/filter_state.dart';
import 'package:shs/widgets/marked_text.dart';
import 'package:shs/widgets/size_grid.dart';
import 'package:shs/widgets/size_grid_buchstaben.dart';
import 'package:shs/widgets/size_grid_hosen.dart';
import 'package:shs/widgets/size_grid_shoes.dart';
class FiltersScreen extends StatefulWidget {
static const routeName = '/filters';
#override
State<FiltersScreen> createState() => _FiltersScreenState();
}
class _FiltersScreenState extends State<FiltersScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
leading: IconButton(
icon: Icon(
Icons.close,
size: 50,
),
onPressed: () {
Navigator.of(context).pop();
},
),
backgroundColor: Theme.of(context).colorScheme.primary,
actions: [
GestureDetector(
onTap: () {}, // Navigate to AccountPage
child: Container(
child: Image.asset('lib/asset/icons/Account.png'),
),
),
SizedBox(
width: 7,
),
GestureDetector(
onTap: () {}, // Navigate to ShoppingBag
child: Container(
child: Image.asset('lib/asset/icons/ShoppingBag.png'),
),
),
GestureDetector(
onTap: () {}, // Navigate to Favorite
child: Container(
child: Image.asset('lib/asset/icons/Favorite.png'),
),
),
SizedBox(
width: 7,
),
],
),
body: Consumer<FilterProvider>(
builder: (context, filters, _) => SingleChildScrollView(
child: Column(children: [
SizedBox(
height: 30,
),
Center(
child: MarkedText(
'Filters',
() {},
Theme.of(context).colorScheme.primary,
fontsize: 80,
),
),
SizedBox(
height: 30,
),
ListTile(
title: Text(
'Size',
style: Theme.of(context)
.textTheme
.displayMedium!
.copyWith(fontSize: 50),
),
trailing: IconButton(
onPressed: () {
filters.expandSize();
},
icon: Icon(
filters.expandedSize
? Icons.expand_less
: Icons.expand_more,
size: 50,
color: Colors.black)),
),
if (filters.expandedSize)
Container(
height: 1800,
child: Column(
children: [
SizedBox(
height: 30,
),
SizedBox(
height: 300,
child: Container(
width: 300, height: 500, child: SizeGrid())),
SizedBox(
height: 30,
),
SizedBox(
height: 230,
child:
Container(width: 300, child: SizeGridBuchstaben())),
SizedBox(
height: 30,
),
SizedBox(
height: 450,
child: Container(width: 300, child: SizeGridHosen())),
SizedBox(
height: 30,
),
Text(
'Size Shoes',
style: Theme.of(context)
.textTheme
.displayMedium!
.copyWith(fontSize: 50),
),
SizedBox(
height: 30,
),
SizedBox(
height: 530,
child: Container(width: 300, child: SizeGridShoes())),
SizedBox(
height: 30,
),
],
),
),
ListTile(
title: Text(
'Price',
style: Theme.of(context)
.textTheme
.displayMedium!
.copyWith(fontSize: 50),
),
trailing: IconButton(
onPressed: () {
filters.expandPrice();
},
icon: Icon(
filters.expandedPrice
? Icons.expand_less
: Icons.expand_more,
size: 50,
color: Colors.black)),
),
if (filters.expandedPrice)
Container(
child: Row(children: [
SizedBox(
width: 20,
),
Text(
'bis',
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontSize: 32),
),
SizedBox(
width: 30,
),
Container(
width: 150,
height: 40,
child: TextField(
keyboardType: TextInputType.number,
onSubmitted: ((value) {
final doubleValue = double.parse(value);
filters.applyFilterUpdate(doubleValue);
}),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15)),
hintText: '\$',
hintStyle: Theme.of(context)
.textTheme
.bodyMedium!
.copyWith(fontSize: 24)),
)),
]),
),
ListTile(
title: Text(
'State',
style: Theme.of(context)
.textTheme
.displayMedium!
.copyWith(fontSize: 50),
),
trailing: IconButton(
onPressed: () {
filters.expandState();
},
icon: Icon(
filters.expandedState
? Icons.expand_less
: Icons.expand_more,
size: 50,
color: Colors.black)),
),
if (filters.expandedState) FiltersState(),
ListTile(
title: Text(
'Color',
style: Theme.of(context)
.textTheme
.displayMedium!
.copyWith(fontSize: 50),
),
trailing: IconButton(
onPressed: () {
filters.expandColor();
},
icon: Icon(
filters.expandedColor
? Icons.expand_less
: Icons.expand_more,
size: 50,
color: Colors.black)),
),
if (filters.expandedColor) FilterColor(),
ListTile(
title: Text(
'Brand',
style: Theme.of(context)
.textTheme
.displayMedium!
.copyWith(fontSize: 50),
),
trailing: IconButton(
onPressed: () {
filters.expandBrand();
},
icon: Icon(
filters.expandedBrand
? Icons.expand_less
: Icons.expand_more,
size: 50,
color: Colors.black)),
), //Marken aufzählen List Tile von Zustand verwenden
SizedBox(
height: 30,
),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(); //Filter anwenden
},
child: Text('Apply Filters!')),
]),
),
),
);
}
}
This is my Product Grid Code:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shs/providers/products_provider.dart';
import 'product_card.dart';
class ProductGrid extends StatelessWidget {
ScrollController scrollController = ScrollController();
List<String> filtersize;
List<String> filterColor;
String? filterProdCat;
List<String> filterZustand;
double? filterprice;
ProductGrid(
{required this.filterColor,
this.filterProdCat,
required this.filterZustand,
this.filterprice,
required this.filtersize});
Future<void> updateProducts(
{required BuildContext context,
required List<String> filtersize,
required List<String> filterColor,
//required List<String> filterMarke,
String? filterProdCat,
required List<String> filterZustand,
double? filterprice}) async {
await Provider.of<ProductProvider>(
context,
listen: false,
).fetchProducts(
showAll: false,
filtersize: filtersize,
filterColor: filterColor,
//filterMarke: filterMarke,
filterProdCat: filterProdCat,
filterZustand: filterZustand,
filterprice: filterprice);
}
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: updateProducts(
context: context,
filtersize: filtersize,
filterColor: filterColor,
filterZustand: filterZustand),
builder: ((context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return Consumer<ProductProvider>(
builder: (ctx, products, _) => GridView.builder(
controller: scrollController,
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: products.items.length,
itemBuilder: ((context, index) => ProductCard(
state: products.items[index].zustand!,
imageUrl: products.items[index].imageUrl,
price: products.items[index].price,
size: products.items[index].size!,
id: products.items[index].id,
prodCat: products.items[index].prodCat,
marke: products.items[index].marke)),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 10,
mainAxisExtent: 400)));
}));
}
}
And here is my dummy product data from firestore
enter image description here
If you need more code or anything else, no problem I will post it!
I'm new to Flutter so my code is a bit messy, I have to admit...
THANK YOU FOR YOUR HELP!

How Can i create a conditional questionnaire in Flutter

I am trying to build a questionnaire within an application
The idea is that the user answers questions
So every answer he gives is in one question
Accordingly he will receive the following question
For example if he answered a question with answer a
Will get one question
If he answered the same question with answer b
Will get another question later
how can i solve this
The code is attached
Thanks
import 'package:flutter/material.dart';
import 'package:gsheets/question_model.dart';
class QuizScreen extends StatefulWidget {
#override
State<QuizScreen> createState() => _QuizScreenState();
}
class _QuizScreenState extends State<QuizScreen> {
//define the datas
List<Question> questionList = getQuestions();
int currentQuestionIndex = 0;
int score = 0;
Answer? selectedAnswer;
int? nextQuestionId;
int? questionId;
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color.fromARGB(255, 5, 50, 80),
body: Container(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 32),
child:
Column(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
const Text(
"Simple Quiz App",
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
_questionWidget(),
_answerList(),
_nextButton(),
]),
),
);
}
_questionWidget() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Question ${currentQuestionIndex + 1}/${questionList.length.toString()}",
style: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 20),
Container(
alignment: Alignment.center,
width: double.infinity,
padding: const EdgeInsets.all(32),
decoration: BoxDecoration(
color: Colors.orangeAccent,
borderRadius: BorderRadius.circular(16),
),
child: Text(
questionList[currentQuestionIndex].questionText,
style: const TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.w600,
),
),
)
],
);
}
_answerList() {
return Column(
children: questionList[currentQuestionIndex]
.answerList
.map(
(e) => _answerButton(e),
)
.toList(),
);
}
Widget _answerButton(Answer answer) {
bool isSelected = answer == selectedAnswer;
return Container(
width: double.infinity,
margin: const EdgeInsets.symmetric(vertical: 8),
height: 48,
child: ElevatedButton(
child: Text(answer.answerText),
style: ElevatedButton.styleFrom(
shape: const StadiumBorder(),
primary: isSelected ? Colors.orangeAccent : Colors.white,
onPrimary: isSelected ? Colors.white : Colors.black,
),
onPressed: () {
// if (selectedAnswer == null) {
// if (answer.isCorrect) {
// score++;
// }
setState(() {
selectedAnswer = answer;
});
}
// },
),
);
}
_nextButton() {
// if(answer == )
bool isLastQuestion = false;
if (currentQuestionIndex == questionList.length - 1) {
isLastQuestion = true;
}
return Container(
width: MediaQuery.of(context).size.width * 0.5,
height: 48,
child: ElevatedButton(
child: Text(isLastQuestion ? "Submit" : "Next"),
style: ElevatedButton.styleFrom(
shape: const StadiumBorder(),
primary: Colors.blueAccent,
onPrimary: Colors.white,
),
onPressed: () {
// if(nextQuestionId == questionId)
if (isLastQuestion) {
//display score
showDialog(context: context, builder: (_) => _showScoreDialog());
} else {
//next question
setState(() {
selectedAnswer = null;
currentQuestionIndex++;
});
}
},
),
);
}
_showScoreDialog() {
bool isPassed = false;
if (score >= questionList.length * 0.6) {
//pass if 60 %
isPassed = true;
}
String title = isPassed ? "Passed " : "Failed";
return AlertDialog(
title: Text(
title + " | Score is $score",
style: TextStyle(color: isPassed ? Colors.green : Colors.redAccent),
),
content: ElevatedButton(
child: const Text("Restart"),
onPressed: () {
Navigator.pop(context);
setState(() {
currentQuestionIndex = 0;
score = 0;
selectedAnswer = null;
});
},
),
);
}
}
========================
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gsheets/quiz.dart';
class Question {
final String questionText;
final List<Answer> answerList;
final int questionId;
Question(this.questionText, this.answerList, this.questionId);
}
class Answer {
final String answerText;
final int nextQuestionId;
// final bool selectedOption;
Answer(this.answerText, this.nextQuestionId);
}
List<Question> getQuestions() {
List<Question> list = [];
list.add(Question(
"Which school did you learn",
[
Answer("a", 3),
Answer("b", 2),
],
3,
));
list.add(Question(
"what is your age",
[
Answer("18", 3),
Answer("20", 5),
],
2,
));
list.add(Question(
"which car do you drive",
[
Answer("c", 3),
Answer("d", 2),
],
3,
));
return list;
}

How to select multiple checkboxes in flutter in checkboxlisttile

Can anyone please tell me how do I select multiple options in checkboxlisttile.
Here I am able to click only one option. I want to set the status column in note table in database as completed when i check the particular item.
(Actually I want to select the item as completed and display it under another tab called completed. checkboxlisttile is created dynamically i.e from database. When a new note is added it is displayed in this listview.)
note_info.dart //this is the screen where notes are displayed i.e listview
import 'dart:io';
import 'package:vers2cts/models/note_model.dart';
import 'package:vers2cts/models/customer_model.dart';
import 'package:vers2cts/services/db_service.dart';
import 'package:vers2cts/utils/db_helper.dart';
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'new_note.dart';
class Note_Info extends StatefulWidget{
final String appBarTitle;
final CustomerModel customer;
//Note_Info();
Note_Info(this. customer, this.appBarTitle);
#override
State<StatefulWidget> createState() {
//return Note_InfoState();
return Note_InfoState(this. customer,this.appBarTitle);
}
}
class Note_InfoState extends State<Note_Info> {
DBService dbService = DBService();
List<NoteModel> noteList;
int count = 0;
static final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
NoteModel note=NoteModel();
String appBarTitle;
CustomerModel customer=new CustomerModel();
Note_InfoState(this.customer, this.appBarTitle);
bool rememberMe = false;
DateTime _date = DateTime.now();
TextEditingController custfNameController = TextEditingController();
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
updateListView();
if (noteList == null) {
noteList = List<NoteModel>();
updateListView();
}
TextStyle titleStyle = Theme.of(context).textTheme.subhead;
var height = MediaQuery.of(context).size.height;
var name=customer.first_name+" "+customer.last_name;
custfNameController.text = name;
return DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(
Icons.add,
),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => NewNote(customer,note)));
},
)
],
),
body: Container(
child: Column(
children: <Widget>[
TextField(controller: custfNameController,
style: TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold),
textAlign: TextAlign.center),
Padding(
padding: const EdgeInsets.all(15.0),
child: Row(children: [
ImageProfile(customer.cust_photo),
Padding(
padding: const EdgeInsets.only(left: 30.0),
child: IconButton(
icon: Icon(
Icons.call,
color: Colors.green,
size: 45,
),
onPressed: () {
},
),
),
],),
),
SizedBox(
height: 50,
child: AppBar(
bottom: TabBar(
tabs: [
Tab(
text: "All",
),
Tab(
text: "Pending",
),
Tab(
text: "Cancelled",
),
Tab(
text: "Completed",
),
],
),
),
),
// create widgets for each tab bar here
Expanded(
child: TabBarView(
children: [
// first tab bar view widget
Container(
child: getNotecheckList()
),
// second tab bar view widget
Container(
),
Container(
child: Center(
child: Text(
'Cancelled',
),
),
),
Container(
child: Center(
child: Text(
'Completed',
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 55.0,
width: 200,
child: RaisedButton(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
color: Theme
.of(context)
.primaryColorDark,
textColor: Colors.white,
child: Text('Save', textScaleFactor: 1.5,),
onPressed: () {
setState(() {
//_reset();
});
},
),
),
),
]
),
)
));
}
Widget ImageProfile(String fileName) {
return Center(
child: CircleAvatar(
radius: 80.0,
backgroundImage: fileName == null
?AssetImage('images/person_icon.jpg')
:FileImage(File(customer.cust_photo))),
);
}
Future<void> updateListView() {
final Future<Database> dbFuture = DB.init();
dbFuture.then((database) {
int cid=customer.cust_id;
Future<List<NoteModel>> noteListFuture = dbService.getCustomerNotes(cid);
noteListFuture.then((noteList) {
setState(() {
this.noteList = noteList;
this.count = noteList.length;
});
});
});
}
int _isChecked=-1;
ListView getNotecheckList() {
return ListView.builder(
itemCount: count,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.white,
elevation: 2.0,
child: CheckboxListTile(
title: Text(this.noteList[position].note),
subtitle: Text(this.noteList[position].actn_on),
//secondary: const Icon(Icons.web),
value: position== _isChecked,
onChanged: (bool value) {
setState(() {
_isChecked = value?position:-1;
});
},
controlAffinity: ListTileControlAffinity.leading,
),
);
},
);
}
}
new_note.dart //this is where new note is added.
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:smooth_star_rating/smooth_star_rating.dart';
import 'package:intl/intl.dart';
import 'package:vers2cts/models/customer_model.dart';
import 'package:vers2cts/models/note_model.dart';
import 'package:vers2cts/services/db_service.dart';
import 'package:vers2cts/utils/form_helper.dart';
class NewNote extends StatefulWidget{
final NoteModel note;
final CustomerModel customer;
NewNote(this.customer,this. note);
//Dropdown
/*
final String label;
final Function(Color) onChanged;
final double height;
final double width;
NewNote.fordropdwn({
Key key,
this.onChanged,
this.height = 25,
this.width = 150,
this.label,
}) : super(key: key);*/
#override
State<StatefulWidget> createState() {
//return New_NoteState(this.customer);
return New_NoteState(this.customer,this.note);
}
}
class New_NoteState extends State<NewNote> with SingleTickerProviderStateMixin{
New_NoteState(this.customer,this.note);
NoteModel note=new NoteModel();
CustomerModel customer=new CustomerModel();
TextEditingController NoteController=TextEditingController();
TextEditingController custfNameController = TextEditingController();
DateTime _reminderDate = DateTime.now();
DBService dbService=new DBService();
SpeedDial _speedDial(){
return SpeedDial(
// child: Icon(Icons.add),
animatedIcon: AnimatedIcons.add_event,
animatedIconTheme: IconThemeData(size: 24.0),
backgroundColor: Colors.yellow,
curve: Curves.easeInCirc,
children: [
SpeedDialChild(
child: Icon(Icons.location_on,color: Colors.yellow,),
//backgroundColor: Theme.of(context).primaryColor,
label: 'Add Location',
//labelBackgroundColor:Theme.of(context).primaryColor,
),
SpeedDialChild(
child: Icon(Icons.keyboard_voice),
//backgroundColor: Colors.yellow,
label: 'Add voice',
//labelBackgroundColor: Colors.yellow
),
SpeedDialChild(
child: Icon(Icons.attachment_outlined,color :Colors.redAccent),
//backgroundColor:Theme.of(context).primaryColorLight,
label: 'Add File',
// labelBackgroundColor: Theme.of(context).primaryColorLight
),
SpeedDialChild(
child: Icon(Icons.image,color: Colors.lightBlue,),
//backgroundColor: Colors.yellow,
label: 'Add Image',
// labelBackgroundColor: Colors.yellow,
),
],
);
}
//for DropDownMenu
Color value=Colors.red;
final List<Color> colors = [
Colors.red,
Colors.blue,
Colors.green,
Colors.yellow,
Colors.pink,
Colors.purple,
Colors.brown,
];
//for Switch
bool isSwitched = false;
var textValue = 'Switch is OFF';
void toggleSwitch(bool value) {
if(isSwitched == false)
{
setState(() {
isSwitched = true;
note.rmnd_ind=1;
//this.note.remindOn = _reminderDate.toString();
});
}
else
{
setState(() {
isSwitched = false;
note.rmnd_ind=0;
});
}
}
#override
Widget build(BuildContext context) {
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
var name=customer.first_name+customer.last_name;
custfNameController.text = name;
return WillPopScope(
onWillPop: () {
// Write some code to control things, when user press Back navigation button in device navigationBar
moveToLastScreen();
},
child: Scaffold(
appBar:AppBar(),
body:ListView(
children: <Widget>[
SizedBox(
height: 2.0,
),
TextField(controller: custfNameController,
style: TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold),
textAlign: TextAlign.center),
Align(
alignment: Alignment.centerLeft,
child: Text("Add New",textAlign: TextAlign.left,
style: TextStyle(fontSize: 22,fontWeight: FontWeight.bold),),
),
SizedBox(
height: 2.0,
),
Divider(),
SizedBox(
height: 2.0,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: NoteController,
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: const BorderSide(width: 2.0),)),
keyboardType: TextInputType.multiline,
minLines: 5,//Normal textInputField will be displayed
maxLines: 5, // when user presses enter it will adapt to it
onChanged: (value) {
this.note.note = value;
},
),
),
TableCalendar(
selectedDayPredicate: (day) {
return isSameDay(_reminderDate, day);
},
onDaySelected: (selectedDay, focusedDay) {
setState(() {
String _reminderDate = DateFormat('dd-MM-yyyy').format(selectedDay);
note.actn_on=_reminderDate.toString();
});
},// Set initial date
focusedDay: DateTime.now(),
firstDay: DateTime.utc(2010, 10, 16),
lastDay: DateTime.utc(2030, 3, 14),),
SizedBox(
height: height*0.03,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(//mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text("Remind me",style: TextStyle(fontSize: 20),),
Padding(
padding: const EdgeInsets.only(left:80.0),
child: Container(
child: Switch(
onChanged: toggleSwitch,
value: isSwitched,
//activeColor: Colors.blue,
//activeTrackColor: Colors.yellow,
//inactiveThumbColor: Colors.redAccent,
//inactiveTrackColor: Colors.orange,
),
),
),
],),
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(mainAxisAlignment: MainAxisAlignment.start,
children:<Widget>[
Text("Priority",style: TextStyle(fontSize: 20.0),),
Padding(
padding: const EdgeInsets.only(left:20.0),
child: Container(
child: SmoothStarRating(
size: height=50.0,
allowHalfRating: false,
onRated: (value) {
this.note.prty=value;
print("rating value -> $value");
},
),
),
)]),
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text("Color",style: TextStyle(fontSize: 20),),
Padding(
padding: const EdgeInsets.only(left:80.0),
child: Container(
child: DropdownButton<Color>(
value: value,
//hint: Text(widget.label ?? ''),
onChanged: (color) {
setState(() => value = color);
//widget.onChanged(color);
},
items: colors.map((e) => DropdownMenuItem(
value: e,
child: Container(
// width: 60.0,
//height: 10.0,
width: 60.0,
// height: widget.height,
color: e,
),
),
)
.toList(),
),
),
),
],),
),
SizedBox(
height: height*0.08,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 55.0,
width: 200,
child: RaisedButton(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
color: Theme.of(context).primaryColorDark,
textColor: Colors.white,
child: Text('Save',textScaleFactor: 1.5,),
onPressed: (){
setState(() {
_save();
});
},
),
),
),
],
),
floatingActionButton:_speedDial(),
));
}
void moveToLastScreen() {
Navigator.pop(context, true);
}
void _save() async {
moveToLastScreen();
note.cust_id=customer.cust_id;
print(customer.cust_id);
print(note.cust_id);
int result;
if (note.note_id != null) { // Case 1: Update operation
result = await dbService.updateNote(note);
} else { // Case 2: Insert Operation
result = await dbService.insertNote(note);
}
if (result != 0) { // Success
FormHelper.showAlertDialog(context,'Status', 'Note Saved Successfully');
} else { // Failure
FormHelper.showAlertDialog(context,'Status', 'Problem Saving Note');
}
}
}
db_service.dart
import 'package:vers2cts/models/customer_model.dart';
import 'package:vers2cts/models/languages_model.dart';
import 'package:vers2cts/models/note_model.dart';
import 'package:vers2cts/models/user_model.dart';
import 'package:vers2cts/utils/db_helper.dart';
class DBService {
Future<int> insertNote(NoteModel note) async {
await DB.init();
var result = await DB.insert(NoteModel.table, note);
return result;
}
Future<List<NoteModel>> getCustomerNotes(int customer) async {
await DB.init();
var res = await DB.rawQuery("note WHERE cust_id = '$customer'");
int count = res.length;
List<NoteModel> notelist = List<NoteModel>();
// For loop to create a 'Note List' from a 'Map List'
for (int i = 0; i < count; i++) {
notelist.add(NoteModel.fromMap(res[i]));
}
return notelist;
}
}
note_model.dart
import 'model.dart';
class NoteModel extends Model {
static String table = 'note';
bool isSelected=false;
int note_id;
int cust_id;
String note;
String actn_on;
int rmnd_ind;
double prty;
String colr;
String sts;
int id;
String cre_date;
String cre_by;
String mod_date;
String mod_by;
int txn_id;
int delete_ind;
NoteModel({
this.note_id,
this.cust_id,
this.note,
this.actn_on,
this.rmnd_ind,
this.prty,
this.colr,
this.sts,
this.id,
this.cre_date,
this.cre_by,
this.mod_date,
this.mod_by,
this.txn_id,
this.delete_ind
});
static NoteModel fromMap(Map<String, dynamic> map) {
return NoteModel(
note_id: map["note_id"],
cust_id: map['cust_id'],
note: map['note'].toString(),
actn_on: map['actn_on'].toString(),
rmnd_ind: map['rmnd_ind'],
prty: map['prty'],
colr: map['colr'].toString(),
sts: map['sts'].toString(),
id: map['id'],
cre_date: map['cre_date'].toString(),
cre_by: map['cre_by'].toString(),
mod_date: map['mod_date'].toString(),
mod_by: map['mod_by'].toString(),
txn_id: map['txn_id'],
delete_ind: map['delete_ind'],
);
}
Map<String, dynamic> toMap() {
Map<String, dynamic> map = {
'note_id': note_id,
'cust_id': cust_id,
'note':note,
'actn_on': actn_on,
'rmnd_ind': rmnd_ind,
'prty': prty,
'colr': colr,
'sts':sts,
'id': id,
'cre_date': cre_date,
'cre_by': cre_by,
'mod_date':mod_date,
'mod_by':mod_by,
'txn_id':txn_id,
'delete_ind': delete_ind
};
if (note_id != null) {
map['note_id'] = note_id;
}
return map;
}
}
db_helper.dart
import 'dart:async';
import 'package:vers2cts/models/model.dart';
import 'package:path/path.dart' as p;
import 'package:sqflite/sqflite.dart';
abstract class DB {
static Database _db;
static int get _version => 1;
static Future<Database> init() async {
if (_db != null) {
return _db;
}
try {
var databasesPath = await getDatabasesPath();
String _path = p.join(databasesPath, 'CTS.db');
_db = await openDatabase(_path, version: _version, onCreate: onCreate);
print('db location:'+_path);
} catch (ex) {
print(ex);
}
}
static void onCreate(Database db, int version) async {
await db.execute(
'CREATE TABLE note (note_id INTEGER PRIMARY KEY,cust_id INTEGER, '
'note TEXT, '
'actn_on TEXT, rmnd_ind INTEGER, prty REAL, colr TEXT,'
'sts TEXT,'
'id INTEGER, cre_date TEXT,cre_by TEXT, mod_date TEXT,mod_by TEXT, txn_id INTEGER, delete_ind INTEGER)');
}
static Future<List<Map<String, dynamic>>> query(String table) async =>
_db.query(table);
static Future<int> insert(String table, Model model) async =>
await _db.insert(table, model.toMap());
static Future<Batch> batch() async => _db.batch();
static Future<List<Map<String, dynamic>>> rawQuery(String table) async =>
_db.query(table);
}
You need to store what all values are selected from user and then play with it.
For example -
var selectedIndexes = [];
ListView getNotecheckList() {
return ListView.builder(
itemCount: count,
itemBuilder: (_, int index) {
return Card(
color: Colors.white,
elevation: 2.0,
child: CheckboxListTile(
title: Text(this.noteList[position].note),
subtitle: Text(this.noteList[position].actn_on),
value: selectedIndexes.contains(index),
onChanged: (_) {
if (selectedIndexes.contains(index)) {
selectedIndexes.remove(index); // unselect
} else {
selectedIndexes.add(index); // select
}
},
controlAffinity: ListTileControlAffinity.leading,
),
);
},
);
}
store only index or whole array and play around
Output :-
Code :-
import 'package:flutter/material.dart';
class CheckBoxExample extends StatefulWidget {
const CheckBoxExample({Key? key}) : super(key: key);
#override
State<CheckBoxExample> createState() => _CheckBoxExampleState();
}
class _CheckBoxExampleState extends State<CheckBoxExample> {
List multipleSelected = [];
List checkListItems = [
{
"id": 0,
"value": false,
"title": "Sunday",
},
{
"id": 1,
"value": false,
"title": "Monday",
},
{
"id": 2,
"value": false,
"title": "Tuesday",
},
{
"id": 3,
"value": false,
"title": "Wednesday",
},
{
"id": 4,
"value": false,
"title": "Thursday",
},
{
"id": 5,
"value": false,
"title": "Friday",
},
{
"id": 6,
"value": false,
"title": "Saturday",
},
];
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 64.0),
child: Column(
children: [
Column(
children: List.generate(
checkListItems.length,
(index) => CheckboxListTile(
controlAffinity: ListTileControlAffinity.leading,
contentPadding: EdgeInsets.zero,
dense: true,
title: Text(
checkListItems[index]["title"],
style: const TextStyle(
fontSize: 16.0,
color: Colors.black,
),
),
value: checkListItems[index]["value"],
onChanged: (value) {
setState(() {
checkListItems[index]["value"] = value;
if (multipleSelected.contains(checkListItems[index])) {
multipleSelected.remove(checkListItems[index]);
} else {
multipleSelected.add(checkListItems[index]);
}
});
},
),
),
),
const SizedBox(height: 64.0),
Text(
multipleSelected.isEmpty ? "" : multipleSelected.toString(),
style: const TextStyle(
fontSize: 22.0,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
],
),
),
);
}
}

Null check operator used on a null value Carousel Flutter

Good Morning,
I'm trying to put a Carousel on the home page looking for Firebase data, but for some reason, the first time I load the application it appears the message below:
════════ Exception caught by widgets library ═════════════════════════════════════ ══════════════════
The following _CastError was thrown building DotsIndicator (animation: PageController # 734f9 (one client, offset 0.0), dirty, state: _AnimatedState # 636ca):
Null check operator used on a null value
and the screen looks like this:
After giving a hot reload the error continues to appear, but the image is loaded successfully, any tips of what I can do?
HomeManager:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provantagens_app/models/section.dart';
class HomeManager extends ChangeNotifier{
HomeManager({this.images}){
_loadSections();
images = images ?? [];
}
void addSection(Section section){
_editingSections.add(section);
notifyListeners();
}
final List<dynamic> _sections = [];
List<String> images;
List<dynamic> newImages;
List<dynamic> _editingSections = [];
bool editing = false;
bool loading = false;
int index, totalItems;
final Firestore firestore = Firestore.instance;
Future<void> _loadSections() async{
loading = true;
firestore.collection('home').snapshots().listen((snapshot){
_sections.clear();
for(final DocumentSnapshot document in snapshot.documents){
_sections.add( Section.fromDocument(document));
images = List<String>.from(document.data['images'] as List<dynamic>);
}
});
loading = false;
notifyListeners();
}
List<dynamic> get sections {
if(editing)
return _editingSections;
else
return _sections;
}
void enterEditing({Section section}){
editing = true;
_editingSections = _sections.map((s) => s.clone()).toList();
defineIndex(section: section);
notifyListeners();
}
void saveEditing() async{
bool valid = true;
for(final section in _editingSections){
if(!section.valid()) valid = false;
}
if(!valid) return;
loading = true;
notifyListeners();
for(final section in _editingSections){
await section.save();
}
for(final section in List.from(_sections)){
if(!_editingSections.any((s) => s.id == section.id)){
await section.delete();
}
}
loading = false;
editing = false;
notifyListeners();
}
void discardEditing(){
editing = false;
notifyListeners();
}
void removeSection(Section section){
_editingSections.remove(section);
notifyListeners();
}
void onMoveUp(Section section){
int index = _editingSections.indexOf(section);
if(index != 0) {
_editingSections.remove(section);
_editingSections.insert(index - 1, section);
index = _editingSections.indexOf(section);
}
notifyListeners();
}
HomeManager clone(){
return HomeManager(
images: List.from(images),
);
}
void onMoveDown(Section section){
index = _editingSections.indexOf(section);
totalItems = _editingSections.length;
if(index < totalItems - 1){
_editingSections.remove(section);
_editingSections.insert(index + 1, section);
index = _editingSections.indexOf(section);
}else{
}
notifyListeners();
}
void defineIndex({Section section}){
index = _editingSections.indexOf(section);
totalItems = _editingSections.length;
notifyListeners();
}
}
HomeScreen:
import 'package:carousel_pro/carousel_pro.dart';
import 'package:flutter/material.dart';
import 'package:provantagens_app/commom/custom_drawer.dart';
import 'package:provantagens_app/commom/custom_icons_icons.dart';
import 'package:provantagens_app/models/home_manager.dart';
import 'package:provantagens_app/models/section.dart';
import 'package:provantagens_app/screens/home/components/home_carousel.dart';
import 'package:provantagens_app/screens/home/components/menu_icon_tile.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
// ignore: must_be_immutable
class HomeScreen extends StatelessWidget {
HomeManager homeManager;
Section section;
List<Widget> get children => null;
String videoUrl = 'https://www.youtube.com/watch?v=VFnDo3JUzjs';
int index;
var _tapPosition;
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(colors: const [
Colors.white,
Colors.white,
], begin: Alignment.topCenter, end: Alignment.bottomCenter)),
child: Scaffold(
backgroundColor: Colors.transparent,
drawer: CustomDrawer(),
appBar: AppBar(
backgroundColor: Colors.transparent,
iconTheme: IconThemeData(color: Colors.black),
title: Text('Página inicial', style: TextStyle(color: Color.fromARGB(255, 30, 158, 8))),
centerTitle: true,
actions: <Widget>[
Divider(),
],
),
body: Consumer<HomeManager>(
builder: (_, homeManager, __){
return ListView(children: <Widget>[
AspectRatio(
aspectRatio: 1,
child:HomeCarousel(homeManager),
),
Column(
children: <Widget>[
Container(
height: 50,
),
Divider(
color: Colors.black,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Padding(
padding: const EdgeInsets.only(left:12.0),
child: MenuIconTile(title: 'Parceiros', iconData: Icons.apartment, page: 1,),
),
Padding(
padding: const EdgeInsets.only(left:7.0),
child: MenuIconTile(title: 'Beneficios', iconData: Icons.card_giftcard, page: 2,),
),
Padding(
padding: const EdgeInsets.only(right:3.0),
child: MenuIconTile(title: 'Suporte', iconData: Icons.help_outline, page: 6,),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
MenuIconTile(iconData: Icons.assignment,
title: 'Dados pessoais',
page: 3)
,
MenuIconTile(iconData: Icons.credit_card_outlined,
title: 'Meu cartão',
page: 4)
,
MenuIconTile(iconData: Icons.account_balance_wallet_outlined,
title: 'Pagamento',
page: 5,)
,
],
),
Divider(
color: Colors.black,
),
Container(
height: 50,
),
Consumer<HomeManager>(
builder: (_, sec, __){
return RaisedButton(
child: Text('Teste'),
onPressed: (){
Navigator.of(context)
.pushReplacementNamed('/teste',
arguments: sec);
},
);
},
),
Text('Saiba onde usar o seu', style: TextStyle(color: Colors.black, fontSize: 20),),
Text('Cartão Pró Vantagens', style: TextStyle(color: Color.fromARGB(255, 30, 158, 8), fontSize: 30),),
AspectRatio(
aspectRatio: 1,
child: Image.network(
'https://static.wixstatic.com/media/d170e1_80b5f6510f5841c19046f1ed5bca71e4~mv2.png/v1/fill/w_745,h_595,al_c,q_90,usm_0.66_1.00_0.01/Arte_Cart%C3%83%C2%B5es.webp',
fit: BoxFit.fill,
),
),
Divider(),
Container(
height: 150,
child: Row(
children: [
AspectRatio(
aspectRatio: 1,
child: Image.network(
'https://static.wixstatic.com/media/d170e1_486dd638987b4ef48d12a4bafee20e80~mv2.png/v1/fill/w_684,h_547,al_c,q_90,usm_0.66_1.00_0.01/Arte_Cart%C3%83%C2%B5es_2.webp',
fit: BoxFit.fill,
),
),
Padding(
padding: const EdgeInsets.only(left:20.0),
child: RichText(
text: TextSpan(children: <TextSpan>[
TextSpan(
text: 'Adquira já o seu',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
TextSpan(
text: '\n\CARTÃO PRÓ VANTAGENS',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Color.fromARGB(255, 30, 158, 8)),
),
]),
),
),
],
),
),
Divider(),
tableBeneficios(),
Divider(),
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text(
'O cartão Pró-Vantagens é sediado na cidade de Hortolândia/SP e já está no mercado há mais de 3 anos. Somos um time de profissionais apaixonados por gestão de benefícios e empenhados em gerar o máximo de valor para os conveniados.'),
FlatButton(
onPressed: () {
launch(
'https://www.youtube.com/watch?v=VFnDo3JUzjs');
},
child: Text('SAIBA MAIS')),
],
),
),
Container(
color: Color.fromARGB(255, 105, 190, 90),
child: Column(
children: <Widget>[
Row(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Text(
'© 2020 todos os direitos reservados a Cartão Pró Vantagens.',
style: TextStyle(fontSize: 10),
),
)
],
),
Divider(),
Row(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Text(
'Rua Luís Camilo de Camargo, 175 -\n\Centro, Hortolândia (piso superior)',
style: TextStyle(fontSize: 10),
),
),
Padding(
padding: const EdgeInsets.only(left: 16),
child: IconButton(
icon: Icon(CustomIcons.facebook),
color: Colors.black,
onPressed: () {
launch(
'https://www.facebook.com/provantagens/');
},
),
),
Padding(
padding: const EdgeInsets.only(left: 16),
child: IconButton(
icon: Icon(CustomIcons.instagram),
color: Colors.black,
onPressed: () {
launch(
'https://www.instagram.com/cartaoprovantagens/');
},
),
),
],
),
],
),
)
],
),
]);
},
)
),
);
}
tableBeneficios() {
return Table(
defaultColumnWidth: FlexColumnWidth(120.0),
border: TableBorder(
horizontalInside: BorderSide(
color: Colors.black,
style: BorderStyle.solid,
width: 1.0,
),
verticalInside: BorderSide(
color: Colors.black,
style: BorderStyle.solid,
width: 1.0,
),
),
children: [
_criarTituloTable(",Plus, Premium"),
_criarLinhaTable("Seguro de vida\n\(Morte Acidental),X,X"),
_criarLinhaTable("Seguro de Vida\n\(Qualquer natureza),,X"),
_criarLinhaTable("Invalidez Total e Parcial,X,X"),
_criarLinhaTable("Assistência Residencial,X,X"),
_criarLinhaTable("Assistência Funeral,X,X"),
_criarLinhaTable("Assistência Pet,X,X"),
_criarLinhaTable("Assistência Natalidade,X,X"),
_criarLinhaTable("Assistência Eletroassist,X,X"),
_criarLinhaTable("Assistência Alimentação,X,X"),
_criarLinhaTable("Descontos em Parceiros,X,X"),
],
);
}
_criarLinhaTable(String listaNomes) {
return TableRow(
children: listaNomes.split(',').map((name) {
return Container(
alignment: Alignment.center,
child: RichText(
text: TextSpan(children: <TextSpan>[
TextSpan(
text: name != "X" ? '' : 'X',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
TextSpan(
text: name != 'X' ? name : '',
style: TextStyle(
fontSize: 10.0,
fontWeight: FontWeight.bold,
color: Color.fromARGB(255, 30, 158, 8)),
),
]),
),
padding: EdgeInsets.all(8.0),
);
}).toList(),
);
}
_criarTituloTable(String listaNomes) {
return TableRow(
children: listaNomes.split(',').map((name) {
return Container(
alignment: Alignment.center,
child: RichText(
text: TextSpan(children: <TextSpan>[
TextSpan(
text: name == "" ? '' : 'Plano ',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
TextSpan(
text: name,
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Color.fromARGB(255, 30, 158, 8)),
),
]),
),
padding: EdgeInsets.all(8.0),
);
}).toList(),
);
}
void _storePosition(TapDownDetails details) {
_tapPosition = details.globalPosition;
}
}
I forked the library to create a custom carousel for my company's project, and since we updated flutter to 2.x we had the same problem.
To fix this just update boolean expressions like
if(carouselState.pageController.position.minScrollExtent == null ||
carouselState.pageController.position.maxScrollExtent == null){ ... }
to
if(!carouselState.pageController.position.hasContentDimensions){ ... }
Here is flutter's github reference.
This worked for me
So I edited scrollposition.dart package
from line 133
#override
//double get minScrollExtent => _minScrollExtent!;
// double? _minScrollExtent;
double get minScrollExtent {
if (_minScrollExtent == null) {
_minScrollExtent = 0.0;
}
return double.parse(_minScrollExtent.toString());
}
double? _minScrollExtent;
#override
// double get maxScrollExtent => _maxScrollExtent!;
// double? _maxScrollExtent;
double get maxScrollExtent {
if (_maxScrollExtent == null) {
_maxScrollExtent = 0.0;
}
return double.parse(_maxScrollExtent.toString());
}
double? _maxScrollExtent;
Just upgrade to ^3.0.0 Check here https://pub.dev/packages/carousel_slider
I faced the same issue.
This is how I solved it
class PlansPage extends StatefulWidget {
const PlansPage({Key? key}) : super(key: key);
#override
State<PlansPage> createState() => _PlansPageState();
}
class _PlansPageState extends State<PlansPage> {
int _currentPage = 1;
late CarouselController carouselController;
#override
void initState() {
super.initState();
carouselController = CarouselController();
}
}
Then put initialization the carouselController inside the initState method I was able to use the methods jumpToPage(_currentPage ) and animateToPage(_currentPage) etc.
I use animateToPage inside GestureDetector in onTap.
onTap: () {
setState(() {
_currentPage = pageIndex;
});
carouselController.animateToPage(_currentPage);
},
I apologize in advance if this is inappropriate.
I solved the similar problem as follows. You can take advantage of the Boolean variable. I hope, help you.
child: !loading ? HomeCarousel(homeManager) : Center(child:ProgressIndicator()),
or
child: isLoading ? HomeCarousel(homeManager) : SplashScreen(),
class SplashScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Loading...')
),
);
}
}