avatar

松果工作室

欢迎光临

  • 首页
  • ESP
  • LVGL
  • freeRTOS
  • 快速笔记
  • 考察日志
  • 个人收藏
  • 我的服务
Home (Flutter) 侧边导航栏的实现
文章

(Flutter) 侧边导航栏的实现

Posted 2025-04-9 Updated 2025-04- 9
By YCP
8~11 min read

跟着视频敲了几天到今天开始有了一些手感,能自己独立完成部分代码了

CODE

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void onPressed() {
    print('Button Pressed');
  }

  void onTap_Home() {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) => Home()));
  }

  void onTap_Settings() {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => Settings()));
  }

  void onTap_Music() {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => Music()));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My App'),
        backgroundColor: Colors.blue,
      ),
      drawer: Drawer(
        child: Container(
          color: Colors.blue,
          child: ListView(
            children: [
              DrawerHeader(
                child: Center(
                  child: Text(
                    'D R A W E R',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                    ),
                  ),
                ),
              ),
              ListTile(
                leading: Icon(
                  Icons.home,
                  size: 30,
                  color: Colors.white,
                ),
                title: Text(
                  'Home',
                  style: TextStyle(fontSize: 20, color: Colors.white),
                ),
                onTap: onTap_Home,
              ),
              ListTile(
                leading: Icon(
                  Icons.settings,
                  size: 30,
                  color: Colors.white,
                ),
                title: Text(
                  'Settings',
                  style: TextStyle(fontSize: 20, color: Colors.white),
                ),
                onTap: onTap_Settings,
              ),
              ListTile(
                leading: Icon(
                  Icons.music_note,
                  size: 30,
                  color: Colors.white,
                ),
                title: Text(
                  'Music',
                  style: TextStyle(fontSize: 20, color: Colors.white),
                ),
                onTap: onTap_Music,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue[100],
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: Container(
        child:Center(
          child: Text(
            'Home Page',
            style: TextStyle(
              color: Colors.white,
              fontSize: 30,
            ),
          ),
        ),
      ),
    );
  }
}

class Settings extends StatelessWidget {
  const Settings({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
      title: Text('Settings'),
    ));
  }
}

class Music extends StatelessWidget {
  const Music({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
      title: Text('Music'),
    ));
  }
}

预览

截屏2025-04-09 下午9.14.28.png

Flutter
License:  CC BY 4.0
Share

Further Reading

OLDER

(Flutter)侧浮动顶栏

NEWER

(Flutter)一种简单布局

Recently Updated

  • (ESP-IDF)LVGL 模拟器
  • (ESP-IDF)LVGL 自定义对象加入编码器组
  • (ESP-IDF)vscode配置文件
  • (Elec)来复再生式晶体管单管收音机
  • (ESP-IDF)ESPNOW

Trending Tags

LVGL WCH Linux Elec ThatProject freeRTOS STM ESP Flutter Others

Contents

©2026 松果工作室. Some rights reserved.

Using the Halo theme Chirpy