Flutter-使用材料在Longpress上更改应用栏

时间:2019-03-21 11:38:50

标签: flutter gesture material appbar

我正在尝试使用Flutter做这种事情: https://storage.googleapis.com/spec-host-backup/mio-design%2Fassets%2F0B3T7oTWa3HiFcHBDaTlreHdVZGc%2Fitem-selection-selecting-items.mp4

var gestureTemp = GestureDetector(
    onLongPress: (){
      print('LONG PRESSED');
      //CHANGE APPBAR
    },
    child: Padding(
        padding: EdgeInsets.only(right:8),
        child: Chip(
            avatar: CircleAvatar(
                backgroundColor: Colors.grey.shade800,
                child: icon
            ),
            label: Text(space.label, style: TextStyle(fontSize: 12, color:Colors.grey.shade800))
    ),
  ));

它检测到长按,但是我不知道如何更改我的应用栏...

有什么想法吗?

编辑:这是我要做的

var appBar1 = AppBar(...);
var appBar2 = AppBar(...);
var appBar = appBar1;

我的appBar显示在我的脚手架中。

在我的GestureDetector上:

onLongPress: (){
  print('LONG PRESSED');
  setState(() {
    appBar = appBar2;
  });
},

1 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow!

您描述的方法听起来很正确。这是一个独立的示例,因此您可以仔细检查代码:

AppBar Switch Demo

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: MyPage());
  }
}

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  static final AppBar _defaultBar = AppBar(
    title: Text('Inbox'),
    leading: Icon(Icons.menu),
    actions: <Widget>[Icon(Icons.search), Icon(Icons.more_vert)],
    backgroundColor: Colors.black,
  );

  static final AppBar _selectBar = AppBar(
    title: Text('1'),
    leading: Icon(Icons.close),
    actions: <Widget>[
      Icon(Icons.flag),
      Icon(Icons.delete),
      Icon(Icons.more_vert)
    ],
    backgroundColor: Colors.deepPurple,
  );

  AppBar _appBar = _defaultBar;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _appBar,
      body: Center(
        child: RaisedButton(
          child: Text('Switch!'),
          onPressed: () {
            setState(() {
              _appBar = _appBar == _defaultBar
                  ? _selectBar
                  : _defaultBar;
            });
          },
        ),
      ),
    );
  }
}