Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sunarya-thito committed Feb 24, 2025
1 parent 17c7dd3 commit 26e5b9c
Show file tree
Hide file tree
Showing 19 changed files with 702 additions and 188 deletions.
20 changes: 19 additions & 1 deletion docs/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:docs/pages/docs/components/dot_indicator_example.dart';
import 'package:docs/pages/docs/components/drawer_example.dart';
import 'package:docs/pages/docs/components/dropdown_menu_example.dart';
import 'package:docs/pages/docs/components/expandable_sidebar_example.dart';
import 'package:docs/pages/docs/components/formatted_input_example.dart';
import 'package:docs/pages/docs/components/hover_card_example.dart';
import 'package:docs/pages/docs/components/input_example.dart';
import 'package:docs/pages/docs/components/input_otp_example.dart';
Expand Down Expand Up @@ -80,6 +81,7 @@ import 'package:docs/pages/docs/theme_page.dart';
import 'package:docs/pages/docs/typography_page.dart';
import 'package:docs/pages/docs/web_preloader_page.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand All @@ -99,8 +101,18 @@ import 'pages/docs/components/number_input_example.dart';

const kEnablePersistentPath = false;

Map<String, Object?>? _docs;

String get flavor {
String? flavor = _docs?['flavor'] as String?;
assert(flavor != null, 'Flavor not found in docs.json');
return flavor!;
}

void main() async {
WidgetsFlutterBinding.ensureInitialized();
_docs = jsonDecode(await rootBundle.loadString('docs.json'));
print('Running app with flavor: $flavor');
GoRouter.optionURLReflectsImperativeAPIs = true;
final prefs = await SharedPreferences.getInstance();
var colorScheme = prefs.getString('colorScheme');
Expand Down Expand Up @@ -696,7 +708,13 @@ class MyAppState extends State<MyApp> {
path: 'expandable_sidebar',
builder: (context, state) => const ExpandableSidebarExample(),
name: 'expandable_sidebar',
)
),
GoRoute(
path: 'formatted_input',
builder: (context, state) {
return const FormattedInputExample();
},
name: 'formatted_input'),
]),
];
late ColorScheme colorScheme;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,34 @@ class _AutoCompleteExample1State extends State<AutoCompleteExample1> {
final TextEditingController _controller = TextEditingController();

void _updateSuggestions(String value) {
if (value.isEmpty) {
String? currentWord = _controller.currentWord;
if (currentWord == null || currentWord.isEmpty) {
setState(() {
_currentSuggestions = [];
});
return;
}
setState(() {
_currentSuggestions = suggestions
.where(
(element) => element.toLowerCase().contains(value.toLowerCase()))
.where((element) =>
element.toLowerCase().contains(currentWord.toLowerCase()))
.toList();
});
}

@override
Widget build(BuildContext context) {
return AutoComplete(
controller: _controller,
suggestions: _currentSuggestions,
onChanged: (value) {
_updateSuggestions(value);
},
trailing: IconButton.text(
icon: const Icon(Icons.clear),
density: ButtonDensity.compact,
onPressed: () {
_controller.clear();
setState(() {
_currentSuggestions = [];
});
},
child: TextField(
controller: _controller,
onChanged: _updateSuggestions,
trailing: const IconButton.text(
density: ButtonDensity.compact,
icon: Icon(Icons.clear),
onPressed: clearActiveTextInput,
),
),
onAcceptSuggestion: (value) {
_controller.text = _currentSuggestions[value];
setState(() {
_currentSuggestions = [];
});
},
);
}
}
36 changes: 23 additions & 13 deletions docs/lib/pages/docs/components/drawer/drawer_example_1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,29 @@ class _DrawerExample1State extends State<DrawerExample1> {
return Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(48),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Drawer ${count + 1} at ${positions[count % positions.length].name}'),
const Gap(16),
PrimaryButton(
onPressed: () {
open(context, count + 1);
},
child: const Text('Open Another Drawer'),
),
],
child: IntrinsicWidth(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Drawer ${count + 1} at ${positions[count % positions.length].name}'),
const Gap(16),
PrimaryButton(
onPressed: () {
open(context, count + 1);
},
child: const Text('Open Another Drawer'),
),
const Gap(8),
SecondaryButton(
onPressed: () {
closeOverlay(context);
},
child: const Text('Close Drawer'),
),
],
),
),
);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:shadcn_flutter/shadcn_flutter.dart';

class FormattedInputExample1 extends StatelessWidget {
const FormattedInputExample1({super.key});

@override
Widget build(BuildContext context) {
return FormattedInput(
parts: [
InputPart.editable(length: 2, width: 40),
InputPart.static('/'),
InputPart.editable(length: 2, width: 40),
InputPart.static('/'),
InputPart.editable(length: 4, width: 60),
],
trailing: IconButton.text(
density: ButtonDensity.compact,
icon: Icon(Icons.calendar_month),
onPressed: () {},
),
initialValue: FormattedValue(
[
'12',
'/',
'34',
'/',
'5678',
],
),
);
}
}
26 changes: 26 additions & 0 deletions docs/lib/pages/docs/components/formatted_input_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:shadcn_flutter/shadcn_flutter.dart';

