SwiftUI,是FlutterUI该有的样子吗?

2019-06-11 12:02:17 浏览数 (1)

刚刚的【2019 WWDC 大会】苹果发布了 SwiftUI 开发语言,咋看上去,和Flutter的UI比较像,但个人感觉比Flutter的方式更好——虽然有些地方比较奇怪。有兴趣的开发人员可以试着按链接操作一下:

https://developer.apple.com/tutorials/swiftui/

Flutter的写法:

代码语言:javascript复制
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    var stack = new Stack(
      alignment: const Alignment(0.6, 0.6),
      children: [
        new CircleAvatar(
          backgroundImage: new AssetImage('images/pic.jpg'),
          radius: 100.0,
        ),
        new Container(
          decoration: new BoxDecoration(
            color: Colors.black45,
          ),
          child: new Text(
            'Mia B',
            style: new TextStyle(
              fontSize: 20.0,
              fontWeight: FontWeight.bold,
              color: Colors.white,
            ),
          ),
        ),
      ],
    );
  }
}

SwiftUI的写法:

代码语言:javascript复制
struct ContentView: View {
    var body: some View {
        VStack {
            VStack(alignment: .leading) {
                Text("Turtle Rock")
                    .font(.title)
                HStack(alignment: .top) {
                    Text("Joshua Tree National Park")
                        .font(.subheadline)
                    Spacer()
                    Text("California")
                        .font(.subheadline)
                }
            }
            .padding()
        }
    }
}

没有child/children的困惑、没有了括号地狱,也不用把装饰器等不像widget的东西硬要变成widget使用,感觉肉眼终于能较好分辨出UI结构了,有股云雾散开的感觉……

0 人点赞