andrea pirola
andrea pirola

Reputation: 101

Flutter : change position of text on the appbar

How can i change the position of my text "Sito Web" on the right of the appbar? I tryid with

alignment: Alignment.TopRight

but he move it the text on the top right corner down the appbar. Here a screen of what i mean https://ibb.co/X28TzNN change the position of "Sito Web" in the position of the red cirle. That's the codes, i tryid with this method too, but can't move in the appbar. Here the file with the appbar background_image_task-9.dart

import 'package:flutter/material.dart';

class BackgroundImage extends StatelessWidget{
  final Widget body;
  BackgroundImage({this.body});
    @override
    Widget build(BuildContext context){
      return Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text('Blumax', style: TextStyle(
            fontWeight: FontWeight.w500,
            fontFamily: 'DancingScript',
            fontSize: 40
          ),),
          centerTitle: false,
        ),
        body: Stack(
          children: <Widget>[Container(
          decoration: BoxDecoration(
            image: DecorationImage(image: AssetImage("assets/blumax.jpg"), fit: BoxFit.cover),
          ),
        ),
          body
         ]
       )
    );
  }
}

And here the file with the container hyperlink.dart

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

class Hyperlink extends StatelessWidget {
  final String _url;
  final String _text;

  Hyperlink(this._url, this._text);

  _launchURL() async {
    if (await canLaunch(_url)) {
      await launch(_url);
    } else {
      throw 'Could not launch $_url';
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: Container(
        alignment: Alignment(0.9, -1.0),
      child: Text(
        _text,
        textAlign: TextAlign.right,
        style: TextStyle(
          fontFamily: 'RobotoMono',
          fontSize: 20,
          color: Colors.white,
          decoration: TextDecoration.underline),
      )),
      onTap: _launchURL,
    );
  }
}

main.dart

import 'package:flutter/material.dart';
import 'background_image_task-9.dart';
import 'hyperlink.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Blumax',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: myColour
      ),
      home: BackgroundImage(
        body: Center(
          child: Hyperlink('www.test.it', 'Sito Web',),
        ),
      )
    );
  }
}

Upvotes: 4

Views: 8085

Answers (1)

Darshan
Darshan

Reputation: 11634

You can use Row widget and use mainAxisAlignment: MainAxisAlignment.spaceBetween to render BluMax and Sito Web elements on left and right of the appbar respectively. And since you want to open a url after tapping on sito web, you can wrap that element with GestureDetector and then call launchURL method that opens required url. A working sample code is as below:

appBar: AppBar(
          title: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text('Blumax'),
              GestureDetector(
                  child: Text('Sito Web'),
                  onTap: _openURL
              )

            ],
          ),
          centerTitle: false,
          actions: <Widget>[
            PopupMenuButton<int>(
              itemBuilder: (context) =>
              [
                PopupMenuItem(
                  value: 1,
                  child: Text("First"),
                ),
                PopupMenuItem(
                  value: 2,
                  child: Text("Second"),
                ),
              ],
            )
          ],
        ),

This is rendered on screen as:

enter image description here ...

void _openURL() async {
      const url = 'https://flutter.dev';
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    }

I just used a text Blumax for demo, but you may replace it per your need.

Hope this answers your question.

Upvotes: 6

Related Questions