2022年8月4日 星期四

Flutter學習-10 TextField組件

 TextField主要用於輸入文字。其中的組件組成如下圖:


它的屬性很多。  此部件的作用在於輸入文字。

const TextField({
Key? key,
this.controller, //控制器,用來獲得及設置此TextField的內容,沒有設置,系統會自己設置一個。
this.focusNode, //用於管理節點,設置節點名稱
this.decoration = const InputDecoration(), //輸入框的裝飾器
TextInputType? keyboardType, //設置不同的輸入類型
this.textInputAction, //用於控制鍵盤動作,默認為是完成TextInputAction.done
this.textCapitalization = TextCapitalization.none, //
this.style, //輸入文本的樣式
this.strutStyle,
this.textAlign = TextAlign.start, //輸入文本的位置
this.textAlignVertical,
this.textDirection, //輸入文字的排列方向
this.readOnly = false, //是否只能閱讀
ToolbarOptions? toolbarOptions,
this.showCursor, //顯示光標
this.autofocus = false, //自動獲取焦點
this.obscuringCharacter = '•',
this.obscureText = false, //是否隱藏輸入文字
this.autocorrect = true, //是否自動校正
SmartDashesType? smartDashesType, //
SmartQuotesType? smartQuotesType,
this.enableSuggestions = true,
this.maxLines = 1, //最多的行數
this.minLines, //最少的行數
this.expands = false, //
this.maxLength, //一行最大的長度
this.maxLengthEnforcement, //設定在達到最長的長度時是否繼續輸入
this.onChanged, //改變時的回調
this.onEditingComplete, //編輯完成的回調
this.onSubmitted, //點擊完的回調,會回傳輸入的框的值當作參數
this.onAppPrivateCommand,
this.inputFormatters, //輸入的格式
this.enabled, //輸入框是否能輸入
this.cursorWidth = 2.0,
this.cursorHeight, //光標的長度
this.cursorRadius, //光標邊角的形狀
this.cursorColor, //光標的顏色
this.selectionHeightStyle = ui.BoxHeightStyle.tight,
this.selectionWidthStyle = ui.BoxWidthStyle.tight,
this.keyboardAppearance,
this.scrollPadding = const EdgeInsets.all(20.0),
this.dragStartBehavior = DragStartBehavior.start,
bool? enableInteractiveSelection,
this.selectionControls,
this.onTap, //點擊輸入框時的回調
this.mouseCursor,
this.buildCounter,
this.scrollController,
this.scrollPhysics,
this.autofillHints = const <String>[],
this.clipBehavior = Clip.hardEdge,
this.restorationId,
this.scribbleEnabled = true,
this.enableIMEPersonalizedLearning = true,
(1)focusNode使用說明

(1-1)設置兩個節點
FocusNode node1 = FocusNode();
FocusNode node2 = FocusNode();
 (1-2)在TextField中設置
focusNode: node1,
focusNode: node2,

(1-3)在onchange中加入以下程式,可跳至此部件中.
focusNode: node1,

FocusScope.of(context).requestFocus(node1);

TextField decoration

參考:


為了裝飾TextField ,decoration設置了一個InputDecoration類,用來提供TextField 樣式設計的各種屬性。
const InputDecoration({
this.icon, //小圖
this.iconColor, //小圖顏色
this.label, //描述輸入框
this.labelText, //描述輸入框的文字
this.labelStyle, //描述輸入框文字的樣式
this.floatingLabelStyle,
this.helperText, //輸入框入方的輔助文字
this.helperStyle,
this.helperMaxLines,
this.hintText, //輔助文字
this.hintStyle, //輔助文字的樣式
this.hintTextDirection,
this.hintMaxLines,
this.errorText, //錯誤訊息提示
this.errorStyle,
this.errorMaxLines,
this.floatingLabelBehavior,
this.floatingLabelAlignment,
this.isCollapsed = false,
this.isDense,
this.contentPadding, //文字內距
this.prefixIcon, //文本字的小圖 prefixIcon: Icon(Icons.
add)
this.prefixIconConstraints,
this.prefix, //於文本前加用通用組件 prefix: CircularProgressIndicator(),
this.prefixText,
this.prefixStyle,
this.prefixIconColor,
this.suffixIcon, //於文本後加入小圖
this.suffix, //於文本前加用通用組件 suffix: CircularProgressIndicator(),
this.suffixText, //於文本後加入文字
this.suffixStyle,
this.suffixIconColor,
this.suffixIconConstraints,
this.counter, //位於輸入方右下方的小控件
this.counterText,
this.counterStyle,
this.filled, //是否要填充顏色
this.fillColor, //填充的顏色,上為ture時才會填充
this.focusColor,
this.hoverColor,
this.errorBorder,
this.focusedBorder,
this.focusedErrorBorder,
this.disabledBorder,
this.enabledBorder, //是否能使用邊框
this.border, //邊框設定
this.enabled = true, //輸入框是否可使用
this.semanticCounterText,
this.alignLabelWithHint,
this.constraints,


範例:

import 'package:flutter/material.dart';

void main() {
runApp(const MyApp());
}

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

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

TextEditingController controller1 = TextEditingController(); //宣告文字控件,之後可利用控件取出該部件的文字


@override
void initState() {
super.initState();
var controller_text=controller1.text;

controller1.addListener(() {
//裡面可寫要對這個文字控件執行的工作


}); //程式初始化時對controller1監聽,只要有變化就執行
}


@override
void dispose() {
super.dispose();
controller1.dispose(); //結束時,釋放controller1記憶體
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: controller1,
//用於控制鍵盤右下角送出那個鍵的樣式,默認為是TextInputAction.done,尚有"go""send" "search" "next" "previous"等。
textInputAction: TextInputAction.done,
//輸入時的文字樣式 .characters輸入時全部大寫文字 .nono沒有限制 .senence 句子首字大寫 .word 每個單詞首字大寫
textCapitalization: TextCapitalization.words,
//文本對齊的方式,在輸入框的那個部位 .start開始 .justify平均分散 .left左邊 .right 右邊 .end 結束
textAlign: TextAlign.left,
//鍵盤輸入限制類型 .number .text .phone .datetime .emailAdress .url
keyboardType: TextInputType.number,

//文字的樣式,大小顏色等。
style: TextStyle(color: Colors.blue, fontWeight: FontWeight.w500),

//光標相關設定
cursorColor: Colors.blue,
//光標顏色
cursorRadius: Radius.circular(16.0),
//光標邊角形狀
cursorWidth: 16.0,
//光標寬度
cursorHeight: 8.0,
//光標長度

maxLength: 99,
//最多的字數
maxLines: 4,
//最多有幾行 ,null是沒有限制
minLines: 1,
//需與maxLines共同使用

//以星號代表輸入文字,設定此為ture時,maxLines不能超過一行
//obscureText: true,

autofocus: true,
//自動選擇焦點
autocorrect: true,
//是否自動校正文字
//點擊時回調
onTap: () {},
//改變時回調
onChanged: (v) {},
//完成時回調,完成文字
onSubmitted: (v) {},
//輸入時回調
onEditingComplete: () {},
//裝飾器
decoration: InputDecoration(
labelText: "測試輸入",
hintText: "預設文字",
//放入輸入框之前 abc
icon: Icon(Icons.abc_outlined),
//於輸入框內的最前面 人像
prefixIcon: Icon(Icons.person),
//於輸入框的文字
prefixText: '姓名:',
//suffixIcon: Icon(Icons.tab,),
suffixIcon: IconButton(
icon: Icon(Icons.abc),
onPressed: () {},
),
//於輸入框後的小圖,使用IconButton便可以使用onpressed屬性
suffixText: '321', //放於輸入框後的文字
helperText: 'help',
//輸入框下的協助文字
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(16.0)), //改成有圓角的輸入框
), //加入邊框
//counter: Text("自定義計數 0/maxLength"), //右下角的計算器,不設定會計算剩餘字數
),
),
],
),
),
);
}
}






