flutter - 如何在 flutter 中管理开关小部件的 native 外观

标签 flutter dart flutter-layout

我想在 Flutter/Dart 中使用不同操作系统的 native 外观创建 switch/toggle 小部件。我的意思是,如果我在 iOS 移动设备上运行,它应该是这样的:

enter image description here

如果我在 Android 手机上运行,​​它应该是这样的:

enter image description here

帮我在flutter/dart中实现这种类型的功能

最佳答案

使用 Switch.adaptive

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


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

示例代码:
bool _value = true;

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

关于flutter - 如何在 flutter 中管理开关小部件的 native 外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58319811/

相关文章:

java - Flutter android 应用程序在签名发布后尝试调试时崩溃

flutter - 相对于其他小部件更改文本的大小/位置

flutter - 如何在 flutter 中使用自定义剪辑器制作弯曲的应用程序栏

dart - 如何在 flutter 中更改分页数据表的颜色?

firebase - 如何在flutter中将数据从CSV文件导入数据库

flutter - 如何更改 flutter 中的文本选择选项?

flutter - 如何在没有脚手架的情况下显示 snackBar

flutter - 何时在 Flutter 中使用 FutureBuilder

dart - 来自dart2js检查模式的不清楚错误

flutter - 堆叠在listview flutter 无限高度