- Flutter (Dart)meimiaomeimo.cn
Flutter 是一个用于构建跨平台移动应用的框架。
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 用于构建原生移动应用。
jsx
import React from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
export default function App() {
const [count, setCount] = React.useState(0);
return (
<Button
title=“Press me”
onPress={() => setCount(count + 1)}
/>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
3. Swift (iOS)
Swift 是 iOS 开发的主要语言。
swift
import UIKit
class ViewController: UIViewController {
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func incrementCounter(_ sender: Any) {
counter += 1
let alert = UIAlertController(title: "Counter", message: "You've pressed the button \(counter) times", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
4. Java (Android)
Java 是 Android 开发的主要语言之一。
java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
Toast.makeText(MainActivity.this, "You've pressed the button
由于生成一个完整的App代码涉及多个方面(如前端、后端、数据库、UI设计等),且不同的平台和语言有不同的实现方式,这里我将为你提供几种常见编程语言和框架的示例代码片段,这些代码片段可以帮助你开始开发一个简单的App。
- Flutter (Dart)
Flutter 是一个用于构建跨平台移动应用的框架。
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 用于构建原生移动应用。
jsx
import React from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
export default function App() {
const [count, setCount] = React.useState(0);
return (
<Button
title=“Press me”
onPress={() => setCount(count + 1)}
/>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
3. Swift (iOS)
Swift 是 iOS 开发的主要语言。
swift
import UIKit
class ViewController: UIViewController {
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func incrementCounter(_ sender: Any) {
counter += 1
let alert = UIAlertController(title: "Counter", message: "You've pressed the button \(counter) times", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
4. Java (Android)
Java 是 Android 开发的主要语言之一。
java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
Toast.makeText(MainActivity.this, "You've pressed the button