flutter制作具有自定义导航栏的渐进式 Web 应用程序,福利分享
import 'package:flutter/material.dart';
import 'package:flutter_web/chrome.dart' as chrome;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Custom Navigation Bar',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Custom Navigation Bar'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
// 设置自定义导航栏
chrome.setBrowserStyle({
'textColor': '#FFFFFF',
'contextMenuColor': '#FFFFFF',
'loadingBarColor': '#009688',
'accentColor': '#009688',
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'This is a progressive web app with a custom navigation bar.',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
);
}
}
这段代码创建了一个Flutter Web应用,并在initState
方法中调用了chrome.setBrowserStyle
来设置自定义的浏览器样式。这个例子展示了如何为Progressive Web App (PWA) 定制导航栏的颜色和文字颜色。
评论已关闭