參考文件:

Flutter 65: 图解基本 TextField 文本输入框 (二)




errorText文字的設置方法




Flutter學習-9-5 Slider和RangeSlider

 一、slider滑動軸。

slider是滑動軸,在使用時可以利用滑動方式取值,可以再利用這取出的值,操控諸如音樂播放器等元素。Slider的屬性及方法如下:

const Slider({
Key? key,
required this.value, //Slider的值,這裡的值會對應到滑動軸上的位置
required this.onChanged,// 改變時的回調
this.onChangeStart, //在剛點下去要做的回調
this.onChangeEnd, //在點選結束後要做的回調
this.min = 0.0, //可設定slider的最小值
this.max = 1.0, //可設定slider的最大值
this.divisions, //每一格滑動的區間
this.label, //在滑動軸要顯現的文字,
this.activeColor, //已滑過的軌道顏色
this.inactiveColor, //未滑過的軌道顏色
this.thumbColor,
this.mouseCursor,
this.semanticFormatterCallback,
this.focusNode,
this.autofocus = false,

範例:

宣告value

double _rangeValues2=0;

程式

Slider(
value: _rangeValues2,
min: 0, // 範圍最小值
max: 100, // 範圍最大值
// 每次滑動的刻度
divisions: 10,
onChangeStart: (value){ //滑動開始時
print('滑動開始的$value');
},
onChangeEnd: (value){ //滑動開始時
print('滑動結束的$value');
},
label: "現在的數值是$_rangeValues2", // 文字標籤
activeColor: Colors.blue, //已滑過的軌道顏色
inactiveColor: Colors.grey, //未滑過的軌道顏色
onChanged: (value){ // 滑動時的回調
setState(() {
print('滑動中的$value');
_rangeValues2 = value;
});
},
),


二、RangeSlider(有起始點和結束點的滑動軸)

參數跟Slider基本相同。在宣告value,要給一個範圍值,前面的是最小的數值,後面最大的數輦。

RangeSlider({
Key? key,
required this.values,
required this.onChanged,
this.onChangeStart,
this.onChangeEnd,
this.min = 0.0,
this.max = 1.0,
this.divisions,
this.labels,
this.activeColor,
this.inactiveColor,
this.semanticFormatterCallback,

範例:

宣告value方法
RangeValues _rangeValues = RangeValues(0, 100);
程式寫法
RangeSlider(
values: _rangeValues,
max: 100,
//最大值
divisions: 10,
//間隔
labels: RangeLabels(
'开始:${_rangeValues.start}', '结束:${_rangeValues.end}'),
//區間範圍的文字說明
onChanged: (value) {
setState(() {
_rangeValues = value;
});
},
),



上面提供了兩種方法,只是大部分人在使用時,都會想要設計更適合程式需要的滑動軸畫面,顯然上面提供的方法,不足以達到這個目的。因此就有了SliderTheme,以便自己客製化設計出精美的滑動軸。SliderTheme的屬性及方法有下列兩項:
Key? key,
required this.data,
required Widget child,

data是調整Theme的屬性,而child跟上面一樣,是主要設計slider的地方。

track 指軌道,tumbe指姆指按下要拉動的控點整體(滑塊),TickMark(刻度線)
ValueIndicator為包裹說明文字的容器。

const SliderThemeData({
this.trackHeight, //滑動軸的高度
this.activeTrackColor, //已執行過滑動軌道顏色
this.inactiveTrackColor, //未執行過滑動軌道顏色
this.disabledActiveTrackColor, //onchanged value值等於null時,已執行過滑動的軌道顏色
this.disabledInactiveTrackColor, //onChanged value值等於null時,未執行過滑動的軌道顏色
this.activeTickMarkColor, //滑動過部分刻度線的顏色
this.inactiveTickMarkColor, //未滑動過部分刻度線的顏色
this.disabledActiveTickMarkColor, //null時,滑動部分,刻度線的顏色
this.disabledInactiveTickMarkColor, //null時,未滑動部分,刻度線的顏色
this.thumbColor, //未滑動時滑塊的顏色
this.overlappingShapeStrokeColor, //滑動時周邊的繪製色
this.disabledThumbColor, //null時,滑塊的顏色
this.overlayColor, //按下去,滑塊周邊的顏色
this.valueIndicatorColor, //顯示數劇那個氣泡的顏色
this.overlayShape, //控制點的形狀
this.tickMarkShape, //刻度線的形狀
this.thumbShape, //滑塊的形狀
this.trackShape, //軌道的形狀
this.valueIndicatorShape, ////顯示數劇那個氣泡的形狀
this.rangeTickMarkShape, //刻度範圍形狀
this.rangeThumbShape, //滑塊範圍形狀
this.rangeTrackShape, //軌道範圍形狀
this.rangeValueIndicatorShape, //
this.showValueIndicator, //顯示氣泡方式
this.valueIndicatorTextStyle,
this.minThumbSeparation, //RangeSlider 的 start 和 end 最小間隔
this.thumbSelector,
this.mouseCursor,
});

範例:
SliderTheme(
data: SliderTheme.of(context).copyWith(
//.copywith為針對原有部件做部份的修正,這裡是針對SliderTheme做修正
trackHeight: 6,
// 軌道高度
activeTrackColor: Colors.yellowAccent,
// 已滑動的軌道顏色
inactiveTrackColor: Colors.blue,
// 整個軌道的顏色(不包括已滑動的部分)
thumbColor: Colors.green,
// 整個拉動控制點的顏色
activeTickMarkColor: Colors.brown,
// 刻度的顏色
overlappingShapeStrokeColor: Colors.black,
//重疊部份邊線的顏色

trackShape: RectangularSliderTrackShape(),
// 軌道形状,這裡是方形
thumbShape: RoundSliderThumbShape(
// 滑動控制點的形狀,這裡是圓形
enabledThumbRadius: 30 // 圓形的半徑
),

overlayColor: Colors.purpleAccent,
// 滑動控制點外圈的顏色
overlayShape: RoundSliderOverlayShape(
// 滑動控制點的形狀
overlayRadius: 40, // 滑動控制點大小
),

valueIndicatorShape: PaddleSliderValueIndicatorShape(),
// 包著顯示的標籤形狀
valueIndicatorColor: Colors.greenAccent,
showValueIndicator:ShowValueIndicator.onlyForContinuous, //顯示氣泡的方式

),
child: Slider(
value: _Slidervalue,
min: 0,
max: 100,
divisions: 5,
label: '$_Slidervalue',
onChanged: (v){
setState(() {
_Slidervalue = v;
});
},
),
),



參考:https://juejin.cn/post/6959703051586240549

2022年8月3日 星期三

Flutter學習-9-3 checkbox_grouped 1.8.1 套件

 套件庫的下載地點:

https://pub.dev/packages/checkbox_grouped

SimpleGroupedCheckbox

一、宣告 GroupController

GroupController controller = GroupController();

屬性及方法說明:

(1)isMultipleSelection 加上變為可複選,預設為false為單選。
GroupController controller = GroupController(isMultipleSelection: true);

其它:
controller.initSelectedItem;  //初始化,不做也會自動初始化
selectedItems = controller.selectedItem; //設定selectedItems此變數,為取得的項目
//controller.enableAll(); 啟用項目
//controller.disableAll(); //禁用項目


二、寫出程式

SimpleGroupedCheckbox<int>(
controller: controller, //設定此SimpleGroupedCheckbox的控制器
itemsTitle: ["1" ,"2","3","4","5"], //文字選項,每個需不同,若相同時會出錯
itemsSubTitle: ["1","2","3","",""], //小標,需與上面的itemsTitle數目對應
values: [1,2,3,4,4], //選擇後得到的值,需與上面的itemsTitle數目對應
checkFirstElement: false,
groupTitle: "測試的標題",//grouptitle標題
helperGroupTitle:true, //grouptitle標題前會有一個框,點選此框可以全部選擇
groupTitleAlignment:Alignment.bottomLeft, //標題要對齊那裡
//用戶選擇時,執行下列的回調程式
onItemSelected: (v){ //獲得的值為一個陣列
print('1');
print(v); //取得陣列中的第一個值
print(v[1]); //取得陣列中的第一個值
//print('2');
//print(selectedItems[1]); //取得陣列中的第一個值
},
groupStyle: GroupStyle( //基礎相關於此groupcheckbox的文字設定
activeColor: Colors.red,
itemTitleStyle: TextStyle(
fontSize: 13
)
),

)