如何在Flutter中管理开关小部件的本机外观 [英] How to manage native looks of switch widget in flutter

查看:50
本文介绍了如何在Flutter中管理开关小部件的本机外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Flutter/Dart 中创建具有不同操作系统本机外观的switch/toggle小部件.

我的意思是,如果我在iOS移动设备上运行,它应该类似于:/p>

如果我在Android移动设备上运行,它应该看起来像:

帮助我在flutter/dart中实现这种功能

解决方案

使用 Switch.adaptive

如果目标平台是iOS,则创建CupertinoSwitch,否则创建材料设计开关.

  Switch.adaptive(值:true,onChanged:(newValue){}) 


示例代码:

  bool _value = true;@override窗口小部件build(BuildContext context){返回脚手架(appBar:AppBar(),正文:Switch.adaptive(值:_value,onChanged:(newValue)=>setState(()=> _value = newValue),),);} 

I want to create switch/toggle widget in Flutter/Dart with native looks of different OS.

I mean if I run on iOS mobile it should be looks like:

If I run on Android mobile it should be looks like :

Help me to achieve this type of functionality in flutter/dart

解决方案

Use Switch.adaptive

Creates a CupertinoSwitch if the target platform is iOS, creates a material design switch otherwise.

Switch.adaptive(value: true, onChanged: (newValue) {})


Sample code:

bool _value = true;

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(),
    body: Switch.adaptive(
      value: _value,
      onChanged: (newValue) => setState(() => _value = newValue),
    ),
  );
}

这篇关于如何在Flutter中管理开关小部件的本机外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