【问题标题】:Flutter MaterialApp - How can I use the AppBar title as a linkFlutter MaterialApp - 如何使用 AppBar 标题作为链接
【发布时间】:2018-07-07 05:25:36
【问题描述】:

我想将由 New Project 模板创建的默认 Flutter 应用的 AppBar 的标题转换为链接。这是我的代码:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
  return new MaterialApp(
    title: 'WaterFlow Target',
    theme: new ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: new MyHomePage(title: 'WaterFlow'),
  );
}
}

在第 9 行中,您可以看到带有 title 参数的 MyHomePage 构造函数调用。

【问题讨论】:

  • 请详细说明。 将其用作链接是什么意思?你想让 appBar 可以点击吗?

标签: android-studio dart flutter


【解决方案1】:

url_launcher 插件添加到您的pubspec.yaml

dependencies:
  url_launcher: 2.0.1

写下这段代码:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class TitleLink extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          elevation: 0.5,
          centerTitle: true,
          title: new InkWell(
            child: new Text("www.google.com"),
            onTap: () => launch('https://google.com'),
          ),
        ),
        body: new Center(child: new Text("Tap on Title to open Google"))
    );
  }
}

【讨论】:

  • 在 Flutter 中使用 url_launcher 时,请确保您的 Flutter 项目的 Gradle 构建设置与 Flutter 项目版本兼容。如果您收到任何 Gradle 错误报告,请切换到 url_launcher 包的 1.0.2 版本。详情请见pub.dartlang.org/packages/url_launcher/versions/…
猜你喜欢
  • 2019-09-09
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
  • 2019-06-04
  • 2020-01-16
  • 1970-01-01
  • 2020-03-13
  • 1970-01-01
相关资源
最近更新 更多