gpt4 book ai didi

点击菜单图标时 flutter 抽屉

转载 作者:行者123 更新时间:2023-12-03 23:04:25 31 4
gpt4 key购买 nike

我目前正在试用抽屉小部件。我构建了一个带有汉堡菜单图标、标题文本和搜索图标的应用栏。
这是我的代码:
`

import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: PreferredSize(
child: Container(
color: Colors.white,
padding:
EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(Icons.menu),
Text(
'Appbar',
style: TextStyle(fontWeight: FontWeight.bold),
),
Icon(Icons.search)
],
),
),
preferredSize: Size.fromHeight(40)),
backgroundColor: Hexcolor('#e9f1fe'),
body: Center(
child: Text('Experimenting drawer'),
)));
}
}
`
这是输出: enter image description here ,
另外,我有一个单独的 dart 文件用于自定义抽屉,我希望在点击汉堡菜单图标时显示。我怎样才能做到这一点?
这是自定义抽屉的代码:
    import 'package:flutter/material.dart';

class DrawerScreen extends StatefulWidget {
@override
_DrawerScreenState createState() => _DrawerScreenState();
}

class _DrawerScreenState extends State<DrawerScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(
child: Column(
children: [
ListTile(
leading: CircleAvatar(
radius: 28,
backgroundColor: Colors.blueGrey,
child: CircleAvatar(
radius: 50,
backgroundImage: AssetImage('assets/images/joker.jpg'),
),
),
title: Text(
'Joaquin Phoenix',
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
subtitle: Text(
"You wouldn't get it",
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontWeight: FontWeight.w400,
fontSize: 10.0),
),
)
],
)),
backgroundColor: Colors.blue[50],
);
}
}

最佳答案

您可以复制粘贴运行下面的两个完整代码
第一步:_DrawerScreenState删除 Scaffold第 2 步:对于 drawer背景颜色,你可以用 Theme 包裹

class _DrawerScreenState extends State<DrawerScreen> {
@override
Widget build(BuildContext context) {
return Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.blue[50],
),
child: Drawer(
第 3 步:使用 DrawerScreen()main.dart
SafeArea(
child: Scaffold(
drawer: DrawerScreen(),
第 4 步:包装 Icons.menuBuilder并打开 openDrawer()
Builder(
builder: (context) => GestureDetector(
onTap: () {
Scaffold.of(context).openDrawer();
},
child: Icon(Icons.menu)),
),
工作演示
enter image description here
全码 main.dart
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'drawer.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
drawer: DrawerScreen(),
appBar: PreferredSize(
child: Container(
color: Colors.white,
padding:
EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Builder(
builder: (context) => GestureDetector(
onTap: () {
Scaffold.of(context).openDrawer();
},
child: Icon(Icons.menu)),
),
Text(
'Appbar',
style: TextStyle(fontWeight: FontWeight.bold),
),
Icon(Icons.search)
],
),
),
preferredSize: Size.fromHeight(40)),
backgroundColor: Hexcolor('#e9f1fe'),
body: Center(
child: Text('Experimenting drawer'),
)));
}
}
全码 drawer.dart
import 'package:flutter/material.dart';

class DrawerScreen extends StatefulWidget {
@override
_DrawerScreenState createState() => _DrawerScreenState();
}

class _DrawerScreenState extends State<DrawerScreen> {
@override
Widget build(BuildContext context) {
return Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.blue[50],
),
child: Drawer(
child: Column(
children: [
ListTile(
leading: CircleAvatar(
radius: 28,
backgroundColor: Colors.blueGrey,
child: CircleAvatar(
radius: 50,
backgroundImage: AssetImage('assets/images/joker.jpg'),
),
),
title: Text(
'Joaquin Phoenix',
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
subtitle: Text(
"You wouldn't get it",
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontWeight: FontWeight.w400,
fontSize: 10.0),
),
)
],
)),
);
}
}

关于点击菜单图标时 flutter 抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63515730/

31 4 0