flutter->Scaffold左侧/右侧侧边栏和UserAccountsDrawerHeader的使用

本文讨论了在Flutter应用中,如何处理AppBar的leading/actions与Scaffold的drawer(包括endDrawer)之间的冲突,以及展示了如何在主页布局中使用相关组件。
摘要由CSDN通过智能技术生成

//appBar的 leading/actions   和 Scaffold的drawer/endDrawer  冲突只能存在一个

 

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(title: 'contaniner', home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final _leftList = List.generate(
    20,
    (index) => const Column(
      children: [
        ListTile(
          leading: CircleAvatar(child: Icon(Icons.swipe_left, size: 30)),
          title: Text('左侧数据'),
          trailing: Icon(Icons.keyboard_arrow_right),
        ),
        Divider()
      ],
    ),
  );
  final _rightList = List.generate(
    20,
    (index) => const Column(
      children: [
        ListTile(
          leading: CircleAvatar(child: Icon(Icons.swipe_right, size: 30)),
          title: Text('右侧数据'),
          selectedColor: Colors.green,
          trailing: Icon(Icons.keyboard_arrow_right),
        ),
        Divider()
      ],
    ),
  );
  static const Widget _userImage = CircleAvatar(
      backgroundImage: NetworkImage('https://img0.baidu.com/it/u=2462933260,1879339806&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500'),
      radius: 200 //设置半径
  );
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: Column(
          children: [
            Container(
              width: double.infinity,
              child: const UserAccountsDrawerHeader(
                decoration: BoxDecoration(
                  // color: Colors.green,
                  image: DecorationImage(
                    fit: BoxFit.cover,
                    image: NetworkImage(
                        'https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500'),
                  ),
                ),
                accountName: Text('小刀刀'),
                accountEmail: Text('49701361@qq.com'),
                currentAccountPicture: _userImage,
                otherAccountsPictures: [
                  _userImage,
                  _userImage,
                ],
              ),
            ),
            Expanded(
              child: ListView(
                children: [..._leftList],
              ),
            ),
          ],
        ),
      ),
      endDrawer: Drawer(
        child: Column(
           children: [
            Container(
              width: double.infinity,
              child: const DrawerHeader(
                decoration: BoxDecoration(color: Colors.green),
                child: Text("左侧侧数据"),
              ),
            ),
            Expanded(
              child: ListView(
                children: [..._rightList],
              ),
            ),
          ],
        ),
      ),
      appBar: AppBar(
        title: const Center(child: Text('flutter bar')),
        // leading: IconButton(
        //   onPressed: () => print("我是图标按钮1"),
        //   icon: Icon(Icons.ac_unit), //设置图标
        //   color: Colors.red, //设置按钮颜色
        //   splashColor: Colors.yellow, //设置水波纹
        //   highlightColor: Colors.purple, //设置高亮的颜色
        //   tooltip: '我是提示信息', //提示信息
        // ),
        // actions: const [
        //   Icon(Icons.settings),
        //   Icon(Icons.vaccines),
        // ],
        backgroundColor: Colors.cyan[800],
        elevation: 0.0,
        centerTitle: true,
      ),
      //带索引的集合循环
      body:HomeWidget()
    );
  }
}

class HomeWidget extends StatelessWidget {
  const HomeWidget({super.key});

  @override
  Widget build(BuildContext context) {
    // return const Text('我是首页数据', style: TextStyle(color: Colors.red));
    return ListView(
        children: List.generate(
      20,
      (index) => ListTile(
        leading: Icon(Icons.access_alarm, size: 30),
        title: Text('测试$index'),
        subtitle: Text('子标题$index'),
        selected: index == 1,
        selectedColor: Colors.green,
        trailing: Icon(Icons.keyboard_arrow_right),
      ),
    ));
  }
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值