import '../../widget_usage_example.dart';
import '../component_page.dart';
import 'formatted_input/formatted_input_example_1.dart';

class FormattedInputExample extends StatelessWidget {
const FormattedInputExample({super.key});

@override
Widget build(BuildContext context) {
return const ComponentPage(
name: 'formatted_input',
description: 'Text input with formatted parts.',
displayName: 'Formatted Input',
children: [
WidgetUsageExample(
title: 'Formatted Input Example',
path:
'lib/pages/docs/components/formatted_input/formatted_input_example_1.dart',
child: FormattedInputExample1(),
),
],
);
}
}
4 changes: 2 additions & 2 deletions docs/lib/pages/docs/components/input/input_example_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class InputExample2 extends StatefulWidget {
}

class _InputExample2State extends State<InputExample2> {
final TextEditingController _searchController = TextEditingController();
final TextEditingController _searchController =
TextEditingController(text: 'Hello World');
@override
Widget build(BuildContext context) {
return TextField(
controller: _searchController,
initialValue: 'Hello World!',
placeholder: Text('Search something...'),
leading: StatedWidget.builder(
builder: (context, states) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,15 @@ class MultiSelectExample1 extends StatefulWidget {
}

class _MultiSelectExample1State extends State<MultiSelectExample1> {
List<String> selectedValues = [];
Iterable<String>? selectedValues;
@override
Widget build(BuildContext context) {
return MultiSelect<String>(
itemBuilder: (context, item) {
return Text(item);
},
searchFilter: (item, query) {
return item.toLowerCase().contains(query.toLowerCase()) ? 1 : 0;
},
popupConstraints: const BoxConstraints(
maxHeight: 300,
maxWidth: 200,
),
onChanged: (value) {
setState(() {
selectedValues = value;
});
},
value: selectedValues,
placeholder: const Text('Select a fruit'),
children: const [
popup: SelectPopup(
items: SelectItemList(children: [
SelectGroup(
headers: [
SelectLabel(
Expand Down Expand Up @@ -106,7 +93,17 @@ class _MultiSelectExample1State extends State<MultiSelectExample1> {
),
],
),
],
])),
onChanged: (value) {
setState(() {
selectedValues = value;
});
},
constraints: BoxConstraints(
minWidth: 200,
),
value: selectedValues,
placeholder: const Text('Select a fruit'),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:shadcn_flutter/shadcn_flutter.dart';

class MultiSelectExample2 extends StatefulWidget {
const MultiSelectExample2({super.key});

@override
State<MultiSelectExample2> createState() => _MultiSelectExample2State();
}

class _MultiSelectExample2State extends State<MultiSelectExample2> {
final Map<String, List<String>> fruits = {
'Apple': ['Red Apple', 'Green Apple'],
'Banana': ['Yellow Banana', 'Brown Banana'],
'Lemon': ['Yellow Lemon', 'Green Lemon'],
'Tomato': ['Red', 'Green', 'Yellow', 'Brown'],
};
Iterable<String>? selectedValues;

Iterable<MapEntry<String, List<String>>> _filteredFruits(
String searchQuery) sync* {
for (final entry in fruits.entries) {
final filteredValues = entry.value
.where((value) => _filterName(value, searchQuery))
.toList();
if (filteredValues.isNotEmpty) {
yield MapEntry(entry.key, filteredValues);
} else if (_filterName(entry.key, searchQuery)) {
yield entry;
}
}
}

bool _filterName(String name, String searchQuery) {
return name.toLowerCase().contains(searchQuery);
}

@override
Widget build(BuildContext context) {
return MultiSelect<String>(
itemBuilder: (context, item) {
return Text(item);
},
popup: SelectPopup.builder(
searchPlaceholder: const Text('Search fruit'),
builder: (context, searchQuery) {
final filteredFruits = searchQuery == null
? fruits.entries
: _filteredFruits(searchQuery);
return SelectItemList(
children: [
for (final entry in filteredFruits)
SelectGroup(
headers: [
SelectLabel(
child: Text(entry.key),
),
],
children: [
for (final value in entry.value)
SelectItemButton(
value: value,
child: Text(value),
),
],
),
],
);
},
),
onChanged: (value) {
setState(() {
selectedValues = value;
});
},
constraints: BoxConstraints(
minWidth: 200,
),
value: selectedValues,
placeholder: const Text('Select a fruit'),
);
}
}
Loading

0 comments on commit 26e5b9c

Please sign in to comment.