flutter:功能性组件 (八)

简介: 本文介绍了Flutter中常用的UI组件和功能,包括进度指示器(线性和圆形)、下拉刷新、选项按钮(单选按钮、复选框、开关)、手势识别(GestureDetector、Ink和InkWell)以及提示和Offstage组件的使用方法和示例代码。这些组件和功能可以帮助开发者快速构建交互丰富的应用程序界面。

前言

在现代移动应用开发中,用户界面的设计与交互体验息息相关。Flutter作为一个高效的跨平台框架,提供了多种常用的UI组件与功能,使得开发者能够快速构建交互丰富的应用程序界面。

进度指示器

线性

Widget buildLinearProgress(){
    return Container(
      width: 300,
      height: 10,
      child: LinearProgressIndicator(
        valueColor: new AlwaysStoppedAnimation<Color>(Colors.blue),
        backgroundColor: Color(0xff00ff00),
        minHeight: 10,
      ),
    );
  }

圆形

Widget buildCircularProgress(){
    return Container(
      width: 55,
      height: 55,
      child: const CircularProgressIndicator(
        valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
        backgroundColor: Color(0xff00ff00),
        //   圆圈的宽
        strokeWidth: 6.0,
      ),
    );
}

下拉框刷新

return Scaffold(
      appBar: AppBar(
        title: Text('下拉刷新'),
      ),
      body: RefreshIndicator(
        color: Colors.blue,
        backgroundColor: Colors.grey[200],
        onRefresh: () async {
          await Future.delayed(Duration(milliseconds: 1000));
          return Future.value(true);
        },
        child: SingleChildScrollView(
          child: Container(
            width: 300,
            height: 300,
       ),
        ),
      ),
    );


选项按钮

单选按钮Radio

class MyTest extends StatefulWidget {
  const MyTest({Key? key}) : super(key: key);
  @override
  State<MyTest> createState() => _MyTestState();
}
class _MyTestState extends State<MyTest> {
  int _groupValue=0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("title"),
      ),
        body: Row(
          children: [
            //  给一个形参  用来记录 得到的值
            Radio(value: 0, groupValue: _groupValue, onChanged: (v){
              setState(() {
                this._groupValue=v!;
                print("you choose is boy");
              });
            }),
            Text("boy"),
            Radio(value: 1, groupValue: _groupValue, onChanged: (v){
              setState(() {
                this._groupValue=v!;
                print("you choose is girl");
              });
            }),
            Text("girl"),
          ],
        ),
    );
  }
}

RadioListTitle

复选框

bool checkIsSelect=false;
  Widget buildCheckedBox(){
    return Checkbox(
        value: checkIsSelect,
        activeColor: Colors.red,
        checkColor: Colors.yellow,
        onChanged: (value){
      setState(() {
        checkIsSelect=value!;
        print(checkIsSelect);
      });
    });
  }

加属性为

CheckboxListTitle

bool checkIsSelect=false;
  Widget buildCheckedBox(){
    return CheckboxListTile(
        value: checkIsSelect,
        activeColor: Colors.red,
        checkColor: Colors.yellow,
        title: Text("title"),
    subtitle: Text("sub title"),
        secondary: Icon(Icons.home),
        onChanged: (value){
      setState(() {
        checkIsSelect=value!;
        print(checkIsSelect);
      });
    });
  }


开关switch

bool switchValue = true;
Switch _switch() {
  return Switch(
          activeColor: Colors.red,
      value: switchValue,
      onChanged: (value) {
        switchValue = !switchValue;
        setState(() {
          print(switchValue);
        });
      });
}

用SwitchListTitle  可使用更多的属性

Switch(value: isSelected, onChanged: (value){
              setState(() {
                isSelected=value;
                print(isSelected);
              });
            })


手势识别


Widget  buildGestureDetector(){
    return GestureDetector(
      // 手指抬起来 触发
      onTap: (){
        print('hello');
      },
    child: Container(
      width: 100,
      height: 100,
      child: Image.asset(
        "imgs/laying.jpg"
      ),
    ),
    );
}

ink 和inkwell

Widget test(){
    return new Material(
      //  ink 用来处理 图片
      child:  new Ink(
        width: 300,
        height: 300,
        color: Colors.blue,
        //InkWell  用来处理 回调
        child: new InkWell(
          onTap: (){
            debugPrint("hello");
          },
          child: Container(
            width: 30,
            height: 30,
            alignment: Alignment(0,0),
            child: Text("login",style: TextStyle(color: Colors.white),),
          ),
        ),
      ),
    );
  }


提示

Widget test() {
  return Scaffold(
    body: Center(
      child: Tooltip(
        message: '这是一个提示',
        verticalOffset: 16,
        child: ElevatedButton(
          onPressed: () {
            print("hello");
          },
          child: Text("click me"),
        ),
      ),
    ),
  );
}

offstage

在Flutter中,Offstage是一个用于控制子组件是否显示的小部件。它有一个offstage属性,用于指定子组件是否应该被隐藏。
当offstage属性为true时,子组件将被隐藏,不会被渲染和响应用户交互事件。当offstage属性为false时,子组件将正常显示和响应交互事件。


相关文章
|
22天前
|
传感器 缓存 监控
Stream 组件在 Flutter 中的应用场景有哪些?
Stream 组件在 Flutter 中的应用场景有哪些?
164 58
|
18天前
|
UED 开发者
Flutter|常用数据通信组件
Flutter|常用数据通信组件
96 49
|
1月前
|
开发工具
Flutter-AnimatedWidget组件源码解析
Flutter-AnimatedWidget组件源码解析
157 60
|
22小时前
|
存储 开发框架 开发者
flutter:代码存储&基本组件 (五)
本文档介绍了Flutter中的一些基本组件和代码示例,包括代码存储、基本组件如AppBar的简单使用、可滑动切换的标签栏、TextField的多种用法(如简单使用、登录页面、文本控制器的监听与使用、修饰等),以及如何实现点击空白区域隐藏键盘等功能。通过这些示例,开发者可以快速掌握在Flutter应用中实现常见UI元素的方法。
|
22天前
|
开发者
Flutter|常用数据通信组件
Flutter|常用数据通信组件
|
22天前
Stream 组件在 Flutter 中的具体使用方法是什么?
Stream 组件在 Flutter 中的具体使用方法是什么?
|
1月前
|
容器
Flutter基本组件Text使用
Flutter基本组件Text使用
44 12
|
1月前
|
Dart
Flutter|常用数据通信组件
在做需求时经常会遇到组件间通信,本篇汇总了几种常用的通信方式
23 1
|
2月前
|
开发工具
解决Flutter中ThemeData.primaryColor在AppBar等组件中不生效
解决Flutter中ThemeData.primaryColor在AppBar等组件中不生效
63 1
|
7天前
|
Android开发 iOS开发 容器
鸿蒙harmonyos next flutter混合开发之开发FFI plugin
鸿蒙harmonyos next flutter混合开发之开发FFI plugin