【Flutter】StatefulWidget 组件 ( Image 组件 | TextField 组件 )

网友投稿 545 2022-05-30

文章目录

一、Image 组件

二、TextField 组件

三、 相关资源

一、Image 组件

Image 组件有多个命名构造函数 , 可以从 文件 / 内存 / 网络 / Assets 中加载文件 , 分别对应不同的构造函数 ;

class Image extends StatefulWidget { // 从网络中加载图片的构造函数 Image.network( // 图片的网络地址 String src, { Key key, double scale = 1.0, this.frameBuilder, this.loadingBuilder, this.errorBuilder, this.semanticLabel, this.excludeFromSemantics = false, this.width, // 组件宽度 this.height, // 组件高度 this.color, this.colorBlendMode, this.fit, this.alignment = Alignment.center, this.repeat = ImageRepeat.noRepeat, this.centerSlice, this.matchTextDirection = false, this.gaplessPlayback = false, this.filterQuality = FilterQuality.low, this.isAntiAlias = false, Map headers, int cacheWidth, int cacheHeight, }) : image = ResizeImage.resizeIfNeeded(cacheWidth, cacheHeight, NetworkImage(src, scale: scale, headers: headers)), assert(alignment != null), assert(repeat != null), assert(matchTextDirection != null), assert(cacheWidth == null || cacheWidth > 0), assert(cacheHeight == null || cacheHeight > 0), assert(isAntiAlias != null), super(key: key); // 从文件中加载图片的构造函数 Image.file( File file, { Key key, double scale = 1.0, this.frameBuilder, this.errorBuilder, this.semanticLabel, this.excludeFromSemantics = false, this.width, this.height, this.color, this.colorBlendMode, this.fit, this.alignment = Alignment.center, this.repeat = ImageRepeat.noRepeat, this.centerSlice, this.matchTextDirection = false, this.gaplessPlayback = false, this.isAntiAlias = false, this.filterQuality = FilterQuality.low, int cacheWidth, int cacheHeight, }) : image = ResizeImage.resizeIfNeeded(cacheWidth, cacheHeight, FileImage(file, scale: scale)), loadingBuilder = null, assert(alignment != null), assert(repeat != null), assert(filterQuality != null), assert(matchTextDirection != null), assert(cacheWidth == null || cacheWidth > 0), assert(cacheHeight == null || cacheHeight > 0), assert(isAntiAlias != null), super(key: key); // 从 Assets 资源文件中加载图片 Image.asset() // 从内存中加载图片的构造函数 Image.memory() }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

图片组件使用示例 :

// 图片组件 , 从网络中加载一张图片 Image.network( // 图片地址 "https://img-blog.csdnimg.cn/20210228180808133.png", // 图片宽度 width: 200, // 图片高度 height: 200, ),

1

2

3

4

5

6

7

8

9

完整代码示例 :

import 'package:flutter/material.dart'; class StatefulWidgetPage extends StatefulWidget { @override _StatefulWidgetPageState createState() => _StatefulWidgetPageState(); } class _StatefulWidgetPageState extends State { /// 当前被选中的底部导航栏索引 int _currentSelectedIndex = 0; // This widget is the root of your application. @override Widget build(BuildContext context) { // 文本组件样式 , 可以设置给 Text 文本组件 // 设置字体大小 20, 颜色红色 TextStyle textStyle = TextStyle(fontSize: 20, color: Colors.red); return MaterialApp( title: 'StatefulWidgetPage 组件示例', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( // 顶部标题栏 appBar: AppBar(title: Text('StatefulWidgetPage 组件示例'),), // 底部导航栏 BottomNavigationBar 设置 // items 可以设置多个 BottomNavigationBarItem bottomNavigationBar: BottomNavigationBar( // 设置当前选中的底部导航索引 currentIndex: _currentSelectedIndex, // 设置点击底部导航栏的回调事件 , index 参数是点击的索引值 onTap: (index){ // 回调 StatefulWidget 组件的 setState 设置状态的方法 , 修改当前选中索引 // 之后 BottomNavigationBar 组件会自动更新当前选中的选项卡 setState(() { // 改变 int _currentSelectedIndex 变量的状态 _currentSelectedIndex = index; }); }, // 条目 items: [ // 设置底部导航栏条目, 每个条目可以设置一个图标 BottomNavigationBarItem( // 默认状态下的图标 icon: Icon(Icons.home, color: Colors.grey,), // 激活状态下的图标 activeIcon: Icon(Icons.home, color: Colors.red,), // 设置标题 title: Text("主页") ), // 设置底部导航栏条目, 每个条目可以设置一个图标 BottomNavigationBarItem( // 默认状态下的图标 icon: Icon(Icons.settings, color: Colors.grey,), // 激活状态下的图标 activeIcon: Icon(Icons.settings, color: Colors.red,), // 设置标题 title: Text("设置") ) ],), // 设置悬浮按钮 floatingActionButton: FloatingActionButton( onPressed: (){ print("悬浮按钮点击"); }, child: Text("悬浮按钮组件"), ), // Container 容器使用 body: _currentSelectedIndex == 0 ? // 刷新指示器组件 RefreshIndicator( // 显示的内容 child: ListView( children: [ Container( // 对应底部导航栏设置选项卡 // 设置容器的装饰器 , BoxDecoration 是最常用的装饰器 // 可以自行查看 BoxDecoration 中可以设置的属性 decoration: BoxDecoration(color: Colors.white), // 设置 child 子组件居中方式, 居中放置 alignment: Alignment.center, // 子组件, 子组件设置为一个 Column 组件 child: Column( // Column 子组件, 这里设置 Text 文本组件 children: [ Text("主页面选项卡, 下拉刷新"), // 图片组件 , 从网络中加载一张图片 Image.network( // 图片地址 "https://img-blog.csdnimg.cn/20210228180808133.png", // 图片宽度 width: 200, // 图片高度 height: 200, ), ], ), ), ], ), // 刷新时回调的方法 // 列表发生下拉操作时, 回调该方法 // 该回调是 Future 类型的 onRefresh: _refreshIndicatorOnRefresh, ) : Container( // 对应底部导航栏设置选项卡 // 设置容器的装饰器 , BoxDecoration 是最常用的装饰器 // 可以自行查看 BoxDecoration 中可以设置的属性 decoration: BoxDecoration(color: Colors.white), // 设置 child 子组件居中方式, 居中放置 alignment: Alignment.center, // 子组件, 子组件设置为一个 Column 组件 child: Column( // Column 子组件, 这里设置 Text 文本组件 children: [ Text("设置页面选项卡") ], ), ) , // 该设置与 _currentSelectedIndex == 0? 相对应, ?: 三目运算符 ), ); } /// RefreshIndicator 发生下拉操作时, 回调该方法 /// 该方啊是一个异步方法 , 在方法体前添加 async 关键字 Future _refreshIndicatorOnRefresh() async{ // 暂停 500 ms , 使用 await 关键字实现 // 在这 500 ms 之间 , 列表处于刷新状态 // 500 ms 之后 , 列表变为非刷新状态 await Future.delayed(Duration(milliseconds: 500)); return null; } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

运行效果展示 :

二、TextField 组件

TextField 组件构造函数的可选参数 : 下面代码中的可选参数就是 TextField 组件可以设置的参数选项 ;

class TextField extends StatefulWidget { const TextField({ Key key, this.controller, this.focusNode, this.decoration = const InputDecoration(), TextInputType keyboardType, this.textInputAction, 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.maxLengthEnforced = true, 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, this.enableInteractiveSelection = true, this.onTap, this.mouseCursor, this.buildCounter, this.scrollController, this.scrollPhysics, this.autofillHints, this.restorationId, }) }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

输入框组件代码示例 :

// 输入框组件 TextField( // 设置输入框样式 decoration: InputDecoration( // 设置内容边距, 左右边距为 10, 上下边距为 0 contentPadding: EdgeInsets.fromLTRB(10, 0, 10, 0), // 设置的提示文案信息 hintText: "提示信息", // 设置提示文案样式 hintStyle: TextStyle(fontSize: 20, color: Colors.grey), ),

1

2

3

4

5

6

7

8

9

10

11

完整代码示例 :

import 'package:flutter/material.dart'; class StatefulWidgetPage extends StatefulWidget { @override _StatefulWidgetPageState createState() => _StatefulWidgetPageState(); } class _StatefulWidgetPageState extends State { /// 当前被选中的底部导航栏索引 int _currentSelectedIndex = 0; // This widget is the root of your application. @override Widget build(BuildContext context) { // 文本组件样式 , 可以设置给 Text 文本组件 // 设置字体大小 20, 颜色红色 TextStyle textStyle = TextStyle(fontSize: 20, color: Colors.red); return MaterialApp( title: 'StatefulWidgetPage 组件示例', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( // 顶部标题栏 appBar: AppBar(title: Text('StatefulWidgetPage 组件示例'),), // 底部导航栏 BottomNavigationBar 设置 // items 可以设置多个 BottomNavigationBarItem bottomNavigationBar: BottomNavigationBar( // 设置当前选中的底部导航索引 currentIndex: _currentSelectedIndex, // 设置点击底部导航栏的回调事件 , index 参数是点击的索引值 onTap: (index){ // 回调 StatefulWidget 组件的 setState 设置状态的方法 , 修改当前选中索引 // 之后 BottomNavigationBar 组件会自动更新当前选中的选项卡 setState(() { // 改变 int _currentSelectedIndex 变量的状态 _currentSelectedIndex = index; }); }, // 条目 items: [ // 设置底部导航栏条目, 每个条目可以设置一个图标 BottomNavigationBarItem( // 默认状态下的图标 icon: Icon(Icons.home, color: Colors.grey,), // 激活状态下的图标 activeIcon: Icon(Icons.home, color: Colors.red,), // 设置标题 title: Text("主页") ), // 设置底部导航栏条目, 每个条目可以设置一个图标 BottomNavigationBarItem( // 默认状态下的图标 icon: Icon(Icons.settings, color: Colors.grey,), // 激活状态下的图标 activeIcon: Icon(Icons.settings, color: Colors.red,), // 设置标题 title: Text("设置") ) ],), // 设置悬浮按钮 floatingActionButton: FloatingActionButton( onPressed: (){ print("悬浮按钮点击"); }, child: Text("悬浮按钮组件"), ), // Container 容器使用 body: _currentSelectedIndex == 0 ? // 刷新指示器组件 RefreshIndicator( // 显示的内容 child: ListView( children: [ Container( // 对应底部导航栏设置选项卡 // 设置容器的装饰器 , BoxDecoration 是最常用的装饰器 // 可以自行查看 BoxDecoration 中可以设置的属性 decoration: BoxDecoration(color: Colors.white), // 设置 child 子组件居中方式, 居中放置 alignment: Alignment.center, // 子组件, 子组件设置为一个 Column 组件 child: Column( // Column 子组件, 这里设置 Text 文本组件 children: [ Text("主页面选项卡, 下拉刷新"), // 图片组件 , 从网络中加载一张图片 Image.network( // 图片地址 "https://img-blog.csdnimg.cn/20210228180808133.png", // 图片宽度 width: 200, // 图片高度 height: 200, ), // 输入框组件 TextField( // 设置输入框样式 decoration: InputDecoration( // 设置内容边距, 左右边距为 10, 上下边距为 0 contentPadding: EdgeInsets.fromLTRB(10, 0, 10, 0), // 设置的提示文案信息 hintText: "提示信息", // 设置提示文案样式 hintStyle: TextStyle(fontSize: 20, color: Colors.grey), ), ) ], ), ), ], ), // 刷新时回调的方法 // 列表发生下拉操作时, 回调该方法 // 该回调是 Future 类型的 onRefresh: _refreshIndicatorOnRefresh, ) : Container( // 对应底部导航栏设置选项卡 // 设置容器的装饰器 , BoxDecoration 是最常用的装饰器 // 可以自行查看 BoxDecoration 中可以设置的属性 decoration: BoxDecoration(color: Colors.white), // 设置 child 子组件居中方式, 居中放置 alignment: Alignment.center, // 子组件, 子组件设置为一个 Column 组件 child: Column( // Column 子组件, 这里设置 Text 文本组件 children: [ Text("设置页面选项卡") ], ), ) , // 该设置与 _currentSelectedIndex == 0? 相对应, ?: 三目运算符 ), ); } /// RefreshIndicator 发生下拉操作时, 回调该方法 /// 该方啊是一个异步方法 , 在方法体前添加 async 关键字 Future _refreshIndicatorOnRefresh() async{ // 暂停 500 ms , 使用 await 关键字实现 // 在这 500 ms 之间 , 列表处于刷新状态 // 500 ms 之后 , 列表变为非刷新状态 await Future.delayed(Duration(milliseconds: 500)); return null; } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

【Flutter】StatefulWidget 组件 ( Image 组件 | TextField 组件 )

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

运行效果展示 :

三、 相关资源

参考资料 :

flutter 官网 : https://flutter.dev/

Flutter 开发文档 : https://flutter.cn/docs ( 强烈推荐 )

官方 GitHub 地址 : https://github.com/flutter

Flutter 中文社区 : https://flutter.cn/

Flutter 实用教程 : https://flutter.cn/docs/cookbook

Flutter CodeLab : https://codelabs.flutter-io.cn/

Dart 中文文档 : https://dart.cn/

Dart 开发者官网 : https://api.dart.dev/

Flutter 中文网 ( 非官方 , 翻译的很好 ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/

Flutter 相关问题 : https://flutterchina.club/faq/ ( 入门阶段推荐看一遍 )

博客源码下载 :

GitHub 地址 : https://github.com/han1202012/flutter_cmd ( 随博客进度一直更新 , 有可能没有本博客的源码 )

博客源码快照 : https://download.csdn.net/download/han1202012/15500147 ( 本篇博客的源码快照 , 可以找到本博客的源码 )

Flutter Image

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:android FragmentPagerAdapter getItem方法没有执行
下一篇:[HTTP][会话与消息][三][学习笔记]
相关文章