【问题标题】:Flutter SearchDelegate: How to change the color of the blue bubbles from text cursor and select?Flutter SearchDelegate:如何从文本光标更改蓝色气泡的颜色并选择?
【发布时间】:2021-10-03 23:11:00
【问题描述】:

Flutter SearchDelegate:如何在 Flutter 中更改搜索代理小部件中蓝色气泡的颜色?

环境: sdk: ">=2.12.0

在我的 Scaffold-Appbar 中,我有一个带有 IconButton 的操作,然后我调用 DataSearch。然后我在 DataSearch 中设置 ThemeData appBarTheme。就是这样。

      appBar: AppBar(
        title: Text(widget.title),
        actions: [
          IconButton(
            icon: const Icon(Icons.search),
            onPressed: () {
              showSearch(context: context, delegate: DataSearch());
            },
          ),
        ],
      ),


class DataSearch extends SearchDelegate<String> {
  @override
  ThemeData appBarTheme(BuildContext context) {
    return ThemeData(
      textSelectionTheme: TextSelectionThemeData(
        //cursorColor: Colors.red,
        selectionHandleColor: Colors.red,
        //selectionColor: Colors.white,
      ), // cursor color
    );
  }

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以通过设置selectionHandleColor 来更改textSelectionTheme 内的颜色。

    textSelectionTheme: TextSelectionThemeData(
            selectionHandleColor: Colors.red, // Change bubble to red
            cursorColor: Colors.white,
          ), 
    

    编辑:完整示例

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(
            textSelectionTheme: TextSelectionThemeData(
              selectionHandleColor: Colors.red,
            ),
            primaryColor: Colors.green,
          ),
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            appBar: AppBar(
              title: TextField(),
            ),
            body: Center(
              child: Text('MyApp'),
            ),
          ),
        );
      }
    }
    

    结果:

    【讨论】:

    • selectionHandleColor 不起作用。 cursorColor 工作正常。
    • 不知道为什么它不适合你。我的气泡颜色在这个例子中变成了红色,并且工作得很好:/
    • 我创建了一个新的空项目,仅使用您的解决方案,它也无法正常工作,所以我在问题中添加了更多代码和描述
    • 也许你可以分享一个小示例项目到github并在这里发布。谢谢。
    • 它在 SearchDelegate-ThemeData 中不起作用,但它在 MaterialApp-ThemeData 中起作用,这意味着它接管了所有 SearchDelegate。即 selectionColor 必须在每个 SearchDelegate-ThemeData 中设置。
    猜你喜欢
    • 2020-05-26
    • 2016-05-22
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2021-12-26
    • 2017-04-14
    • 2012-05-21
    • 2021-08-08
    相关资源
    最近更新 更多