- Flutter (Dart)etym.cn
Flutter 是一个用于构建跨平台移动应用的框架,使用 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),
),
);
}
}
2. React Native (JavaScript/TypeScript)
React Native 是一个用于构建原生应用的 JavaScript 框架。
jsx
import React from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
class App extends React.Component {
state = { count: 0 };
increment = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
text: {
fontSize: 20,
textAlign: ‘center’,
margin: 10,
},
});
export default App;
3. Kotlin (Android)
Kotlin 是 Android 开发的一种流行语言。
kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private lateinit var counterTextView: TextView
private lateinit var incrementButton: Button
private var count = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
counterTextView = findViewById(R.id.counterTextView)
incrementButton = findViewById(R.id.incrementButton)
incrementButton.setOnClickListener {
count++
counterTextView.text = "You have pushed the button $count times"
}
}
}
(注意:这里假设你已经有一个名为 activity_main.xml 的布局文件,其中包含了 counterTextView 和 incrementButton 的定义。)
- Swift (iOS)
Swift 是 iOS 开发的主要语言。
swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var counterLabel: UILabel!
var count =
由于生成一个完整的App代码涉及多个方面(如前端、后端、数据库、用户界面设计等),且不同的平台和框架有其特定的语法和工具链,因此在这里我将简要地给出用几种不同语言和技术栈创建App的示例代码或框架。
- Flutter (Dart)
Flutter 是一个用于构建跨平台移动应用的框架,使用 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),
),
);
}
}
2. React Native (JavaScript/TypeScript)
React Native 是一个用于构建原生应用的 JavaScript 框架。
jsx
import React from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
class App extends React.Component {
state = { count: 0 };
increment = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
text: {
fontSize: 20,
textAlign: ‘center’,
margin: 10,
},
});
export default App;
3. Kotlin (Android)
Kotlin 是 Android 开发的一种流行语言。
kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private lateinit var counterTextView: TextView
private lateinit var incrementButton: Button
private var count = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
counterTextView = findViewById(R.id.counterTextView)
incrementButton = findViewById(R.id.incrementButton)
incrementButton.setOnClickListener {
count++
counterTextView.text = "You have pushed the button $count times"
}
}
}
(注意:这里假设你已经有一个名为 activity_main.xml 的布局文件,其中包含了 counterTextView 和 incrementButton 的定义。)
- Swift (iOS)
Swift 是 iOS 开发的主要语言。
swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var counterLabel: UILabel!
var count =