Skip to content

Commit

Permalink
Implement minimum UI
Browse files Browse the repository at this point in the history
  • Loading branch information
watamario15 committed Jul 15, 2022
1 parent e35a4ac commit 4d94427
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 119 deletions.
3 changes: 2 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.formsclient">
<application
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="formsclient"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
Expand Down
306 changes: 218 additions & 88 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,115 +1,245 @@
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter/material.dart';

void main() {
runApp(const MyApp());
}
class Selection {
final int label;
final String value;

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
const Selection(this.label, this.value);

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
Selection.fromJson(Map<String, dynamic> json)
: label = json['label']!,
value = json['value']!;

Map<String, dynamic> toJson() => {'label': label, 'value': value};
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
class Question {
final int id;
final String type;
final String explanation;
final List<Selection> selections;

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
const Question(this.id, this.type, this.explanation, this.selections);

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
Question.fromJson(Map<String, dynamic> json)
: id = json['id']!,
type = json['type']!,
explanation = json['explanation']!,
selections = [for (var item in json['selection']!) item];

Map<String, dynamic> toJson() => {
'id': id,
'type': type,
'explanation': explanation,
'selection': selections
};
}

class MyFormModel {
final String title;
final List<Question> questions;

@override
State<MyHomePage> createState() => _MyHomePageState();
const MyFormModel(this.title, this.questions);

MyFormModel.fromJson(Map<String, dynamic> json)
: title = json['title']!,
questions = [for (var item in json['questions']!) item];

Map<String, dynamic> toJson() => {'title': title, 'questions': questions};
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
class GetFormModel {
final String result;
final String id;
final MyFormModel form;

const GetFormModel(this.result, this.id, this.form);

GetFormModel.fromJson(Map<String, dynamic> json)
: result = json['result']!,
id = json['id']!,
form = json['form']!;

Map<String, dynamic> toJson() => {'result': result, 'id': id, 'form': form};
}

class CheckboxFormField extends FormField<Map<int, bool>> {
CheckboxFormField({
Key? key,
FormFieldSetter<Map<int, bool>>? onSaved,
FormFieldValidator<Map<int, bool>>? validator,
AutovalidateMode autovalidateMode = AutovalidateMode.disabled,
Map<int, bool>? initialValue,
required List<Selection> options,
}) : super(
key: key,
onSaved: onSaved,
validator: validator,
initialValue: initialValue ?? {},
autovalidateMode: autovalidateMode,
builder: (state) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
for (var item in options)
CheckboxListTile(
title: Text(item.value),
value: state.value!.putIfAbsent(item.label, () => false),
controlAffinity: ListTileControlAffinity.leading,
onChanged: (flag) {
if (flag == null) return;
final newState = state.value!;
newState[item.label] = flag;
state.didChange(newState);
},
),
],
);
},
);
}

class MyForm extends StatefulWidget {
const MyForm({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
State<MyForm> createState() => _MyFormState();
}

class _MyFormState extends State<MyForm> {
final _formKey = GlobalKey<FormState>();

// final _json = JSONForm.fromJson(jsonDecode(jsonString));
final _formResponse = const GetFormModel(
'ok',
'someid',
MyFormModel(
'testform',
[
Question(
0,
'radio',
'Hey! This is radio!',
[
Selection(0, 'This is first'),
Selection(1, 'This is second'),
Selection(2, 'This is third'),
],
),
Question(1, 'text', 'Hey! This is text!', []),
],
),
);

/// タイトル
Widget _title(String title) => Container(
decoration: const BoxDecoration(
color: Colors.black26,
),
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
children: [
Container(
padding: const EdgeInsets.all(8.0),
child: Text(
title,
style: Theme.of(context).textTheme.headline3,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: const Color.fromARGB(255, 255, 0, 0),
onPrimary: const Color.fromARGB(255, 0, 0, 0),
),
onPressed: null, // 未実装
child: const Text('編集'),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: const Color.fromARGB(255, 255, 0, 0),
onPrimary: const Color.fromARGB(255, 0, 0, 0),
),
onPressed: null, // 未実装
child: const Text('集計'),
),
],
),
],
),
);

/// 質問項目
Widget _formItem(Question question) => Container(
decoration: const BoxDecoration(
color: Colors.black26,
),
child: Column(
children: [
Text(
'$_counter',
question.explanation,
style: Theme.of(context).textTheme.headline4,
),
question.type == 'text'
? TextFormField(
onSaved: (content) {},
)
: CheckboxFormField(
options: question.selections,
onSaved: (selected) {},
),
],
),
);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('Zemi-A Forms'),
),
body: Form(
key: _formKey,
child: ListView(
children: [
_title(_formResponse.form.title),
for (var item in _formResponse.form.questions) _formItem(item),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
onPressed: () => _formKey.currentState!.save(),
tooltip: '送信',
child: const Icon(Icons.send),
),
);
}
}

class MyFormApp extends StatelessWidget {
const MyFormApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Zemi-A Form',
theme: ThemeData(
brightness: Brightness.light,
useMaterial3: true,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
useMaterial3: true,
),
home: const MyForm(),
);
}
}

void main() {
runApp(const MyFormApp());
}
2 changes: 2 additions & 0 deletions macos/Runner/DebugProfile.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<true/>
<key>com.apple.security.cs.allow-jit</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
Expand Down
2 changes: 2 additions & 0 deletions macos/Runner/Release.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>
21 changes: 21 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.4"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -156,6 +170,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
vector_math:
dependency: transitive
description:
Expand Down
Loading

0 comments on commit 4d94427

Please sign in to comment.