IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> flutter之组件 -> 正文阅读

[移动开发]flutter之组件

MaterialApp

表示一个应用了 Material 界面风格的应用程序,它封装了应用程序实现 Material Design 所需要的一些widget

MaterialApp(
  // 指定应用程序在任务栏上显示的标题
  title: 'Flutter初体验',
  // 指定应用程序的主界面
  home: Text('aaa'),
  // 配置应用程序的主题
  theme: ThemeData(primarySwatch: Colors.red),
)

Text
用于在页面上渲染普通文本字符串

Text('文本内容', style: TextStyle(fontSize: 12))

Scaffold
该组件是页面结构的脚手架,包含了页面的基本组成单元

  • appBar - 头部导航条区域
  • body - 页面主题内容区域
  • drawer - 侧边栏抽屉区域
  • bottomNavigationBar - 底部tabBar区域
  • floatingActionButton - 右下角浮动按钮区域

Button

ElevatedButton(
	child: Text('PRESS'),
    onPressed:buttonPress,
),
TextButton( // FlatButton
   child: Text('PRESS AGAIN'),
   onPressed: buttonPress,
),
OutlinedButton(
   child: Text('Press this'),
   onPressed: buttonPress,
),
 ///----------------------------------
 void buttonPress() {
  print(
      'The button pressed, the value of text box is: ${myUserNameController.text} \n '
          'password is ${myUserPasswordController.text}');
}

Image

Image(
	image: AssetImage('images/100.jpg'),//本地图片,需要修改配置文件
),

///----------------------------------pubspec.yaml
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #   - images/a_dot_burr.jpeg
  #   - images/a_dot_ham.jpeg
  ///----------------------------------
Image(
	image:NetworkImage('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png',
      	), //网络图
),

输入框

TextField(
	textAlign: TextAlign.left,   //左对齐
	decoration: InputDecoration(labelText: '用户名称',hintText: '用户名称不能为空'),
),

实例代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

TextEditingController myUserNameController = TextEditingController();
TextEditingController myUserPasswordController = TextEditingController();

void main() => runApp(MyNewApp());

/// 组件/
class MyNewApp extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // 返回组件的状态,用来交互
    return MyNewAppState();
  }
}
/// 状态/
class MyNewAppState extends State<MyNewApp>{
  @override
  Widget build(BuildContext context) {
    debugPrint(">>> buildState...");
    return MaterialApp(
      title: 'This is title,will not display on Android phone',
      home: Scaffold(
        appBar: AppBar(
          title: Text('This is made by Flutter with Dart'),
          elevation: 10.0,
          actions: <Widget>[
            IconButton(onPressed: buttonPress, icon: Icon(Icons.add_alert))
          ],
          leading: IconButton(
            onPressed: buttonPress,
            icon: Icon(Icons.arrow_back),
          ),
        ),
        body: Center(
          ///----------------------------------按钮
          // child: ElevatedButton(
          //   child: Text('PRESS'),
          //   onPressed:buttonPress,
          // ),
          ///----------------------------------按钮
          // child: TextButton( // FlatButton
          //   child: Text('PRESS AGAIN'),
          //   onPressed: buttonPress,
          // ),
          ///----------------------------------按钮
          // child: OutlinedButton(
          //   child: Text('Press this'),
          //   onPressed: buttonPress,
          // ),
          ///----------------------------------图片
          // child: Image(
          //   // image: AssetImage('images/100.jpg'),
          //    image: NetworkImage('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png',
          //    ),
          // ),
          ///----------------------------------输入框
          // child: TextField(
          //   textAlign: TextAlign.left,   //左对齐
          //   decoration: InputDecoration(labelText: '用户名称',hintText: '用户名称不能为空'),
          // ),
          ///----------------------------------
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextField(
                controller: myUserNameController,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.person),
                  labelText: '用户名称',
                  hintText: '用户名不能为空',
                ),
              ),
              TextField(
                obscureText: true,
                controller: myUserPasswordController,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.lock),
                  labelText: '密码',
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  ElevatedButton(
                    child: Text('登录'),
                    onPressed: buttonPress,
                  ),
                  TextButton(
                    child: Text('取消'),
                    onPressed: buttonPress,
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

  ///状态的初始化
  @override
  void initState(){
    debugPrint(">>> initState...");
    super.initState();
  }

  /// 重构组件
  @override
  void didUpdateWidget(MyNewApp mna){
    debugPrint(">>> didUpdateWidget...");
    super.didUpdateWidget(mna);
  }

  ///组件为非活动状态
  @override void deactivate() {
    debugPrint(">>> deactivate");
    super.deactivate();
  }
  ///当组件被销毁
  @override
  void dispose(){
    super.dispose();
  }


}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'This is title,will not display on Android phone',
      home: Scaffold(
        appBar: AppBar(
          title: Text('This is made by Flutter with Dart'),
          elevation: 10.0,
          actions: <Widget>[
            IconButton(onPressed: buttonPress, icon: Icon(Icons.add_alert))
          ],
          leading: IconButton(
            onPressed: buttonPress,
            icon: Icon(Icons.arrow_back),
          ),
        ),
        body: Center(
          ///----------------------------------
          // child: ElevatedButton(
          //   child: Text('PRESS'),
          //   onPressed:buttonPress,
          // ),
          ///----------------------------------
          // child: TextButton( // FlatButton
          //   child: Text('PRESS AGAIN'),
          //   onPressed: buttonPress,
          // ),
          ///----------------------------------
          // child: OutlinedButton(
          //   child: Text('Press this'),
          //   onPressed: buttonPress,
          // ),
          ///----------------------------------
          // child: Image(
          //   // image: AssetImage('images/100.jpg'),
          //    image: NetworkImage('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png',
          //    ),
          // ),
          ///----------------------------------
          // child: TextField(
          //   textAlign: TextAlign.left,
          //   decoration: InputDecoration(labelText: '用户名称',hintText: '用户名称不能为空'),
          // ),
          ///----------------------------------
          child: Column(
            children: <Widget>[
              TextField(
                controller: myUserNameController,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.person),
                  labelText: '用户名称',
                  hintText: '用户名不能为空',
                ),
              ),
              TextField(
                obscureText: true,
                controller: myUserPasswordController,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.lock),
                  labelText: '密码',
                ),
              ),
              Row(
                children: <Widget>[
                  ElevatedButton(
                    child: Text('登录'),
                    onPressed: buttonPress,
                  ),
                  TextButton(
                    child: Text('取消'),
                    onPressed: buttonPress,
                  )
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

void buttonPress() {
  print(
      'The button pressed, the value of text box is: ${myUserNameController.text} \n '
          'password is ${myUserPasswordController.text}');
}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-01 14:37:31  更:2021-08-01 14:38:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/7 2:34:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码