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/providers/equipment_profile_provider.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 'components/equipment_profile_container/widget_container_equipment_profile.dart';
@ -15,7 +16,7 @@ class EquipmentProfilesScreen extends StatefulWidget {
}
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 = [];
int get profilesCount => EquipmentProfiles.of(context).length;
@ -30,45 +31,43 @@ class _EquipmentProfilesScreenState extends State<EquipmentProfilesScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
elevation: 0,
title: Text(S.of(context).equipmentProfiles),
),
body: SafeArea(
bottom: false,
child: ListView.separated(
padding: EdgeInsets.fromLTRB(
Dimens.paddingM,
Dimens.paddingM,
Dimens.paddingM,
Dimens.paddingM +
MediaQuery.of(context).padding.bottom +
Dimens.grid56 +
kFloatingActionButtonMargin,
return SliverScreen(
title: S.of(context).equipmentProfiles,
appBarActions: [
if (profilesCount < maxProfiles)
IconButton(
onPressed: _addProfile,
icon: const Icon(Icons.add),
),
separatorBuilder: (context, index) =>
index > 0 ? const SizedBox(height: Dimens.grid16) : const SizedBox.shrink(),
itemCount: profilesCount,
itemBuilder: (context, index) => index > 0
? EquipmentProfileContainer(
key: profileContainersKeys[index],
data: EquipmentProfiles.of(context)[index],
onExpand: () => _keepExpandedAt(index),
onUpdate: (profileData) => _updateProfileAt(profileData, index),
onDelete: () => _removeProfileAt(index),
)
: const SizedBox.shrink(),
IconButton(
onPressed: Navigator.of(context).pop,
icon: const Icon(Icons.close),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: profilesCount < maxProfiles
? FloatingActionButton(
onPressed: _addProfile,
child: const Icon(Icons.add),
)
: null,
],
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => index > 0
? Padding(
padding: EdgeInsets.fromLTRB(
Dimens.paddingM,
index == 0 ? Dimens.paddingM : 0,
Dimens.paddingM,
Dimens.paddingM,
),
child: EquipmentProfileContainer(
key: profileContainersKeys[index],
data: EquipmentProfiles.of(context)[index],
onExpand: () => _keepExpandedAt(index),
onUpdate: (profileData) => _updateProfileAt(profileData, index),
onDelete: () => _removeProfileAt(index),
),
)
: const SizedBox.shrink(),
childCount: profileContainersKeys.length,
),
),
],
);
}

View file

@ -1,6 +1,6 @@
import 'package:flutter/material.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/general/widget_settings_section_general.dart';
@ -12,48 +12,27 @@ class SettingsScreen extends StatelessWidget {
@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(
S.of(context).settings,
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
fontSize: 24,
),
),
),
actions: [
IconButton(
onPressed: Navigator.of(context).pop,
icon: const Icon(Icons.close),
),
],
),
SliverList(
delegate: SliverChildListDelegate(
<Widget>[
const MeteringSettingsSection(),
const GeneralSettingsSection(),
const ThemeSettingsSection(),
const AboutSettingsSection(),
SizedBox(height: MediaQuery.of(context).padding.bottom),
],
),
),
],
return SliverScreen(
title: S.of(context).settings,
appBarActions: [
IconButton(
onPressed: Navigator.of(context).pop,
icon: const Icon(Icons.close),
),
),
],
slivers: [
SliverList(
delegate: SliverChildListDelegate(
<Widget>[
const MeteringSettingsSection(),
const GeneralSettingsSection(),
const ThemeSettingsSection(),
const AboutSettingsSection(),
SizedBox(height: MediaQuery.of(context).padding.bottom),
],
),
),
],
);
}
}

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)),
],
),
),
);
}
}