flutter 扑动/镖动:如何使文本在超出其容器宽度时可悬停

k10s72fa  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(187)

我有一个艺术家的名单,我把艺术家的子流派作为副标题:

我希望字幕的额外部分可以像[techno, rap ...一样悬停,并且只有在鼠标悬停在字幕上时才显示整个列表。
下面是列表创建部分

Container(
            height: height,
            child: _foundUsers.isNotEmpty
                ? ListView.builder(
                    itemCount: _foundUsers.length,
                    itemBuilder: (context, index) => Card(
                        key: ValueKey(_foundUsers[index]["name"]),
                        color: Colors.blue,
                        // elevation: 1,
                        margin: const EdgeInsets.symmetric(vertical: 5.0),
                        child: Container(
                          height: height / _cardsPerScreen - 10,
                          child: _foundUsers.isNotEmpty
                              ? SingleChildScrollView(
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      ListTile(
                                        leading: Text(
                                          _foundUsers[index]["id"].toString(),
                                          style: const TextStyle(
                                              fontSize: 24,
                                              color: Colors.white),
                                        ),
                                        title: Text(
                                            _foundUsers[index]['artist'].name,
                                            style:
                                                TextStyle(color: Colors.white)),
                                        subtitle: Text(
                                            '${_foundUsers[index]["artist"].subgenres.toString()}',
                                            style:
                                                TextStyle(color: Colors.white)),
                                      ),
                                    ],
                                  ),
                                )
                              : Container(),
                        )),
                  )
                : const Text(
                    'No results found',
                    style: TextStyle(fontSize: 24),
                  ),
          ),
qv7cva1a

qv7cva1a1#

这个效果被称为TextOverflow效果。您可以通过添加maxLines和overflow参数将此效果添加到您的文本部件中,如下所示:-

Text('${_foundUsers[index]["artist"].subgenres.toString()}',
maxLines: 1, //  <-- add this parameter
overflow: TextOverflow.ellipsis, <-- add this parameter
style:TextStyle(color: Colors.white))

关于Hovering效果,可以使用悬停1.0.4:https://pub.dev/packages/hovering。您必须使用Hover部件将Text部件换行,如下所示:-

HoverWidget(
  onHover: (PointerEnterEvent event) {},
  hoverChild: Padding(
    padding: const EdgeInsets.only(left: 5, right: 5),
    child: Text(
        '${_foundUsers[index]["artist"].subgenres.toString()}',
        style: TextStyle(color: Colors.white)),
  ),
  child: Padding(
      padding: const EdgeInsets.only(left: 5, right: 5),
      child: Text(
          '${_foundUsers[index]["artist"].subgenres.toString()}',
          maxLines: 1, //  <-- add this parameter
          overflow: TextOverflow.ellipsis, //  <-- add this parameter
          style: TextStyle(color: Colors.white))),
),

完整的代码:-(我已经使用了静态文本,如名称和abcde...在此代码)

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:hovering/hovering.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: TextHover(),
        ),
      ),
    );
  }
}

class TextHover extends StatefulWidget {
  final ValueChanged<double>? valueChanged;

  const TextHover({super.key, this.valueChanged});

  @override
  TextHoverState createState() {
    return TextHoverState();
  }
}

class TextHoverState extends State<TextHover> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            for (int i = 0; i < 5; i++) ...[
              Center(
                child: Container(
                  color: Colors.green[200],
                  width: 120,
                  height: 100,
                  margin: const EdgeInsets.only(top: 10, bottom: 10),
                  child: Column(
                    children: [
                      const SizedBox(
                        height: 20,
                      ),
                      Expanded(child: Text('Name : $i')),
                      Expanded(
                        child: HoverWidget(
                          onHover: (PointerEnterEvent event) {},
                          hoverChild: Center(
                            child: Padding(
                              padding: const EdgeInsets.only(left: 5, right: 5),
                              child: Text(
                                '$i:abcdefghijklmnopqrstuvwxyz',
                              ),
                            ),
                          ),
                          child: Center(
                            child: Padding(
                              padding: const EdgeInsets.only(left: 5, right: 5),
                              child: Text(
                                '$i:abcdefghijklmnopqrstuvwxyz',
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 20,
                      ),
                    ],
                  ),
                ),
              )
            ]
          ],
        ),
      ),
    );
  }
}

相关问题