Style clipboard in flutter

南笙酒味 提交于 2019-12-11 15:38:35

问题


I am looking for a way to style the clipboard actions without touching textTheme/button property of the main style theme. Is this even possible?


回答1:


Seems like the style is directly tied to the theme. Not the best idea but if you really wanted to you would need to create a custom popup and handle all the actions yourself.

This should get you started...

Output:

Code:

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,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}


来源:https://stackoverflow.com/questions/53501448/style-clipboard-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!