Flutter (Dart)timezf.cn
Flutter是一个用于构建跨平台APP的框架,使用Dart语言。
dart
import ‘package:flutter/material.dart’;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
‘You have pushed the button this many times:’,
),
Text(
‘$_counter’,
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: ‘Increment’,
child: Icon(Icons.add),
),
);
}
}
React Native (JavaScript/TypeScript)
React Native用于构建跨平台移动APP,使用JavaScript或TypeScript。
jsx
import React from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
increment = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
Kotlin (Android)
虽然Kotlin是为Java虚拟机设计的,但它在Android开发中非常受欢迎。
kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private var counter = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
val textView = findViewById<TextView>(R.id.textView)
button.setOnClickListener {
counter++
textView.text = "You have pushed the button this many times: $counter"
}
}
}
注意:对于上述的Kotlin示例,你还需要一个对应的XML布局文件(如activity_main.xml)。
以上示例都是基本的“计数器”APP,用于展示如何在不同的编程语言和框架中设置UI和基本的交互逻辑。要构建一个完整的APP,你还需要考虑很多其他因素,如数据持久化、网络通信、用户认证等。由于创建一个完整的APP代码涉及到很多步骤和组件,这里我会为你展示如何用几种常见的编程语言或框架来“开始”一个简单的APP示例。请注意,这些示例仅用于教学目的,并不能直接编译为一个完整的APP。
Flutter (Dart)
Flutter是一个用于构建跨平台APP的框架,使用Dart语言。
dart
import ‘package:flutter/material.dart’;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
‘You have pushed the button this many times:’,
),
Text(
‘$_counter’,
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: ‘Increment’,
child: Icon(Icons.add),
),
);
}
}
React Native (JavaScript/TypeScript)
React Native用于构建跨平台移动APP,使用JavaScript或TypeScript。
jsx
import React from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
increment = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
Kotlin (Android)
虽然Kotlin是为Java虚拟机设计的,但它在Android开发中非常受欢迎。
kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private var counter = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
val textView = findViewById<TextView>(R.id.textView)
button.setOnClickListener {
counter++
textView.text = "You have pushed the button this many times: $counter"
}
}
}
注意:对于上述的Kotlin示例,你还需要一个对应的XML布局文件(如activity_main.xml)。
以上示例都是基本的“计数器”APP,用于展示如何在不同的编程语言和框架中设置UI和基本的交互逻辑。要构建一个完整的APP,你还需要考虑很多其他因素,如数据持久化、网络通信、用户认证等。