我正在尝试将该横幅的名称“debug”更改为“dev”、“stage”、“prod”。以编程方式
我想在单击该调试横幅时执行一些操作。
我想覆盖该调试横幅的所有功能。
我是否只需使用此创建一个新的自定义横幅:
https://api.flutter.dev/flutter/widgets/Banner-class.html
或者有什么办法吗
调试横幅是否仅在调试模式下显示?是否可以实现多种用途。
提前致谢。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
),
Align(
alignment: Alignment.topRight,
child: Container(
padding: const EdgeInsets.only(top: 55, right: 50),
child: const Banner(
message: "Custom",
location: BannerLocation.bottomStart,
),
),
),
],
);
}
}
我认为为了做到这一点,您不仅需要创建自己的横幅,还需要创建自己的 WidgetsApp。不像 MaterialApp 将选项包裹在 WidgetsApp 中,而是您自己的 WidgetsApp。这意味着您必须复制 WidgetsApp 的 flutter 代码并从那里进行修改。
通过将您的应用程序包装在
Banner
小部件中(位于适合您的小部件树的任何级别),可以轻松实现这一点。
@override
Widget build(BuildContext context) {
return Banner(
message: 'message...',
location: BannerLocation.topEnd,
child: Scaffold(...),
);
}