Skip to content

Commit

Permalink
Merge pull request #47 from wwt/Issue46
Browse files Browse the repository at this point in the history
Fixed Issue #46: `Dropdown`s without a specified `placeholder` should default to `"Submit..."` or `"请选择..."`
  • Loading branch information
goxiaoy committed Apr 15, 2023
2 parents 2109f41 + be6d2ee commit 7a6dea8
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 40 deletions.
13 changes: 7 additions & 6 deletions lib/generated/intl/messages_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:intl/intl.dart';
import 'package:intl/message_lookup_by_library.dart';
import 'package:intl/src/intl_helpers.dart';
Expand All @@ -20,8 +21,8 @@ import 'messages_zh.dart' as messages_zh;

typedef Future<dynamic> LibraryLoader();
Map<String, LibraryLoader> _deferredLibraries = {
'en': () => new Future.value(null),
'zh': () => new Future.value(null),
'en': () => new SynchronousFuture(null),
'zh': () => new SynchronousFuture(null),
};

MessageLookupByLibrary? _findExact(String localeName) {
Expand All @@ -36,18 +37,18 @@ MessageLookupByLibrary? _findExact(String localeName) {
}

/// User programs should call this before using [localeName] for messages.
Future<bool> initializeMessages(String localeName) async {
Future<bool> initializeMessages(String localeName) {
var availableLocale = Intl.verifiedLocale(
localeName, (locale) => _deferredLibraries[locale] != null,
onFailure: (_) => null);
if (availableLocale == null) {
return new Future.value(false);
return new SynchronousFuture(false);
}
var lib = _deferredLibraries[availableLocale];
await (lib == null ? new Future.value(false) : lib());
lib == null ? new SynchronousFuture(false) : lib();
initializeInternalMessageLookup(() => new CompositeMessageLookup());
messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor);
return new Future.value(true);
return new SynchronousFuture(true);
}

bool _messagesExistFor(String locale) {
Expand Down
2 changes: 2 additions & 0 deletions lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes
// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes

import 'package:intl/intl.dart';
import 'package:intl/message_lookup_by_library.dart';
Expand All @@ -23,6 +24,7 @@ class MessageLookup extends MessageLookupByLibrary {
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
"add": MessageLookupByLibrary.simpleMessage("Add"),
"nextPage": MessageLookupByLibrary.simpleMessage("Next Page"),
"placeholder": MessageLookupByLibrary.simpleMessage("Select..."),
"previousPage": MessageLookupByLibrary.simpleMessage("PreviousPage"),
"remove": MessageLookupByLibrary.simpleMessage("Remove"),
"submitSurvey": MessageLookupByLibrary.simpleMessage("Submit")
Expand Down
2 changes: 2 additions & 0 deletions lib/generated/intl/messages_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes
// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes

import 'package:intl/intl.dart';
import 'package:intl/message_lookup_by_library.dart';
Expand All @@ -23,6 +24,7 @@ class MessageLookup extends MessageLookupByLibrary {
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
"add": MessageLookupByLibrary.simpleMessage("添加"),
"nextPage": MessageLookupByLibrary.simpleMessage("下一页"),
"placeholder": MessageLookupByLibrary.simpleMessage("请选择..."),
"previousPage": MessageLookupByLibrary.simpleMessage("上一页"),
"remove": MessageLookupByLibrary.simpleMessage("删除"),
"submitSurvey": MessageLookupByLibrary.simpleMessage("提交")
Expand Down
10 changes: 10 additions & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"previousPage": "PreviousPage",
"submitSurvey": "Submit",
"add": "Add",
"remove": "Remove"
}
"remove": "Remove",
"placeholder": "Select..."
}
5 changes: 3 additions & 2 deletions lib/l10n/intl_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"previousPage": "上一页",
"submitSurvey": "提交",
"add": "添加",
"remove": "删除"
}
"remove": "删除",
"placeholder": "请选择..."
}
5 changes: 4 additions & 1 deletion lib/ui/elements/survey_element_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart
import 'package:logging/logging.dart';
import 'package:reactive_forms/reactive_forms.dart';

import '../../generated/l10n.dart';
import 'checkbox.dart';
import 'image.dart';
import 'matrix.dart';
Expand Down Expand Up @@ -123,9 +124,11 @@ class SurveyElementFactory {

register<s.Dropdown>((context, element, {bool hasTitle = true}) {
final e = (element as s.Dropdown);
final placeholderString = S.of(context).placeholder;

return ReactiveDropdownField(
formControlName: element.name!,
hint: element.placeholder == null ? null : Text(element.placeholder!),
hint: Text(element.placeholder ?? placeholderString),
items: e.choices
?.map((e) => DropdownMenuItem(
value: e.value,
Expand Down
5 changes: 5 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.1"
flutter_localizations:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
scrollable_positioned_list: ^0.3.5
im_stepper: ^1.0.1+1
flutter_widget_from_html_core: ^0.10.0
Expand Down
118 changes: 89 additions & 29 deletions test/questions/dropdown_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_survey_js/generated/l10n.dart';
import 'package:flutter_survey_js/model/survey.dart';
import 'package:flutter_survey_js/multi_localization_delegate.dart';
import 'package:flutter_survey_js/ui/survey_widget.dart';
Expand Down Expand Up @@ -34,39 +36,97 @@ void main() {
final s = Survey.fromJson(json);
});

testWidgets('displays placeholder', (WidgetTester tester) async {
const placeholder = 'Select...';
final s = Survey.fromJson(
{
"title": "Single Page Survey",
"pages": [
group('placeholder', () {
testWidgets('displays placeholder', (WidgetTester tester) async {
const placeholder = 'Select...';
final s = Survey.fromJson(
{
"title": "Single Page Survey",
"pages": [
{
"name": "page1",
"elements": [
{
"type": "dropdown",
"name": "question1",
"choices": ["Item 1", "Item 2", "Item 3"],
"placeholder": placeholder
}
]
}
]
},
);
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: const [
MultiAppLocalizationsDelegate(),
],
home: Material(
child: SurveyWidget(survey: s),
),
),
);
await tester.pump();
await tester.idle();

expect(find.text(placeholder), findsOneWidget);
});

const AppLocalizationDelegate appLocalizationDelegate =
AppLocalizationDelegate();
final locales = appLocalizationDelegate.supportedLocales;

for (var locale in locales) {
testWidgets(
'displays localized default text if `"placeholder"` is not specified in the JSON',
(WidgetTester tester) async {
final s = Survey.fromJson(
{
"name": "page1",
"elements": [
"title": "Single Page Survey",
"pages": [
{
"type": "dropdown",
"name": "question1",
"choices": ["Item 1", "Item 2", "Item 3"],
"placeholder": placeholder
"name": "page1",
"elements": [
{
"type": "dropdown",
"name": "question1",
"choices": ["Item 1", "Item 2", "Item 3"]
}
]
}
]
}
]
},
);
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: const [
MultiAppLocalizationsDelegate(),
],
home: Material(
child: SurveyWidget(survey: s),
),
),
);
await tester.pump();
await tester.idle();
},
);
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: const [
appLocalizationDelegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: Localizations(
delegates: const [
appLocalizationDelegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
locale: locale,
child: Material(
child: SurveyWidget(survey: s),
),
),
),
);
await tester.pump();
await tester.idle();
final placeholderText =
(await appLocalizationDelegate.load(locale)).placeholder;

expect(find.text(placeholder), findsOneWidget);
expect(find.text(placeholderText), findsOneWidget);
});
}
});
}

0 comments on commit 7a6dea8

Please sign in to comment.