【问题标题】:Flutter Drawer when menu icon is tapped点击菜单图标时颤动抽屉
【发布时间】:2020-08-21 02:38:26
【问题描述】:

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

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'),
            )));
  }
}

`

这是输出:

我还有一个单独的 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],
    );
  }
}

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    您可以在下面复制粘贴运行两个完整代码
    第 1 步:_DrawerScreenState 删除 Scaffold
    第二步: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 步:在 main.dart 中使用 DrawerScreen()

    SafeArea(
        child: Scaffold(
            drawer: DrawerScreen(),
    

    第4步:用Builder包裹Icons.menu并用openDrawer()打开

    Builder(
            builder: (context) => GestureDetector(
                onTap: () {
                  Scaffold.of(context).openDrawer();
                },
                child: Icon(Icons.menu)),
          ),
    

    工作演示

    完整代码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),
                    ),
                  )
                ],
              )),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多