🗣 Wd提问: flutter开发网页
🤖 Ai回答: Flutter Web 开发指南
我将为你创建一个使用 Flutter 开发网页的示例,展示 Flutter 在 Web 平台的能力。这个示例将包含响应式布局、动画效果和主题切换功能。
思路分析
1、创建响应式布局,适应不同屏幕尺寸
2、实现暗色/亮色主题切换
3、添加平滑动画增强用户体验
4、模拟常见网页元素(导航栏、卡片、按钮)
下面是完整的实现代码:
dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Web 示例',
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.system,
home: const HomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage createState() = _HomePageState();
}
class _HomePageState extends State<HomePage {
int _currentIndex = 0;
bool _isDarkMode = false;
double _animationValue = 0.0;
final List<String _features = [
'响应式设计',
'跨平台支持',
'高性能渲染',
'丰富的UI组件',
'热重载开发'
];
@override
void initState() {
super.initState();
Future.delayed(const Duration(milliseconds: 300), () {
setState(() {
_animationValue = 1.0;
});
});
}
@override
Widget build(BuildContext context) {
final isDesktop = MediaQuery.of(context).size.width 900;
final isTablet = MediaQuery.of(context).size.width 600;
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Web 开发'),
actions: [
IconButton(
icon: Icon(_isDarkMode ? Icons.light_mode : Icons.dark_mode),
onPressed: () {
setState(() {
_isDarkMode = !_isDarkMode;
});
},
),
if (isDesktop)
...['首页', '产品', '服务', '关于'].map((text) {
return TextButton(
onPressed: () {},
child: Text(
text,
style: TextStyle(
color: _isDarkMode ? Colors.white : Colors.black,
),
),
);
}).toList(),
if (isDesktop)
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
),
child: const Text('联系我们'),
),
if (!isDesktop)
IconButton(
icon: const Icon(Icons.menu),
onPressed: () {},
),
],
),
body: SingleChildScrollView(
child: Column(
children: [
// 头部横幅
AnimatedOpacity(
opacity: _animationValue,
duration: const Duration(seconds: 1),
child: Container(
height: 300,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: _isDarkMode
? [Colors.blue.shade800, Colors.purple.shade800]
: [Colors.blue.shade300, Colors.purple.shade300],
),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'使用 Flutter 构建现代 Web 应用',
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 20),
const Text(
'一套代码,多平台运行',
style: TextStyle(
fontSize: 18,
color: Colors.white,
),
),
const SizedBox(height: 30),
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
foregroundColor: Colors.blue,
backgroundColor: Colors.white,
padding: const EdgeInsets.symmetric(
horizontal: 30, vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
child: const Text(
'立即开始',
style: TextStyle(fontSize: 16),
),
),
],
),
),
),
),
// 特性展示
Padding(
padding: const EdgeInsets.all(40),
child: Column(
children: [
const Text(
'Flutter Web 优势',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
Wrap(
spacing: 20,
runSpacing: 20,
children: _features.map((feature) {
return AnimatedContainer(
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOut,
width: isDesktop ? 280 : isTablet ? 200 : double.infinity,
height: 150,
decoration: BoxDecoration(
color: _isDarkMode
? Colors.grey[800]
: Colors.grey[100],
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.1),
blurRadius: 10,
spreadRadius: 2,
),
],
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Icons.star,
color: Colors.amber,
size: 30,
),
const SizedBox(height: 10),
Text(
feature,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
Text(
'Flutter 提供了优秀的$feature解决方案',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
),
),
],
),
),
);
}).toList(),
),
],
),
),
// 卡片展示
Container(
padding: const EdgeInsets.all(40),
color: _isDarkMode ? Colors.grey[900] : Colors.grey[200],
child: Column(
children: [
const Text(
'成功案例',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
GridView.count(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
crossAxisCount: isDesktop ? 3 : isTablet ? 2 : 1,
crossAxisSpacing: 20,
mainAxisSpacing: 20,
children: List.generate(6, (index) {
return Card(
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 150,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.blue.withOpacity(0.2),
),
child: const Center(
child: Icon(
Icons.photo,
size: 60,
color: Colors.blue,
),
),
),
const SizedBox(height: 15),
Text(
'项目案例 ${index + 1}',
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
Text(
'这是一个使用Flutter开发的Web应用示例,展示了跨平台的强大能力。',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
),
),
const SizedBox(height: 15),
ElevatedButton(
onPressed: () {},
child: const Text('查看详情'),
),
],
),
),
);
}),
),
],
),
),
// 页脚
Container(
height: 100,
color: _isDarkMode ? Colors.black : Colors.blue[900],
child: Center(
child: Text(
'© 2023 Flutter Web 示例 | 使用 Flutter 构建',
style: TextStyle(
color: Colors.white,
fontSize: isDesktop ? 16 : 12,
),
),
),
),
],
),
),
bottomNavigationBar: !isDesktop
? BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
items: const [
BottomNavigationBarItem(
📣 商家广告:





0
IP地址: 12.189.146.165
搜索次数: 189
提问时间: 2025-11-08 18:41:53
本站所有
❓
问答
均由Ai自动分析整理,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
本站由
🟢
豌豆Ai
提供技术支持,使用的最新版:
豌豆Ai站群搜索引擎系统 V.25.10.25
搭建本站。