扑中的样式剪贴板

时间:2018-11-27 14:02:36

标签: flutter flutter-layout

我正在寻找一种样式化剪贴板动作的方式,而无需触摸主要样式主题的textTheme / button属性。这有可能吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

似乎样式直接与主题相关。不是最好的主意,但是如果您确实想要,则需要创建一个自定义弹出窗口并自己处理所有操作。

这应该让您入门...

输出:

output

代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _controller = new TextEditingController();
  final _textfieldFocusNode = new FocusNode();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(20.0),
              child: GestureDetector(
                // intercept all pointer calls
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  FocusScope.of(context).requestFocus(_textfieldFocusNode);
                },
                onLongPress: () {
                  showMenu(
                    context: context,
                    // TODO: Position dynamically based on cursor or textfield
                    position: RelativeRect.fromLTRB(0.0, 300.0, 300.0, 0.0),
                    items: [
                      PopupMenuItem(
                        child: Row(
                          children: <Widget>[
                            // TODO: Dynamic items / handle click
                            PopupMenuItem(
                              child: Text(
                                "Paste",
                                style: Theme.of(context)
                                    .textTheme
                                    .body2
                                    .copyWith(color: Colors.red),
                              ),
                            ),
                            PopupMenuItem(
                              child: Text("Select All"),
                            ),
                          ],
                        ),
                      ),
                    ],
                  );
                },
                child: IgnorePointer(
                  // ensures textfield doesn't overrule GestureDetector
                  child: TextField(
                    focusNode: _textfieldFocusNode,
                    controller: _controller,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}