Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added custom settings section #215

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/shop_app_example/lib/ui/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,24 @@ final appRoutes = <String, WidgetBuilder>{
Routes.talker: (context) => TalkerScreen(
talker: DI<Talker>(),
theme: talkerTheme,
customSettings: ValueNotifier([
CustomSettingsGroup(
name: 'Website settings',
isEnabled: true,
onChangedIsEnabled: (_) {},
items: [
CustomSettingsItemBool(
name: 'Disable remote logs',
value: false,
onChanged: (_) {},
),
CustomSettingsItemString(
name: 'Domain',
value: 'https://example.com',
onChanged: (_) {},
),
],
),
]),
),
};
1 change: 1 addition & 0 deletions packages/talker_flutter/lib/src/src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export 'ui/builders/builders.dart';
export 'ui/talker_builder.dart';
export 'ui/talker_listener.dart';
export 'ui/talker_screen.dart';
export 'ui/talker_settings/talker_settings.dart';
export 'ui/talker_view.dart';
export 'ui/talker_wrapper/talker_wrapper.dart';
export 'ui/theme/theme.dart';
Expand Down
5 changes: 5 additions & 0 deletions packages/talker_flutter/lib/src/ui/talker_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TalkerScreen extends StatelessWidget {
this.theme = const TalkerScreenTheme(),
this.itemsBuilder,
this.appBarLeading,
this.customSettings,
}) : super(key: key);

