フロント側をFlutter(スマホ)Thymeleaf(PC)、バックエンド側SpringBootの自動作成勉強中
9:10
①今日は、今まで作ったFlutter部分で共通化できる箇所を共通化しよう。
22:13
⓪dartファイル名はlowercase_with_underscoresでなければ警告が出るので、ファイル名をlowercase_with_underscoresに変更した。
例:commUtils.dart→comm_utils.dart
①consts.dartを作って以下たちを置いた。
/// 固定値クラス class Consts { /// 自WEBのURL static const String myHttpUrl = 'http://192.168.1.13:8080'; /// OK static const String ok = 'OK'; /// CANCEL static const String cancel = 'CANCEL'; }
②以下のメソッドをcomm_utils.dartに移動した。
user_list.dart(ユーザー情報一覧画面)のソート関連、ページング関連メソッドを共通化した。
///共通ユーティリティクラス class CommUtils { ・・・ /// ページング用DropdownMenuItemを戻す /// /// maxPageSizes 1画面あたりのページ数 /// contentSize 1ページのレコード数 /// totalPages 総ページ数 /// pageNumber 現在のページ番号 /// return ページング用DropdownMenuItem static List<DropdownMenuItem<int>> getPageDropDown(int maxPageSizes, int contentSize, int totalPages, int pageNumberBase) { List<DropdownMenuItem<int>> _items = <DropdownMenuItem<int>>[]; int totalGroups = (totalPages / (maxPageSizes + 0.0)).ceil(); int pageNumber = contentSize == 0 ? 0 : pageNumberBase; int pageNumberGroup = (pageNumber / (maxPageSizes + 0.0)).ceil(); int startPage = (pageNumberGroup - 1) * maxPageSizes + 1; int endPage = startPage + maxPageSizes - 1; if (endPage >= totalPages) { endPage = totalPages; } int pageNumber0Org = pageNumber - 1; Map<int,int> _minusMap = {}; if (pageNumberGroup > 1) { _items.add(getPageMinusItem('[最初]', _minusMap, 0 - 20, -3)); _items.add(getPageMinusItem('[前グループ]', _minusMap, ((pageNumberGroup - 1) * maxPageSizes - 1)*-1 - 20, -4)); } else { _items.add(const DropdownMenuItem( value: -5, child: Text('[最初]', style: TextStyle(fontWeight: FontWeight.normal, color: Colors.grey)), )); } if (pageNumber == 1) { _items.add(const DropdownMenuItem( value: -6, child: Text('[前ページ]', style: TextStyle(fontWeight: FontWeight.normal, color: Colors.grey)), )); } else { _items.add(getPageMinusItem('[前ページ]', _minusMap, (pageNumber0Org - 1)*-1 - 20, -7)); } for (int i = startPage; i <= endPage; i++) { if (pageNumber == i) { _items.add(DropdownMenuItem( value: i - 1, child: Text('[' + CommUtils.chgToString(i) + ']', style: const TextStyle(fontWeight:FontWeight.w900, color: Colors.black, backgroundColor: Colors.yellow)), )); } else { _items.add(DropdownMenuItem( value: i - 1, child: Text('[' + CommUtils.chgToString(i) + ']', style: const TextStyle(fontWeight: FontWeight.normal, color: Colors.black)), )); } } if (pageNumber == totalPages || totalPages == 1 || totalPages == 0) { if (totalPages == 0) { _items.add(const DropdownMenuItem( value: -8, child: Text('[1]', style: TextStyle( fontWeight: FontWeight.normal, color: Colors.grey)), )); _items.add(const DropdownMenuItem( value: -9, child: Text('[次ページ]"', style: TextStyle( fontWeight: FontWeight.normal, color: Colors.grey)), )); } else { _items.add(const DropdownMenuItem( value: -10, child: Text('[次ページ]', style: TextStyle( fontWeight: FontWeight.normal, color: Colors.grey)), )); } } else { _items.add(getPageMinusItem('[次ページ]', _minusMap, (pageNumber0Org + 1)*-1 - 20, -11)); } if (pageNumberGroup < totalGroups) { _items.add(getPageMinusItem('[次グループ]', _minusMap, (((pageNumberGroup * maxPageSizes) - 1) + 1)*-1 - 20, -12)); _items.add(getPageMinusItem('[最後]', _minusMap, (totalPages - 1)*-1 - 20, -13)); } else { _items.add(const DropdownMenuItem( value: -14, child: Text('[最後]', style: TextStyle(fontWeight: FontWeight.normal, color: Colors.grey)), )); } return _items.toList(); } static DropdownMenuItem<int> getPageMinusItem(String str, Map<int,int> minusMap, int minusVal, int constVal) { // すでに設定したバリューの場合 if (minusMap.containsKey(minusVal)) { return DropdownMenuItem( // 指定の値にする value: constVal, child: Text(str, style: const TextStyle(fontWeight: FontWeight.normal, color: Colors.grey)), ); } else { minusMap[minusVal] = minusVal; return DropdownMenuItem( value: minusVal, child: Text(str, style: const TextStyle(fontWeight: FontWeight.normal, color: Colors.black)), ); } } /// 並び順のドロップダウンリストを作成する static List<DropdownMenuItem<int>> getItems(List<String> selectItemNames, int selectItem, String sortOrder) { List<DropdownMenuItem<int>> _items = <DropdownMenuItem<int>>[]; String _nameWk = ""; for (int i = 0; i < selectItemNames.length; i++) { if (selectItem == i) { _nameWk = selectItemNames[i] ; if (sortOrder == 'D') { _nameWk += '▼'; } else if (sortOrder == 'A') { _nameWk += '▲'; } _items.add(DropdownMenuItem( value: i, child: Text(_nameWk, style: const TextStyle(fontWeight: FontWeight.normal, color: Colors.black, backgroundColor: Colors.yellow)), )); } else { _nameWk = selectItemNames[i]; _items.add(DropdownMenuItem( value: i, child: Text(_nameWk, style: const TextStyle(fontWeight: FontWeight.normal, color: Colors.black)), )); } } return _items.toList(); } /// 並び順の記号を返却する static String getSortOrderMark(SrchOrderForm srchOrderForm, String targetItemName) { String _nameWk = ""; if (srchOrderForm.sortOrder == 'D') { _nameWk = '▼ '; } else if (srchOrderForm.sortOrder == 'A') { _nameWk = '▲ '; } if (srchOrderForm.sortItemName == targetItemName) { return _nameWk; } else { return ''; } } }
③いろいろ考えたが、こんな制約が必要なことになった。→2022/04/07の日記でこの件は解消(制約必要なし)に向かっている。
こんな制約とは、
■システム固定で使用しているので使えない項目名。これらは、テーブルの項目名に使用しないでください。
理由:バックエンドSpringBootの@RequestBodyでデータを取り込むときに、
これらのデータとテーブルの項目値を一気に取り込むため、同じ名前だと困る。
・エラーメッセージ表示用に使用する。
itemErrorMessages; // 項目エラーメッセージ
errorMessage; // 実行結果エラーメッセージ
successMessage; // 実行結果OKメッセージ
・リスト表示のページング用に使用する。
page; // ページ番号
pageObjSize; // 検索結果の当ページの行数
pageObjTotalPages; // 検索結果の総ページ数
・画面遷移用モードに使用する。
mode; // モード
・リスト表示のソート用に使用する。
sortItemName;
sortOrder;
updMode;
sortItemNameErr;
sortOrderErr;
updModeErr;
・各項目ごとのエラーメッセージ表示用に使用する。
テーブルの項目名+Err
■2022/06/15に、勉強した成果:『Flutter_JavaSpringプログラム自動作成◎自動生成ツール』をVectorに載せました。Zenn本も書きました。使ってみての感想や間違いの指定や、こうやったほうがいいとかの情報があればメールください。
・Vector
www.vector.co.jp
・Zenn本(Flutter_JavaSpringプログラム自動作成)
zenn.dev