unified SliverList

This commit is contained in:
Vadim 2023-03-26 16:06:03 +03:00
parent 0055b5f628
commit 7656f55662
3 changed files with 106 additions and 80 deletions

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/equipment_profile_provider.dart'; import 'package:lightmeter/providers/equipment_profile_provider.dart';
import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/shared/sliver_screen/screen_sliver.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'components/equipment_profile_container/widget_container_equipment_profile.dart'; import 'components/equipment_profile_container/widget_container_equipment_profile.dart';
@ -15,7 +16,7 @@ class EquipmentProfilesScreen extends StatefulWidget {
} }
class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> { class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
static const maxProfiles = 5; // replace with a constant from iap static const maxProfiles = 5 + 1; // replace with a constant from iap
late List<GlobalKey<EquipmentProfileContainerState>> profileContainersKeys = []; late List<GlobalKey<EquipmentProfileContainerState>> profileContainersKeys = [];
int get profilesCount => EquipmentProfiles.of(context).length; int get profilesCount => EquipmentProfiles.of(context).length;
@ -30,45 +31,43 @@ class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return SliverScreen(
appBar: AppBar( title: S.of(context).equipmentProfiles,
backgroundColor: Theme.of(context).scaffoldBackgroundColor, appBarActions: [
elevation: 0, if (profilesCount < maxProfiles)
title: Text(S.of(context).equipmentProfiles), IconButton(
onPressed: _addProfile,
icon: const Icon(Icons.add),
), ),
body: SafeArea( IconButton(
bottom: false, onPressed: Navigator.of(context).pop,
child: ListView.separated( icon: const Icon(Icons.close),
),
],
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => index > 0
? Padding(
padding: EdgeInsets.fromLTRB( padding: EdgeInsets.fromLTRB(
Dimens.paddingM, Dimens.paddingM,
index == 0 ? Dimens.paddingM : 0,
Dimens.paddingM, Dimens.paddingM,
Dimens.paddingM, Dimens.paddingM,
Dimens.paddingM +
MediaQuery.of(context).padding.bottom +
Dimens.grid56 +
kFloatingActionButtonMargin,
), ),
separatorBuilder: (context, index) => child: EquipmentProfileContainer(
index > 0 ? const SizedBox(height: Dimens.grid16) : const SizedBox.shrink(),
itemCount: profilesCount,
itemBuilder: (context, index) => index > 0
? EquipmentProfileContainer(
key: profileContainersKeys[index], key: profileContainersKeys[index],
data: EquipmentProfiles.of(context)[index], data: EquipmentProfiles.of(context)[index],
onExpand: () => _keepExpandedAt(index), onExpand: () => _keepExpandedAt(index),
onUpdate: (profileData) => _updateProfileAt(profileData, index), onUpdate: (profileData) => _updateProfileAt(profileData, index),
onDelete: () => _removeProfileAt(index), onDelete: () => _removeProfileAt(index),
),
) )
: const SizedBox.shrink(), : const SizedBox.shrink(),
childCount: profileContainersKeys.length,
), ),
), ),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, ],
floatingActionButton: profilesCount < maxProfiles
? FloatingActionButton(
onPressed: _addProfile,
child: const Icon(Icons.add),
)
: null,
); );
} }

View file

@ -1,6 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/screens/shared/sliver_screen/screen_sliver.dart';
import 'components/about/widget_settings_section_about.dart'; import 'components/about/widget_settings_section_about.dart';
import 'components/general/widget_settings_section_general.dart'; import 'components/general/widget_settings_section_general.dart';
@ -12,34 +12,15 @@ class SettingsScreen extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return SliverScreen(
body: SafeArea( title: S.of(context).settings,
top: false, appBarActions: [
bottom: false,
child: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
automaticallyImplyLeading: false,
expandedHeight: Dimens.grid168,
flexibleSpace: FlexibleSpaceBar(
centerTitle: false,
titlePadding: const EdgeInsets.all(Dimens.paddingM),
title: Text(
S.of(context).settings,
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
fontSize: 24,
),
),
),
actions: [
IconButton( IconButton(
onPressed: Navigator.of(context).pop, onPressed: Navigator.of(context).pop,
icon: const Icon(Icons.close), icon: const Icon(Icons.close),
), ),
], ],
), slivers: [
SliverList( SliverList(
delegate: SliverChildListDelegate( delegate: SliverChildListDelegate(
<Widget>[ <Widget>[
@ -52,8 +33,6 @@ class SettingsScreen extends StatelessWidget {
), ),
), ),
], ],
),
),
); );
} }
} }

View file

@ -0,0 +1,48 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/res/dimens.dart';
class SliverScreen extends StatelessWidget {
final String title;
final List<Widget> appBarActions;
final List<Widget> slivers;
const SliverScreen({
required this.title,
required this.appBarActions,
required this.slivers,
super.key,
});
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
top: false,
bottom: false,
child: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
automaticallyImplyLeading: false,
expandedHeight: Dimens.grid168,
flexibleSpace: FlexibleSpaceBar(
centerTitle: false,
titlePadding: const EdgeInsets.all(Dimens.paddingM),
title: Text(
title,
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
fontSize: Dimens.grid24,
),
),
),
actions: appBarActions,
),
...slivers,
SliverToBoxAdapter(child: SizedBox(height: MediaQuery.of(context).padding.bottom)),
],
),
),
);
}
}