0

我想点击屏幕,并将背景颜色更改为随机颜色。它工作正常,但我想在屏幕中心添加文本,请帮助我。也许有人有想法,谢谢!我尝试将多个孩子添加到 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;
  }
}

主要.dart

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:如果我将子(文本)添加到 GestureDetector -gestureDetector 仅适用于文本。

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
}
4

2 回答 2

4

您可以Text在 a 中添加小部件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'),
    ),
  );

希望,它会有所帮助

于 2020-03-09T12:57:21.690 回答
0

你应该可以Column在你的AnimatedContainer. 将Column持有多个Widgets. 我已经更新了代码以显示完整的示例。

ScaffoldColorCenterText60597839(
  child: Text('Bananas'),
)

class ScaffoldColorCenterText60597839 extends StatefulWidget {
  final Widget child;
  ScaffoldColorCenterText60597839({
    this.child
  });

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

class _ScaffoldColorCenterText60597839State extends State<ScaffoldColorCenterText60597839> {
  Color _color = Colors.white;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedContainer(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              widget.child,
              Text('A text widget')
            ],
          )
        ),
        color: _color,
        duration: Duration(milliseconds: 500),
      ),
    );
  }
}
于 2020-03-09T09:48:05.163 回答