flutter代码示例
时间: 2023-09-08 18:10:40 浏览: 208
非常棒flutter项目,flutter漂亮UI,快速上手,flutter快速开发例子,非常就拿对flutter学习的demo
5星 · 资源好评率100%
以下是一个使用Flutter开发的示例代码:
```dart
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter示例',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<String> repositories = []; // 用于存储库列表
@override
void initState() {
super.initState();
fetchRepositories(); // 获取存储库列表
}
void fetchRepositories() async {
// 使用http库发送网络请求
var response = await http.get('https://api.github.com/users/myusername/repos');
if (response.statusCode == 200) {
// 如果请求成功,解析返回的JSON数据
List<dynamic> json = jsonDecode(response.body);
List<String> repos = json.map((repo) => repo['name']).toList();
setState(() {
repositories = repos;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('存储库列表'),
),
body: ListView.builder(
itemCount: repositories.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(repositories[index]),
onTap: () {
// 导航到存储库详情页面
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RepositoryDetailPage(repositoryName: repositories[index]),
),
);
},
);
},
),
);
}
}
class RepositoryDetailPage extends StatelessWidget {
final String repositoryName;
RepositoryDetailPage({required this.repositoryName});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(repositoryName),
),
body: WebView(
url: 'https://github.com/myusername/$repositoryName',
),
);
}
}
```
这个示例代码展示了如何使用Flutter开发一个简单的应用程序。它包括显示用户的存储库列表,显示用户个人资料,以及显示每个存储库的详细信息。代码中使用了一些常用的Flutter库,如http用于网络请求,flutter_webview_plugin用于加载静态页面,flutter_swiper用于实现轮播效果。你可以根据自己的需求进行修改和扩展。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [flutter_github:使用GitHub API的Flutter项目示例](https://download.csdn.net/download/weixin_42104947/18224673)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Flutter 完整示例](https://blog.csdn.net/Android_SE/article/details/100773959)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文