gpt4 book ai didi

flutter - Flutter:我可以在其中添加Text()的地方

转载 作者:行者123 更新时间:2023-12-03 04:10:01 29 4
gpt4 key购买 nike

我想点击屏幕,然后将背景色更改为随机色。它工作正常,但我想在屏幕中央添加文本,请帮帮我。
也许有人有想法,谢谢!
我尝试将多个子项添加到AnimatedContainer中。
试图在GestureDetector中添加文本,但无法正常工作

TopScreen.dart

import 'package:flutter/material.dart';

class TopScreen extends StatefulWidget {
final Widget child; //child widget

TopScreen({this.child});

@override
State createState() => _TopScreenState();

static _TopScreenState of (BuildContext context) {
assert(context != null);
final _TopScreenState result = context.findAncestorStateOfType();
return result;
}
}

class _TopScreenState extends State<TopScreen> {
Color _color = Colors.white;

@override
Widget build(BuildContext context) {
return Scaffold(
body: AnimatedContainer(
child: widget.child,
color: _color,
duration: Duration(milliseconds: 500),
),
);
}

void setColor(Color color) {
this._color = color;
}
}

主镖
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:solidsoftwaretest/TopScreen.dart';

void main() => runApp(MaterialApp(home: TopScreen(child: MainScreen())));

class MainScreen extends StatefulWidget {
@override
State createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
TopScreen.of(context).setColor(Colors.primaries[Random().nextInt(Colors.primaries.length)]);
TopScreen.of(context).setState(() {});
},

);
} //build
}

UPD:如果我将“ child (文本)”添加到GestureDetector-手势姿态仅适用于Text。
class _MainScreenState extends State<MainScreen> {

@override
Widget build(BuildContext context) {
return GestureDetector(
child: Center(
child: Text('Hey there!')
),
onTap: () {
TopScreen.of(context).setColor(Colors.primaries[Random().nextInt(Colors.primaries.length)]);
TopScreen.of(context).setState(() {});
}
);
} //build
}

最佳答案

您可以在Text中添加Container小部件,然后将其进一步包装在GestureDetector中。为了使GestureDetector在整个区域都能正常工作,请给Container一个透明的颜色,如下所示:

return GestureDetector(
onTap: () {
TopScreen.of(context).setColor(Colors.primaries[Random().nextInt(Colors.primaries.length)]);
TopScreen.of(context).setState(() {});
},
child: Container(
color: Colors.transparent,
alignment: Alignment.center,
child: Text('Hey there'),
),
);

希望会有所帮助

关于flutter - Flutter:我可以在其中添加Text()的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60597839/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com