Flutter 案例学习之:Drawer + Routes

GitHub:https://github.com/happy-python/flutter_demos/tree/master/drawer_demo

Flutter 案例学习之:Drawer + Routes_第1张图片
Flutter 案例学习之:Drawer + Routes_第2张图片
Flutter 案例学习之:Drawer + Routes_第3张图片

lib/main.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Tutorial",
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  void _openPage(context, String title) {
    Navigator.of(context).push(MaterialPageRoute(
        builder: (BuildContext context) => DetailPage(title)));
  }

  void _closePage(context) {
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Drawer"),
      ),
      drawer: Drawer(
        child: ListView(
          children: [
            UserAccountsDrawerHeader(
              accountName: Text("Jack"),
              accountEmail: Text("[email protected]"),
              decoration: BoxDecoration(color: Colors.orange),
              currentAccountPicture: CircleAvatar(
                backgroundImage: AssetImage("avatar.png"),
                backgroundColor: Colors.black26,
              ),
              otherAccountsPictures: [
                CircleAvatar(
                  backgroundColor: Colors.black26,
                  child: Text("A"),
                ),
                CircleAvatar(
                  backgroundColor: Colors.black26,
                  child: Text("B"),
                ),
              ],
            ),
            ListTile(
              title: Text("Page 1"),
              trailing: Icon(Icons.arrow_forward),
              onTap: () => _openPage(context, "Page 1 Detail"),
            ),
            ListTile(
              title: Text("Page 2"),
              trailing: Icon(Icons.arrow_forward),
              onTap: () => _openPage(context, "Page 2 Detail"),
            ),
            ListTile(
              title: Text("close"),
              trailing: Icon(Icons.close),
              onTap: () => _closePage(context),
            ),
          ],
        ),
      ),
    );
  }
}

class DetailPage extends StatelessWidget {
  final String title;

  DetailPage(this.title);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Text(title),
      ),
    );
  }
}

在这里关于 AssetImage 的使用方式,参考文档:
https://docs.flutter.io/flutter/painting/AssetImage-class.html

修改主配置文件:pubspec.yaml

assets:
    - avatar.png

目录结构如下图:


Flutter 案例学习之:Drawer + Routes_第4张图片

你可能感兴趣的:(Flutter 案例学习之:Drawer + Routes)