/// Talker implementation
Expand All @@ -28,6 +29,9 @@ class TalkerScreen extends StatelessWidget {
/// log items cards in list
final TalkerDataBuilder? itemsBuilder;

/// Optional custom settings
final ValueNotifier<List<CustomSettingsGroup>>? customSettings;

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -37,6 +41,7 @@ class TalkerScreen extends StatelessWidget {
theme: theme,
appBarTitle: appBarTitle,
appBarLeading: appBarLeading,
customSettings: customSettings,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:talker_flutter/src/ui/talker_settings/talker_settings.dart';

class CustomSettingsGroup {
final String name;
final bool showEnabled;
final bool isEnabled;
final Function(bool isEnabled)? onChangedIsEnabled;
final List<CustomSettingsItem> items;

const CustomSettingsGroup({
required this.name,
this.showEnabled = true,
this.isEnabled = true,
this.onChangedIsEnabled,
this.items = const [],
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter/material.dart';

abstract class CustomSettingsItem<T> {
final String name;
final T value;

const CustomSettingsItem({
required this.name,
required this.value,
});

Widget widgetBuilder(BuildContext context, T value, bool isEnabled);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:talker_flutter/src/ui/talker_settings/talker_settings.dart';

class CustomSettingsItemBool extends CustomSettingsItem<bool> {
final Function(bool value) onChanged;

const CustomSettingsItemBool({
required String name,
required bool value,
required this.onChanged,
}) : super(name: name, value: value);

@override
Widget widgetBuilder(
BuildContext context,
bool value,
bool isEnabled,
) {
return CupertinoSwitch(
value: value,
trackColor: isEnabled ? Colors.red : Colors.grey,
onChanged: isEnabled ? onChanged : null,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:flutter/cupertino.dart';
import 'package:talker_flutter/src/ui/talker_settings/talker_settings.dart';

class CustomSettingsItemString extends CustomSettingsItem<String> {
final Function(String value) onChanged;

const CustomSettingsItemString({
required String name,
required String value,
required this.onChanged,
}) : super(name: name, value: value);

@override
Widget widgetBuilder(
BuildContext context,
String value,
bool isEnabled,
) {
return StringWidgetBuilder(
onChanged: onChanged,
value: value,
isEnabled: isEnabled,
);
}
}

class StringWidgetBuilder extends StatefulWidget {
const StringWidgetBuilder({
Key? key,
required this.onChanged,
this.isEnabled = true,
this.value = '',
}) : super(key: key);

final Function(String value) onChanged;
final bool isEnabled;
final String value;

@override
State<StringWidgetBuilder> createState() => _StringWidgetBuilderState();
}

class _StringWidgetBuilderState extends State<StringWidgetBuilder> {
TextEditingController? _controller;

@override
void initState() {
super.initState();
_controller = TextEditingController(text: widget.value);
}

@override
Widget build(BuildContext context) {
return Container(
constraints: const BoxConstraints(maxWidth: 200),
child: CupertinoTextField(
controller: _controller,
enabled: widget.isEnabled,
onChanged: widget.isEnabled ? widget.onChanged : null,
),
);
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export 'custom_settings/custom_settings_group.dart';
export 'custom_settings/custom_settings_item.dart';
export 'custom_settings/custom_settings_item_bool.dart';
export 'custom_settings/custom_settings_item_string.dart';
export 'talker_settings_bottom_sheet.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TalkerSettingsBottomSheet extends StatefulWidget {
Key? key,
required this.talkerScreenTheme,
required this.talker,
required this.customSettings,
}) : super(key: key);

/// Theme for customize [TalkerScreen]
Expand All @@ -18,6 +19,9 @@ class TalkerSettingsBottomSheet extends StatefulWidget {
/// Talker implementation
final ValueNotifier<Talker> talker;

/// Custom settings
final List<CustomSettingsGroup> customSettings;

@override
State<TalkerSettingsBottomSheet> createState() =>
_TalkerSettingsBottomSheetState();
Expand Down Expand Up @@ -82,6 +86,43 @@ class _TalkerSettingsBottomSheetState extends State<TalkerSettingsBottomSheet> {
widget.talker.notifyListeners();
},
),
...widget.customSettings.map((CustomSettingsGroup group) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Text(
group.name,
textAlign: TextAlign.start,
style: theme.textTheme.titleLarge?.copyWith(
color: widget.talkerScreenTheme.textColor,
fontWeight: FontWeight.w700,
),
),
),
if (group.showEnabled)
TalkerSettingsCard(
talkerScreenTheme: widget.talkerScreenTheme,
title: 'Enabled',
enabled: group.isEnabled,
onChanged: group.onChangedIsEnabled,
),
...group.items.map(
(CustomSettingsItem item) => TalkerSettingsCard(
canEdit: group.isEnabled || !group.showEnabled,
talkerScreenTheme: widget.talkerScreenTheme,
title: item.name,
trailing: item.widgetBuilder(
context,
item.value,
group.isEnabled,
),
),
),
],
);
}),
// Padding(
// padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
// child: Text(
Expand Down Expand Up @@ -145,6 +186,7 @@ class _TalkerSettingsBottomSheetState extends State<TalkerSettingsBottomSheet> {
return BaseBottomSheet(
title: 'Talker Settings',
talkerScreenTheme: widget.talkerScreenTheme,
heightFactor: 0.9,
child: Expanded(
child: CustomScrollView(
slivers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ class TalkerSettingsCard extends StatelessWidget {
Key? key,
required this.talkerScreenTheme,
required this.title,
required this.enabled,
required this.onChanged,
this.enabled = false,
this.onChanged,
this.canEdit = true,
this.trailing,
}) : super(key: key);

final String title;
final bool enabled;
final Function(bool enabled) onChanged;
final Function(bool enabled)? onChanged;
final TalkerScreenTheme talkerScreenTheme;
final bool canEdit;
final Widget? trailing;

@override
Widget build(BuildContext context) {
Expand All @@ -40,11 +42,12 @@ class TalkerSettingsCard extends StatelessWidget {
fontWeight: FontWeight.w500,
),
),
trailing: CupertinoSwitch(
value: enabled,
trackColor: canEdit ? Colors.red : Colors.grey,
onChanged: canEdit ? onChanged : null,
),
trailing: trailing ??
CupertinoSwitch(
value: enabled,
trackColor: canEdit ? Colors.red : Colors.grey,
onChanged: canEdit ? onChanged : null,
),
),
),
),
Expand Down
32 changes: 20 additions & 12 deletions packages/talker_flutter/lib/src/ui/talker_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter/services.dart';
import 'package:group_button/group_button.dart';
import 'package:talker_flutter/src/controller/controller.dart';
import 'package:talker_flutter/src/ui/talker_monitor/talker_monitor.dart';
import 'package:talker_flutter/src/ui/talker_settings/talker_settings.dart';
import 'package:talker_flutter/src/ui/widgets/talker_view_appbar.dart';
import 'package:talker_flutter/talker_flutter.dart';

Expand All @@ -19,6 +18,7 @@ class TalkerView extends StatefulWidget {
this.appBarTitle,
this.itemsBuilder,
this.appBarLeading,
required this.customSettings,
}) : super(key: key);

/// Talker implementation
Expand All @@ -41,6 +41,9 @@ class TalkerView extends StatefulWidget {

final ScrollController? scrollController;

/// Optional custom settings
final ValueNotifier<List<CustomSettingsGroup>>? customSettings;

@override
State<TalkerView> createState() => _TalkerViewState();
}
Expand All @@ -60,7 +63,7 @@ class _TalkerViewState extends State<TalkerView> {
return TalkerBuilder(
talker: widget.talker,
builder: (context, data) {
final filtredElements =
final filteredElements =
data.where((e) => _controller.filter.filter(e)).toList();
final titles = data.map((e) => e.title).toList();
final uniqTitles = titles.toSet().toList();
Expand Down Expand Up @@ -88,7 +91,7 @@ class _TalkerViewState extends State<TalkerView> {
SliverList(
delegate: SliverChildBuilderDelegate(
(context, i) {
final data = _getListItem(filtredElements, i);
final data = _getListItem(filteredElements, i);
if (widget.itemsBuilder != null) {
return widget.itemsBuilder!.call(context, data);
}
Expand All @@ -100,7 +103,7 @@ class _TalkerViewState extends State<TalkerView> {
color: data.getFlutterColor(widget.theme),
);
},
childCount: filtredElements.length,
childCount: filteredElements.length,
),
),
],
Expand All @@ -121,11 +124,11 @@ class _TalkerViewState extends State<TalkerView> {
}

TalkerData _getListItem(
List<TalkerData> filtredElements,
List<TalkerData> filteredElements,
int i,
) {
final data = filtredElements[
_controller.isLogOrderReversed ? filtredElements.length - 1 - i : i];
final data = filteredElements[
_controller.isLogOrderReversed ? filteredElements.length - 1 - i : i];
return data;
}

Expand All @@ -135,12 +138,17 @@ class _TalkerViewState extends State<TalkerView> {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
isScrollControlled: false,
isScrollControlled: true,
builder: (context) {
return TalkerSettingsBottomSheet(
talkerScreenTheme: theme,
talker: talker,
);
return ValueListenableBuilder<List<CustomSettingsGroup>>(
valueListenable: widget.customSettings ?? ValueNotifier([]),
builder: (context, customSettings, child) {
return TalkerSettingsBottomSheet(
talkerScreenTheme: theme,
talker: talker,
customSettings: customSettings,
);
});
},
);
}
Expand Down
Loading