fix: SectionStandardList using BuildContext as param

This commit is contained in:
Rafael Saes 2023-06-30 09:34:22 -03:00
parent bb75472a83
commit c893da7a11
10 changed files with 61 additions and 139 deletions

View file

@ -42,10 +42,9 @@ class _AnonpayDetailsPageBodyState extends State<AnonpayDetailsPageBody> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SectionStandardList( return SectionStandardList(
context: context,
sectionCount: 1, sectionCount: 1,
itemCounter: (int _) => widget.anonpayDetailsViewModel.items.length, itemCounter: (int _) => widget.anonpayDetailsViewModel.items.length,
itemBuilder: (_, __, index) { itemBuilder: (__, index) {
final item = widget.anonpayDetailsViewModel.items[index]; final item = widget.anonpayDetailsViewModel.items[index];
if (item is DetailsListStatusItem) { if (item is DetailsListStatusItem) {

View file

@ -65,12 +65,11 @@ class ContactListPage extends BasePage {
final contacts = contactListViewModel.contactsToShow; final contacts = contactListViewModel.contactsToShow;
final walletContacts = contactListViewModel.walletContactsToShow; final walletContacts = contactListViewModel.walletContactsToShow;
return CollapsibleSectionList( return CollapsibleSectionList(
context: context,
sectionCount: 2, sectionCount: 2,
themeColor: Theme.of(context).primaryTextTheme!.titleLarge!.color!, themeColor: Theme.of(context).primaryTextTheme!.titleLarge!.color!,
dividerThemeColor: dividerThemeColor:
Theme.of(context).primaryTextTheme!.bodySmall!.decorationColor!, Theme.of(context).primaryTextTheme!.bodySmall!.decorationColor!,
sectionTitleBuilder: (_, int sectionIndex) { sectionTitleBuilder: (int sectionIndex) {
var title = S.current.contact_list_contacts; var title = S.current.contact_list_contacts;
if (sectionIndex == 0) { if (sectionIndex == 0) {
@ -84,7 +83,7 @@ class ContactListPage extends BasePage {
itemCounter: (int sectionIndex) => sectionIndex == 0 itemCounter: (int sectionIndex) => sectionIndex == 0
? walletContacts.length ? walletContacts.length
: contacts.length, : contacts.length,
itemBuilder: (_, sectionIndex, index) { itemBuilder: (sectionIndex, index) {
if (sectionIndex == 0) { if (sectionIndex == 0) {
final walletInfo = walletContacts[index]; final walletInfo = walletContacts[index];
return generateRaw(context, walletInfo); return generateRaw(context, walletInfo);

View file

@ -48,10 +48,9 @@ class OrderDetailsPageBodyState extends State<OrderDetailsPageBody> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Observer(builder: (_) { return Observer(builder: (_) {
return SectionStandardList( return SectionStandardList(
context: context,
sectionCount: 1, sectionCount: 1,
itemCounter: (int _) => orderDetailsViewModel.items.length, itemCounter: (int _) => orderDetailsViewModel.items.length,
itemBuilder: (_, __, index) { itemBuilder: (__, index) {
final item = orderDetailsViewModel.items[index]; final item = orderDetailsViewModel.items[index];
if (item is TrackTradeListItem) { if (item is TrackTradeListItem) {

View file

@ -56,12 +56,11 @@ class ConnectionSyncPage extends BasePage {
return Flexible( return Flexible(
child: SectionStandardList( child: SectionStandardList(
sectionCount: 1, sectionCount: 1,
context: context,
dividerPadding: EdgeInsets.symmetric(horizontal: 24), dividerPadding: EdgeInsets.symmetric(horizontal: 24),
itemCounter: (int sectionIndex) { itemCounter: (int sectionIndex) {
return nodeListViewModel.nodes.length; return nodeListViewModel.nodes.length;
}, },
itemBuilder: (_, sectionIndex, index) { itemBuilder: (sectionIndex, index) {
final node = nodeListViewModel.nodes[index]; final node = nodeListViewModel.nodes[index];
final isSelected = node.keyIndex == nodeListViewModel.currentNode.keyIndex; final isSelected = node.keyIndex == nodeListViewModel.currentNode.keyIndex;
final nodeListRow = Semantics( final nodeListRow = Semantics(

View file

@ -28,10 +28,9 @@ class SupportPage extends BasePage {
child: ConstrainedBox( child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: 500), constraints: BoxConstraints(maxWidth: 500),
child: SectionStandardList( child: SectionStandardList(
context: context,
sectionCount: 1, sectionCount: 1,
itemCounter: (int _) => supportViewModel.items.length, itemCounter: (int _) => supportViewModel.items.length,
itemBuilder: (_, __, index) { itemBuilder: (__, index) {
final item = supportViewModel.items[index]; final item = supportViewModel.items[index];
if (item is RegularListItem) { if (item is RegularListItem) {

View file

@ -53,10 +53,9 @@ class TradeDetailsPageBodyState extends State<TradeDetailsPageBody> {
return Observer(builder: (_) { return Observer(builder: (_) {
// FIX-ME: Added `context` it was not used here before, maby bug ? // FIX-ME: Added `context` it was not used here before, maby bug ?
return SectionStandardList( return SectionStandardList(
context: context,
sectionCount: 1, sectionCount: 1,
itemCounter: (int _) => tradeDetailsViewModel.items.length, itemCounter: (int _) => tradeDetailsViewModel.items.length,
itemBuilder: (_, __, index) { itemBuilder: (__, index) {
final item = tradeDetailsViewModel.items[index]; final item = tradeDetailsViewModel.items[index];
if (item is TrackTradeListItem) { if (item is TrackTradeListItem) {

View file

@ -10,11 +10,6 @@ import 'package:cake_wallet/src/widgets/list_row.dart';
import 'package:cake_wallet/src/screens/transaction_details/blockexplorer_list_item.dart'; import 'package:cake_wallet/src/screens/transaction_details/blockexplorer_list_item.dart';
import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart'; import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart';
import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/utils/date_formatter.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:hive/hive.dart';
class TransactionDetailsPage extends BasePage { class TransactionDetailsPage extends BasePage {
TransactionDetailsPage({required this.transactionDetailsViewModel}); TransactionDetailsPage({required this.transactionDetailsViewModel});
@ -28,10 +23,9 @@ class TransactionDetailsPage extends BasePage {
Widget body(BuildContext context) { Widget body(BuildContext context) {
// FIX-ME: Added `context` it was not used here before, maby bug ? // FIX-ME: Added `context` it was not used here before, maby bug ?
return SectionStandardList( return SectionStandardList(
context: context,
sectionCount: 1, sectionCount: 1,
itemCounter: (int _) => transactionDetailsViewModel.items.length, itemCounter: (int _) => transactionDetailsViewModel.items.length,
itemBuilder: (_, __, index) { itemBuilder: (__, index) {
final item = transactionDetailsViewModel.items[index]; final item = transactionDetailsViewModel.items[index];
if (item is StandartListItem) { if (item is StandartListItem) {

View file

@ -26,10 +26,9 @@ class UnspentCoinsDetailsPage extends BasePage {
Widget body(BuildContext context) { Widget body(BuildContext context) {
// FIX-ME: Added `context` it was not used here before, maby bug ? // FIX-ME: Added `context` it was not used here before, maby bug ?
return SectionStandardList( return SectionStandardList(
context: context,
sectionCount: 1, sectionCount: 1,
itemCounter: (int _) => unspentCoinsDetailsViewModel.items.length, itemCounter: (int _) => unspentCoinsDetailsViewModel.items.length,
itemBuilder: (_, __, index) { itemBuilder: (__, index) {
final item = unspentCoinsDetailsViewModel.items[index]; final item = unspentCoinsDetailsViewModel.items[index];
if (item is StandartListItem) { if (item is StandartListItem) {

View file

@ -3,16 +3,14 @@ import 'package:flutter/material.dart';
class CollapsibleSectionList extends SectionStandardList { class CollapsibleSectionList extends SectionStandardList {
CollapsibleSectionList( CollapsibleSectionList(
{required BuildContext context, {required int sectionCount,
required int sectionCount,
required int Function(int sectionIndex) itemCounter, required int Function(int sectionIndex) itemCounter,
required Widget Function(BuildContext context, int sectionIndex, int itemIndex) itemBuilder, required Widget Function(int sectionIndex, int itemIndex) itemBuilder,
Color? themeColor, Color? themeColor,
Color? dividerThemeColor, Color? dividerThemeColor,
Widget Function(BuildContext context, int sectionIndex)? sectionTitleBuilder, Widget Function(int sectionIndex)? sectionTitleBuilder,
bool hasTopSeparator = false}) bool hasTopSeparator = false})
: super( : super(
context: context,
hasTopSeparator: hasTopSeparator, hasTopSeparator: hasTopSeparator,
sectionCount: sectionCount, sectionCount: sectionCount,
itemCounter: itemCounter, itemCounter: itemCounter,
@ -22,71 +20,23 @@ class CollapsibleSectionList extends SectionStandardList {
dividerThemeColor: dividerThemeColor); dividerThemeColor: dividerThemeColor);
@override @override
List<Widget> transform( Widget buildTitle(List<Widget> items, int sectionIndex) {
bool hasTopSeparator,
BuildContext context,
int sectionCount,
int Function(int sectionIndex) itemCounter,
Widget Function(BuildContext context, int sectionIndex, int itemIndex) itemBuilder,
Widget Function(BuildContext context, int sectionIndex)? sectionTitleBuilder,
Color? themeColor,
Color? dividerThemeColor) {
final items = <Widget>[];
for (var sectionIndex = 0; sectionIndex < sectionCount; sectionIndex++) {
final itemCount = itemCounter(sectionIndex);
items.add(Theme(
data: ThemeData(
textTheme: TextTheme(titleMedium: TextStyle(color: themeColor,fontFamily: 'Lato')),
backgroundColor: dividerThemeColor,
unselectedWidgetColor: themeColor,
accentColor: themeColor)
.copyWith(dividerColor: Colors.transparent),
child: Padding(
padding: const EdgeInsets.only(left: 24.0),
child: ListTileTheme(
contentPadding: EdgeInsets.only(right: 16,top:sectionIndex>0?26:0),
child: ExpansionTile(
textColor: themeColor,
iconColor: themeColor,
title: sectionTitleBuilder == null
? Container()
: Container(child: buildTitle(items, sectionIndex, context)),
initiallyExpanded: true,
children: buildSection(itemCount, items, sectionIndex, context),
),
),
),
));
}
items.add(StandardListSeparator(padding: EdgeInsets.only(left: 24)));
return items;
}
@override
Widget buildTitle(
List<Widget> items, int sectionIndex, BuildContext context) {
if (sectionTitleBuilder == null) { if (sectionTitleBuilder == null) {
throw Exception('Cannot to build title. sectionTitleBuilder is null'); throw Exception('Cannot to build title. sectionTitleBuilder is null');
} }
return sectionTitleBuilder!.call(context, sectionIndex); return sectionTitleBuilder!.call(sectionIndex);
} }
@override @override
List<Widget> buildSection(int itemCount, List<Widget> items, int sectionIndex, List<Widget> buildSection(int itemCount, List<Widget> items, int sectionIndex) {
BuildContext context) {
final List<Widget> section = []; final List<Widget> section = [];
for (var itemIndex = 0; itemIndex < itemCount; itemIndex++) { for (var itemIndex = 0; itemIndex < itemCount; itemIndex++) {
final item = itemBuilder(context, sectionIndex, itemIndex); final item = itemBuilder(sectionIndex, itemIndex);
section.add(StandardListSeparator()); section.add(StandardListSeparator());
section.add(item); section.add(item);
} }
return section; return section;
} }

View file

@ -4,8 +4,7 @@ import 'package:cake_wallet/src/widgets/standard_list_status_row.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class StandardListRow extends StatelessWidget { class StandardListRow extends StatelessWidget {
StandardListRow( StandardListRow({required this.title, required this.isSelected, this.onTap});
{required this.title, required this.isSelected, this.onTap});
final String title; final String title;
final bool isSelected; final bool isSelected;
@ -39,7 +38,8 @@ class StandardListRow extends StatelessWidget {
child: Row(mainAxisAlignment: MainAxisAlignment.start, children: [ child: Row(mainAxisAlignment: MainAxisAlignment.start, children: [
if (hasLeftOffset) SizedBox(width: 10), if (hasLeftOffset) SizedBox(width: 10),
Expanded( Expanded(
child: Text(title, child: Text(
title,
style: TextStyle( style: TextStyle(
fontSize: 14, fontSize: 14,
fontWeight: FontWeight.normal, fontWeight: FontWeight.normal,
@ -74,7 +74,6 @@ class SectionHeaderListRow extends StatelessWidget {
} }
class StandardListSeparator extends StatelessWidget { class StandardListSeparator extends StatelessWidget {
StandardListSeparator({this.padding, this.height = 1}); StandardListSeparator({this.padding, this.height = 1});
final EdgeInsets? padding; final EdgeInsets? padding;
@ -108,7 +107,6 @@ class StandardList extends StatelessWidget {
StandardListSeparator(padding: EdgeInsets.only(left: 24)), StandardListSeparator(padding: EdgeInsets.only(left: 24)),
itemCount: itemCount, itemCount: itemCount,
itemBuilder: itemBuilder); itemBuilder: itemBuilder);
} }
} }
@ -120,35 +118,22 @@ class SectionStandardListItem {
} }
class SectionStandardList extends StatelessWidget { class SectionStandardList extends StatelessWidget {
SectionStandardList( SectionStandardList({
{required this.itemCounter, required this.itemCounter,
required this.itemBuilder, required this.itemBuilder,
required this.sectionCount, required this.sectionCount,
required BuildContext context,
this.dividerPadding = const EdgeInsets.only(left: 24), this.dividerPadding = const EdgeInsets.only(left: 24),
this.themeColor, this.themeColor,
this.dividerThemeColor, this.dividerThemeColor,
this.sectionTitleBuilder, this.sectionTitleBuilder,
this.hasTopSeparator = false,}) this.hasTopSeparator = false,
: totalRows = [] { }) : totalRows = [];
totalRows.addAll(transform(
hasTopSeparator,
context,
sectionCount,
itemCounter,
itemBuilder,
sectionTitleBuilder,
themeColor,
dividerThemeColor));
}
final int sectionCount; final int sectionCount;
final bool hasTopSeparator; final bool hasTopSeparator;
final int Function(int sectionIndex) itemCounter; final int Function(int sectionIndex) itemCounter;
final Widget Function(BuildContext context, int sectionIndex, int itemIndex) final Widget Function(int sectionIndex, int itemIndex) itemBuilder;
itemBuilder; final Widget Function(int sectionIndex)? sectionTitleBuilder;
final Widget Function(BuildContext context, int sectionIndex)?
sectionTitleBuilder;
final List<Widget> totalRows; final List<Widget> totalRows;
final Color? themeColor; final Color? themeColor;
final Color? dividerThemeColor; final Color? dividerThemeColor;
@ -156,13 +141,10 @@ class SectionStandardList extends StatelessWidget {
List<Widget> transform( List<Widget> transform(
bool hasTopSeparator, bool hasTopSeparator,
BuildContext context,
int sectionCount, int sectionCount,
int Function(int sectionIndex) itemCounter, int Function(int sectionIndex) itemCounter,
Widget Function(BuildContext context, int sectionIndex, int itemIndex) Widget Function(int sectionIndex, int itemIndex) itemBuilder,
itemBuilder, Widget Function(int sectionIndex)? sectionTitleBuilder,
Widget Function(BuildContext context, int sectionIndex)?
sectionTitleBuilder,
Color? themeColor, Color? themeColor,
Color? dividerThemeColor) { Color? dividerThemeColor) {
final items = <Widget>[]; final items = <Widget>[];
@ -173,12 +155,12 @@ class SectionStandardList extends StatelessWidget {
} }
if (sectionTitleBuilder != null) { if (sectionTitleBuilder != null) {
items.add(buildTitle(items, sectionIndex, context)); items.add(buildTitle(items, sectionIndex));
} }
final itemCount = itemCounter(sectionIndex); final itemCount = itemCounter(sectionIndex);
items.addAll(buildSection(itemCount, items, sectionIndex, context)); items.addAll(buildSection(itemCount, items, sectionIndex));
items.add(sectionIndex + 1 != sectionCount items.add(sectionIndex + 1 != sectionCount
? SectionHeaderListRow() ? SectionHeaderListRow()
@ -188,21 +170,20 @@ class SectionStandardList extends StatelessWidget {
return items; return items;
} }
Widget buildTitle( Widget buildTitle(List<Widget> items, int sectionIndex) {
List<Widget> items, int sectionIndex, BuildContext context) {
if (sectionTitleBuilder == null) { if (sectionTitleBuilder == null) {
throw Exception('Cannot to build title. sectionTitleBuilder is null'); throw Exception('Cannot to build title. sectionTitleBuilder is null');
} }
return sectionTitleBuilder!.call(context, sectionIndex); return sectionTitleBuilder!.call(sectionIndex);
} }
List<Widget> buildSection(int itemCount, List<Widget> items, int sectionIndex, List<Widget> buildSection(
BuildContext context) { int itemCount, List<Widget> items, int sectionIndex) {
final List<Widget> section = []; final List<Widget> section = [];
for (var itemIndex = 0; itemIndex < itemCount; itemIndex++) { for (var itemIndex = 0; itemIndex < itemCount; itemIndex++) {
final item = itemBuilder(context, sectionIndex, itemIndex); final item = itemBuilder(sectionIndex, itemIndex);
section.add(item); section.add(item);
} }
@ -211,6 +192,9 @@ class SectionStandardList extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
totalRows.addAll(transform(hasTopSeparator, sectionCount, itemCounter,
itemBuilder, sectionTitleBuilder, themeColor, dividerThemeColor));
return ListView.separated( return ListView.separated(
separatorBuilder: (_, index) { separatorBuilder: (_, index) {
final row = totalRows[index]; final row = totalRows[index];
@ -219,7 +203,8 @@ class SectionStandardList extends StatelessWidget {
return Container(); return Container();
} }
if (row is StandardListStatusRow || row is TradeDetailsStandardListCard) { if (row is StandardListStatusRow ||
row is TradeDetailsStandardListCard) {
return Container(); return Container();
} }